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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
7 deletions
+38
-7
source_schema.rb
lib/weather-model/source_schema.rb
+1
-1
storer.rb
lib/weather-model/storer.rb
+24
-3
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
},
options:
{
index:
[
:cityname
],
expire:
5
,
expire:
1
,
unique: :mysql_id
,
father:
nil
,
son:
nil
,
...
...
lib/weather-model/storer.rb
View file @
390bb5b0
...
...
@@ -8,10 +8,19 @@ module WeatherModel
after_save
:update_ohm
def
self
.
use_settings
settings
self
.
table_name
=
settings
[
:table_name
]
Ohm
.
use_settings
settings
self
.
define_ohm
settings
end
class
Ohm
<
::
Ohm
::
Model
def
self
.
define_ohm
settings
const_set
(
'Ohm'
,
Class
.
new
(
SourceOhm
)).
use_settings
(
settings
)
end
def
get_ohm
self
.
class
.
const_get
(
'Ohm'
)
end
class
SourceOhm
<
::
Ohm
::
Model
include
::
Ohm
::
Expire
def
self
.
use_settings
settings
...
...
@@ -28,6 +37,17 @@ module WeatherModel
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
# def self.all_valid
# self.all.to
# end
private
def
self
.
son
his_son
# input like :Post
...
...
@@ -41,11 +61,12 @@ module WeatherModel
end
end
private
def
update_ohm
ohm_attrs
=
self
.
attributes
.
except
(
'created_at'
,
'id'
).
merge
(
mysql_id:
id
)
# 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
end
end
...
...
spec/weather-model_spec.rb
View file @
390bb5b0
...
...
@@ -35,13 +35,23 @@ RSpec.describe WeatherModel do
})
expect
(
Forecast
::
ShanghaiTen
.
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.get_ttl).to eq(5)
# 'ohm 对象 同步 mysql对象更新'
mysql_obj
.
update
(
temp_low:
99
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
# ttl 失败
# p Forecast::ShanghaiTen::Ohm.all.to_a.map(&:get_ttl)
# p Forecast::ShanghaiTen::Ohm.all.first.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
...
...
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