Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weather-model
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
weather-model
Commits
390bb5b0
Commit
390bb5b0
authored
May 16, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Ohm definition to set the right key
parent
da693bf7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
31 deletions
+62
-31
source_schema.rb
lib/weather-model/source_schema.rb
+1
-1
storer.rb
lib/weather-model/storer.rb
+48
-27
weather-model_spec.rb
spec/weather-model_spec.rb
+13
-3
No files found.
lib/weather-model/source_schema.rb
View file @
390bb5b0
...
@@ -20,7 +20,7 @@ module WeatherModel
...
@@ -20,7 +20,7 @@ module WeatherModel
},
},
options:
{
options:
{
index:
[
:cityname
],
index:
[
:cityname
],
expire:
5
,
expire:
1
,
unique: :mysql_id
,
unique: :mysql_id
,
father:
nil
,
father:
nil
,
son:
nil
,
son:
nil
,
...
...
lib/weather-model/storer.rb
View file @
390bb5b0
...
@@ -8,44 +8,65 @@ module WeatherModel
...
@@ -8,44 +8,65 @@ module WeatherModel
after_save
:update_ohm
after_save
:update_ohm
def
self
.
use_settings
settings
def
self
.
use_settings
settings
self
.
table_name
=
settings
[
:table_name
]
self
.
table_name
=
settings
[
:table_name
]
Ohm
.
use_settings
settings
self
.
define_ohm
settings
end
end
class
Ohm
<
::
Ohm
::
Model
include
::
Ohm
::
Expire
def
self
.
define_ohm
settings
def
self
.
use_settings
settings
const_set
(
'Ohm'
,
Class
.
new
(
SourceOhm
)).
use_settings
(
settings
)
end
attribute
:mysql_id
attribute
:updated_at
def
get_ohm
index
:mysql_id
self
.
class
.
const_get
(
'Ohm'
)
settings
[
:schema
].
keys
.
each
{
|
attr
|
end
attribute
attr
}
class
SourceOhm
<
::
Ohm
::
Model
[
:index
,
:unique
,
:expire
,
:son
,
:dad
].
each
do
|
option
|
include
::
Ohm
::
Expire
attrs
=
settings
[
:options
][
option
]
def
self
.
use_settings
settings
attrs
.
is_a?
(
Array
)
?
attrs
.
each
{
|
attr
|
method
(
option
).
call
(
attr
)
if
attr
}
:
attribute
:mysql_id
method
(
option
).
call
(
attrs
)
if
attrs
attribute
:updated_at
index
:mysql_id
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
}
[
:index
,
:unique
,
:expire
,
:son
,
:dad
].
each
do
|
option
|
attrs
=
settings
[
:options
][
option
]
attrs
.
is_a?
(
Array
)
?
attrs
.
each
{
|
attr
|
method
(
option
).
call
(
attr
)
if
attr
}
:
method
(
option
).
call
(
attrs
)
if
attrs
end
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def
update
attributes
obj
=
super
(
attributes
)
obj
.
update_ttl
self
.
class
.
instance_variable_get
(
:@expire
)
end
end
end
private
# def self.all_valid
def
self
.
son
his_son
# input like :Post
# self.all.to
# collection :posts, :Post
# end
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
end
private
def
self
.
son
his_son
# input like :Post
# collection :posts, :Post
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
end
def
self
.
dad
his_dad
# input like :Post
def
self
.
dad
his_dad
# input like :Post
# reference :post, :Post
# reference :post, :Post
reference
(
his_dad
.
to_s
.
underscore
.
to_sym
,
his_dad
)
if
his_dad
&&
SourceSchema
.
const_defined?
(
his_dad
)
reference
(
his_dad
.
to_s
.
underscore
.
to_sym
,
his_dad
)
if
his_dad
&&
SourceSchema
.
const_defined?
(
his_dad
)
end
end
end
end
private
private
def
update_ohm
def
update_ohm
ohm_attrs
=
self
.
attributes
.
except
(
'created_at'
,
'id'
).
merge
(
mysql_id:
id
)
ohm_attrs
=
self
.
attributes
.
except
(
'created_at'
,
'id'
).
merge
(
mysql_id:
id
)
# find 找不到 会自动生成
# find 找不到 会自动生成
ohm_obj
=
Ohm
.
find
(
mysql_id:
id
).
first
.
upd
ate
(
ohm_attrs
)
ohm_obj
=
get_ohm
.
find
(
mysql_id:
id
).
first
.
try
(
:update
,
ohm_attrs
)
||
get_ohm
.
cre
ate
(
ohm_attrs
)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
end
end
end
end
...
...
spec/weather-model_spec.rb
View file @
390bb5b0
...
@@ -35,13 +35,23 @@ RSpec.describe WeatherModel do
...
@@ -35,13 +35,23 @@ RSpec.describe WeatherModel do
})
})
expect
(
Forecast
::
ShanghaiTen
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
ShanghaiTen
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
# expect(Forecast::ShanghaiTen::Ohm.all.first.get_ttl).to eq(5)
# 'ohm 对象 同步 mysql对象更新'
# 'ohm 对象 同步 mysql对象更新'
mysql_obj
.
update
(
temp_low:
99
)
mysql_obj
.
update
(
temp_low:
99
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
# ttl 失败
# p Forecast::ShanghaiTen::Ohm.all.first.get_ttl
# p Forecast::ShanghaiTen::Ohm.all.to_a.map(&:get_ttl)
# sleep 4
# expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(1)
p
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
key
# sleep 2
p
p
Ohm
.
redis
.
call
(
'KEYS'
,
'*ShanghaiTen*'
)
# p Ohm.redis.call('HGETALL', 'Forecast::ShanghaiTen::Ohm:indices:cityname:shanghai')
# p Forecast::ShanghaiTen::Ohm.find(mysql_id: mysql_obj.id).to_a
# expect(Forecast::ShanghaiTen::Ohm.find(mysql_id: mysql_obj.id)).to eq(0)
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment