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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
17 deletions
+66
-17
storer.rb
lib/weather-model/storer.rb
+30
-6
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
index
:mysql_id
unique
:mysql_id
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
settings
[
:schema
].
each_pair
{
|
attr
,
type
|
attribute
attr
,
type_proc
(
type
)
}
set_options
settings
end
...
...
@@ -79,6 +79,28 @@ module WeatherModel
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
# 而且 只把 attributes 的值给 TTL了。。。
def
update
attributes
...
...
@@ -86,12 +108,14 @@ module WeatherModel
obj
.
update_ttl
self
.
class
.
instance_variable_get
(
:@expire
)
end
def
self
.
all
super
.
select
{
|
x
|
x
.
attributes
.
empty?
}.
map
(
&
:delete
)
super
def
to_h
keys
=
attributes
.
keys
-
[
:mysql_id
,
:updated_at
]
Hash
[
keys
.
zip
keys
.
map
{
|
column_name
|
send
(
column_name
)
}]
end
private
def
self
.
has_many
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
)
...
...
@@ -103,7 +127,7 @@ module WeatherModel
end
def
self
.
json_column
column
attribute
column
,
->
(
value
)
{
attribute
column
,
proc
{
|
value
|
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
...
...
spec/weather-model_spec.rb
View file @
1b5bf547
...
...
@@ -40,27 +40,29 @@ RSpec.describe WeatherModel do
it
'Forecast'
do
@expire
=
1
Forecast
::
CityForecast
::
Ohm
.
expire
@expire
mysql_obj
=
Forecast
::
CityForecast
.
create
({
mysql
=
Forecast
::
CityForecast
ohm
=
ohm
ohm
.
expire
@expire
mysql_obj
=
mysql
.
create
({
datetime:
'2017-05-16'
,
cityname:
'shanghai'
,
temp_high:
'100'
,
temp_low:
'0'
,
weather_text1:
'酷热'
,
weather_text
2
:
'严寒'
,
weather_pic:
'
http://xxx.com
'
,
weather_text
1
:
'严寒'
,
weather_pic:
'
酷热转严寒
'
,
win_dir:
'东南西北风'
,
win_speed:
'1级'
,
})
expect
(
Forecast
::
CityForecast
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
CityForecast
::
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
(
Forecast
::
CityForecast
::
O
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
expect
(
mysql
.
count
).
to
eq
(
1
)
expect
(
o
hm
.
all
.
count
).
to
eq
(
1
)
expect
(
o
hm
.
all
.
first
.
temp_low
).
to
eq
(
mysql_obj
.
temp_low
.
to_s
)
expect
(
o
hm
.
all
.
first
.
get_ttl
).
to
eq
(
@expire
)
# 'ohm 对象 同步 mysql对象更新'
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
expect
(
Forecast
::
CityForecast
::
O
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
expect
(
o
hm
.
all
.
count
).
to
eq
(
0
),
'TTL过期对象删除成功'
end
it
'aqi_forecast'
do
...
...
@@ -85,13 +87,36 @@ RSpec.describe WeatherModel do
expect
(
Aqi
::
AqiForecast
::
Ohm
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Ohm json_column 正常'
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
[
WeatherModel
::
Forecast
::
CityForecast
,
WeatherModel
::
AutoStation
::
AutoStationTenMins
,
WeatherModel
::
Aqi
::
AqiForecast
,
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
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