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
7 years ago
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
...
@@ -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
,
...
...
This diff is collapsed.
Click to expand it.
lib/weather-model/storer.rb
View file @
390bb5b0
...
@@ -8,10 +8,19 @@ module WeatherModel
...
@@ -8,10 +8,19 @@ 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
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
include
::
Ohm
::
Expire
def
self
.
use_settings
settings
def
self
.
use_settings
settings
...
@@ -28,6 +37,17 @@ module WeatherModel
...
@@ -28,6 +37,17 @@ module WeatherModel
method
(
option
).
call
(
attrs
)
if
attrs
method
(
option
).
call
(
attrs
)
if
attrs
end
end
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
private
def
self
.
son
his_son
# input like :Post
def
self
.
son
his_son
# input like :Post
...
@@ -41,11 +61,12 @@ module WeatherModel
...
@@ -41,11 +61,12 @@ module WeatherModel
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
...
...
This diff is collapsed.
Click to expand it.
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
...
...
This diff is collapsed.
Click to expand it.
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