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
acdb1e0f
Commit
acdb1e0f
authored
May 20, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update json columns
parent
e6ad01b5
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
37 deletions
+29
-37
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
+0
-3
forecast.rb
lib/weather-model/source_schemas/forecast.rb
+1
-1
storer.rb
lib/weather-model/storer.rb
+24
-16
weather-model_spec.rb
spec/weather-model_spec.rb
+3
-2
No files found.
lib/weather-model/json_columns.rb
deleted
100644 → 0
View file @
e6ad01b5
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 @
acdb1e0f
...
...
@@ -15,7 +15,7 @@ module WeatherModel
unique:
nil
,
belongs_to:
nil
,
has_many:
nil
,
json_column
s
: :list
json_column: :list
},
factory_name: :aqi_forecast
,
factory:
{
...
...
lib/weather-model/source_schemas/auto_station.rb
View file @
acdb1e0f
...
...
@@ -23,9 +23,6 @@ module WeatherModel
options:
{
index:
%i[datetime name]
,
expire:
4
*
60
*
60
,
unique:
nil
,
father:
nil
,
son:
nil
,
},
factory_name: :auto_station_ten_min
,
factory:
{
...
...
lib/weather-model/source_schemas/forecast.rb
View file @
acdb1e0f
...
...
@@ -20,7 +20,7 @@ module WeatherModel
unique:
nil
,
belongs_to:
nil
,
has_many:
nil
,
json_column:
nil
},
factory_name: :city_forecast
,
factory:
{
...
...
lib/weather-model/storer.rb
View file @
acdb1e0f
...
...
@@ -3,10 +3,8 @@ module WeatherModel
require
'active_record'
require
'ohm'
require
"ohm/expire"
require
'weather-model/json_columns'
class
Mysql
<
::
ActiveRecord
::
Base
extend
JsonColumns
after_save
:update_ohm
def
self
.
use_settings
settings
self
.
table_name
=
settings
[
:table_name
]
...
...
@@ -42,8 +40,21 @@ module WeatherModel
self
.
class
.
const_get
(
'Ohm'
)
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
extend
JsonColumns
include
::
Ohm
::
Expire
def
self
.
use_settings
settings
...
...
@@ -56,6 +67,7 @@ module WeatherModel
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
}
set_options
settings
end
def
self
.
set_options
settings
...
...
@@ -79,7 +91,6 @@ module WeatherModel
super
end
private
def
self
.
has_many
his_son
# input like :Post
# collection :posts, :Post
...
...
@@ -90,6 +101,14 @@ module WeatherModel
# reference :post, :Post
reference
(
his_dad
.
to_s
.
underscore
.
to_sym
,
his_dad
)
if
his_dad
&&
SourceSchema
.
const_defined?
(
his_dad
)
end
def
self
.
json_column
column
attribute
column
,
->
(
value
)
{
value
.
is_a?
(
String
)
?
JSON
.
parse
(
value
.
gsub
(
'=>'
,
':'
))
:
value
}
end
end
...
...
@@ -100,17 +119,6 @@ module WeatherModel
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
end
# Forecast -> :forecasts
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
spec/weather-model_spec.rb
View file @
acdb1e0f
...
...
@@ -62,7 +62,7 @@ RSpec.describe WeatherModel do
end
it
'aqi_forecast'
do
@aqi_forecast
=
Aqi
::
Forecast
.
create
(
@aqi_forecast
=
Aqi
::
Aqi
Forecast
.
create
(
datetime:
Time
.
now
,
prompt:
''
,
list:
[
...
...
@@ -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
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