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
26461930
Commit
26461930
authored
May 20, 2017
by
Ivan Lan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'lan/update_json_column' into 'master'
Lan/update json column See merge request
!6
parents
d626d089
f38f6ae8
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
32 additions
and
40 deletions
+32
-40
2_create_auto_station_ten_mins.rb
db/migrate/2_create_auto_station_ten_mins.rb
+1
-1
json_columns.rb
lib/weather-model/json_columns.rb
+0
-14
aqi.rb
lib/weather-model/source_schemas/aqi.rb
+1
-1
auto_station.rb
lib/weather-model/source_schemas/auto_station.rb
+1
-4
forecast.rb
lib/weather-model/source_schemas/forecast.rb
+2
-2
storer.rb
lib/weather-model/storer.rb
+24
-16
weather-model_spec.rb
spec/weather-model_spec.rb
+3
-2
No files found.
db/migrate/2_create_auto_station_ten_mins.rb
View file @
26461930
...
@@ -20,7 +20,7 @@ class CreateTenMinAutoStations < ActiveRecord::Migration[5.0]
...
@@ -20,7 +20,7 @@ class CreateTenMinAutoStations < ActiveRecord::Migration[5.0]
end
end
add_index
:auto_station_ten_mins
,
:datetime
add_index
:auto_station_ten_mins
,
:datetime
add_index
:auto_station_ten_mins
,
:
sitenumber
add_index
:auto_station_ten_mins
,
:
name
end
end
def
down
def
down
...
...
lib/weather-model/json_columns.rb
deleted
100644 → 0
View file @
d626d089
module
JsonColumns
def
json_columns
*
columns
columns
.
each
do
|
column
|
define_method
(
column
)
{
value
=
self
.
read_attribute
(
column
)
value
&&
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
}
define_method
(
"
#{
column
}
="
)
{
|
value
|
self
.
[
]
=
(
column
,
JSON
.
dump
(
value
)
)
}
end
end
end
\ No newline at end of file
lib/weather-model/source_schemas/aqi.rb
View file @
26461930
...
@@ -15,7 +15,7 @@ module WeatherModel
...
@@ -15,7 +15,7 @@ module WeatherModel
unique:
nil
,
unique:
nil
,
belongs_to:
nil
,
belongs_to:
nil
,
has_many:
nil
,
has_many:
nil
,
json_column
s
: :list
json_column: :list
},
},
factory_name: :aqi_forecast
,
factory_name: :aqi_forecast
,
factory:
{
factory:
{
...
...
lib/weather-model/source_schemas/autostation.rb
→
lib/weather-model/source_schemas/auto
_
station.rb
View file @
26461930
...
@@ -21,11 +21,8 @@ module WeatherModel
...
@@ -21,11 +21,8 @@ module WeatherModel
pressure: :string
,
pressure: :string
,
},
},
options:
{
options:
{
index:
%i[datetime
sitenumber
]
,
index:
%i[datetime
name
]
,
expire:
4
*
60
*
60
,
expire:
4
*
60
*
60
,
unique:
nil
,
father:
nil
,
son:
nil
,
},
},
factory_name: :auto_station_ten_min
,
factory_name: :auto_station_ten_min
,
factory:
{
factory:
{
...
...
lib/weather-model/source_schemas/forecast.rb
View file @
26461930
...
@@ -15,12 +15,12 @@ module WeatherModel
...
@@ -15,12 +15,12 @@ module WeatherModel
win_speed: :string
,
win_speed: :string
,
},
},
options:
{
options:
{
index:
[
:cityname
,
:date
],
index:
[
:cityname
,
:date
time
],
expire:
11
*
24
*
60
*
60
,
expire:
11
*
24
*
60
*
60
,
unique:
nil
,
unique:
nil
,
belongs_to:
nil
,
belongs_to:
nil
,
has_many:
nil
,
has_many:
nil
,
json_column:
nil
},
},
factory_name: :city_forecast
,
factory_name: :city_forecast
,
factory:
{
factory:
{
...
...
lib/weather-model/storer.rb
View file @
26461930
...
@@ -3,10 +3,8 @@ module WeatherModel
...
@@ -3,10 +3,8 @@ module WeatherModel
require
'active_record'
require
'active_record'
require
'ohm'
require
'ohm'
require
"ohm/expire"
require
"ohm/expire"
require
'weather-model/json_columns'
class
Mysql
<
::
ActiveRecord
::
Base
class
Mysql
<
::
ActiveRecord
::
Base
extend
JsonColumns
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
]
...
@@ -42,8 +40,21 @@ module WeatherModel
...
@@ -42,8 +40,21 @@ module WeatherModel
self
.
class
.
const_get
(
'Ohm'
)
self
.
class
.
const_get
(
'Ohm'
)
end
end
# Forecast -> :forecasts , haven't tested
def
self
.
has_many
name
super
name
.
to_s
.
underscore
.
pluralize
.
to_sym
end
# Forecast -> :forecast
def
self
.
belongs_to
name
super
name
.
to_s
.
underscore
.
to_sym
end
def
self
.
json_column
column
serialize
column
end
class
SourceOhm
<
::
Ohm
::
Model
class
SourceOhm
<
::
Ohm
::
Model
extend
JsonColumns
include
::
Ohm
::
Expire
include
::
Ohm
::
Expire
def
self
.
use_settings
settings
def
self
.
use_settings
settings
...
@@ -56,6 +67,7 @@ module WeatherModel
...
@@ -56,6 +67,7 @@ module WeatherModel
settings
[
:schema
].
keys
.
each
{
|
attr
|
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
attribute
attr
}
}
set_options
settings
end
end
def
self
.
set_options
settings
def
self
.
set_options
settings
...
@@ -79,7 +91,6 @@ module WeatherModel
...
@@ -79,7 +91,6 @@ module WeatherModel
super
super
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
...
@@ -90,6 +101,14 @@ module WeatherModel
...
@@ -90,6 +101,14 @@ module WeatherModel
# 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
def
self
.
json_column
column
attribute
column
,
->
(
value
)
{
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
}
end
end
end
...
@@ -100,17 +119,6 @@ module WeatherModel
...
@@ -100,17 +119,6 @@ module WeatherModel
ohm_obj
=
get_ohm
.
find
(
mysql_id:
id
).
first
.
try
(
:update
,
ohm_attrs
)
||
get_ohm
.
create
(
ohm_attrs
)
ohm_obj
=
get_ohm
.
find
(
mysql_id:
id
).
first
.
try
(
:update
,
ohm_attrs
)
||
get_ohm
.
create
(
ohm_attrs
)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
end
end
# Forecast -> :forecasts
end
def
has_many
name
super
name
.
to_s
.
underscore
.
pluralize
.
to_sym
end
# Forecast -> :forecast
def
belongs_to
name
super
name
.
to_s
.
underscore
.
to_sym
end
end
end
end
end
end
spec/weather-model_spec.rb
View file @
26461930
...
@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do
...
@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do
end
end
it
'aqi_forecast'
do
it
'aqi_forecast'
do
@aqi_forecast
=
Aqi
::
Forecast
.
create
(
@aqi_forecast
=
Aqi
::
Aqi
Forecast
.
create
(
datetime:
Time
.
now
,
datetime:
Time
.
now
,
prompt:
''
,
prompt:
''
,
list:
[
list:
[
...
@@ -79,7 +79,8 @@ RSpec.describe WeatherModel do
...
@@ -79,7 +79,8 @@ RSpec.describe WeatherModel do
},
},
]
]
)
)
expect
(
Aqi
::
Forecast
::
Ohm
.
all
.
first
).
not_to
be_nil
,
'json_column 正常'
expect
(
Aqi
::
AqiForecast
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Mysql json_column 正常'
expect
(
Aqi
::
AqiForecast
::
Ohm
.
all
.
first
.
list
.
class
).
to
eq
(
Array
),
'Ohm json_column 正常'
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