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
1b5bf547
Commit
1b5bf547
authored
May 21, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Ohm # to_h
parent
ff582435
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
18 deletions
+67
-18
storer.rb
lib/weather-model/storer.rb
+31
-7
weather-model_spec.rb
spec/weather-model_spec.rb
+36
-11
No files found.
lib/weather-model/storer.rb
View file @
1b5bf547
...
@@ -64,8 +64,8 @@ module WeatherModel
...
@@ -64,8 +64,8 @@ module WeatherModel
index
:mysql_id
index
:mysql_id
unique
:mysql_id
unique
:mysql_id
settings
[
:schema
].
keys
.
each
{
|
attr
|
settings
[
:schema
].
each_pair
{
|
attr
,
type
|
attribute
attr
attribute
attr
,
type_proc
(
type
)
}
}
set_options
settings
set_options
settings
end
end
...
@@ -79,6 +79,28 @@ module WeatherModel
...
@@ -79,6 +79,28 @@ module WeatherModel
end
end
end
end
def
self
.
all
super
.
select
{
|
x
|
x
.
attributes
.
empty?
}.
map
(
&
:delete
)
super
end
def
self
.
type_proc
type
case
type
when
:integer
proc
{
|
val
|
val
&&
val
.
to_i
}
when
:float
proc
{
|
val
|
val
&&
val
.
to_f
}
when
:datetime
proc
{
|
val
|
val
.
is_a?
(
String
)
?
Time
.
parse
(
val
)
:
val
}
when
:json
proc
{
|
value
|
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
}
end
end
# ohm-expire 只处理了create
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
# 而且 只把 attributes 的值给 TTL了。。。
def
update
attributes
def
update
attributes
...
@@ -86,12 +108,14 @@ module WeatherModel
...
@@ -86,12 +108,14 @@ module WeatherModel
obj
.
update_ttl
self
.
class
.
instance_variable_get
(
:@expire
)
obj
.
update_ttl
self
.
class
.
instance_variable_get
(
:@expire
)
end
end
def
self
.
all
def
to_h
super
.
select
{
|
x
|
x
.
attributes
.
empty?
}.
map
(
&
:delete
)
keys
=
attributes
.
keys
-
[
:mysql_id
,
:updated_at
]
super
Hash
[
keys
.
zip
keys
.
map
{
|
column_name
|
send
(
column_name
)
}]
end
end
private
private
def
self
.
has_many
his_son
# input like :Post
def
self
.
has_many
his_son
# input like :Post
# collection :posts, :Post
# collection :posts, :Post
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
...
@@ -103,7 +127,7 @@ module WeatherModel
...
@@ -103,7 +127,7 @@ module WeatherModel
end
end
def
self
.
json_column
column
def
self
.
json_column
column
attribute
column
,
->
(
value
)
{
attribute
column
,
proc
{
|
value
|
value
.
is_a?
(
String
)
?
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
value
...
...
spec/weather-model_spec.rb
View file @
1b5bf547
...
@@ -40,27 +40,29 @@ RSpec.describe WeatherModel do
...
@@ -40,27 +40,29 @@ RSpec.describe WeatherModel do
it
'Forecast'
do
it
'Forecast'
do
@expire
=
1
@expire
=
1
Forecast
::
CityForecast
::
Ohm
.
expire
@expire
mysql
=
Forecast
::
CityForecast
mysql_obj
=
Forecast
::
CityForecast
.
create
({
ohm
=
ohm
ohm
.
expire
@expire
mysql_obj
=
mysql
.
create
({
datetime:
'2017-05-16'
,
datetime:
'2017-05-16'
,
cityname:
'shanghai'
,
cityname:
'shanghai'
,
temp_high:
'100'
,
temp_high:
'100'
,
temp_low:
'0'
,
temp_low:
'0'
,
weather_text1:
'酷热'
,
weather_text1:
'酷热'
,
weather_text
2
:
'严寒'
,
weather_text
1
:
'严寒'
,
weather_pic:
'
http://xxx.com
'
,
weather_pic:
'
酷热转严寒
'
,
win_dir:
'东南西北风'
,
win_dir:
'东南西北风'
,
win_speed:
'1级'
,
win_speed:
'1级'
,
})
})
expect
(
Forecast
::
CityForecast
.
count
).
to
eq
(
1
)
expect
(
mysql
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
count
).
to
eq
(
1
)
expect
(
o
hm
.
all
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
o
hm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
expect
(
o
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
# 'ohm 对象 同步 mysql对象更新'
# 'ohm 对象 同步 mysql对象更新'
mysql_obj
.
update
(
temp_low:
99
)
mysql_obj
.
update
(
temp_low:
99
)
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
expect
(
o
hm
.
all
.
first
.
temp_low
).
to
eq
(
'99'
),
'ohm 对象 同步 mysql对象更新'
sleep
1.5
sleep
1.5
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
expect
(
o
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
end
end
it
'aqi_forecast'
do
it
'aqi_forecast'
do
...
@@ -85,13 +87,36 @@ RSpec.describe WeatherModel do
...
@@ -85,13 +87,36 @@ RSpec.describe WeatherModel do
expect
(
Aqi
::
AqiForecast
::
Ohm
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Ohm json_column 正常'
expect
(
Aqi
::
AqiForecast
::
Ohm
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Ohm json_column 正常'
end
end
describe
'test Ohm # to_h'
do
before
do
@aqi_live
=
create
(
:aqi_live
)
end
it
'test Ohm to_h'
do
ohm_obj
=
WeatherModel
::
Aqi
::
AqiLive
::
Ohm
.
all
.
first
expect
(
ohm_obj
.
time_point
.
class
).
to
eq
(
Time
)
expect
(
ohm_obj
.
aqi
.
class
).
to
eq
(
Integer
)
expect
(
ohm_obj
.
co
.
class
).
to
eq
(
Float
)
expect
(
ohm_obj
.
area
.
class
).
to
eq
(
String
)
hash
=
ohm_obj
.
to_h
expect
(
hash
[
:time_point
].
class
).
to
eq
(
Time
)
expect
(
hash
[
:aqi
].
class
).
to
eq
(
Integer
)
expect
(
hash
[
:co
].
class
).
to
eq
(
Float
)
expect
(
hash
[
:area
].
class
).
to
eq
(
String
)
end
end
it
'Test all source data'
do
it
'Test all source data'
do
[
[
WeatherModel
::
Forecast
::
CityForecast
,
WeatherModel
::
Forecast
::
CityForecast
,
WeatherModel
::
AutoStation
::
AutoStationTenMins
,
WeatherModel
::
AutoStation
::
AutoStationTenMins
,
WeatherModel
::
Aqi
::
AqiForecast
,
WeatherModel
::
Aqi
::
AqiForecast
,
WeatherModel
::
Aqi
::
AqiLive
,
WeatherModel
::
Aqi
::
AqiLive
,
].
each
{
|
source
|
source
.
all
;
source
.
const_get
(
'Ohm'
).
all
}
].
each
{
|
source
|
# create factory
source
.
all
;
source
.
const_get
(
'Ohm'
).
all
.
first
;
}
end
end
after
do
after
do
...
...
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