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
afebacf0
Commit
afebacf0
authored
May 20, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add JsonColumns & Add reference functions to Mysql
parent
b6a7f1dc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
5 deletions
+45
-5
json_columns.rb
lib/weather-model/json_columns.rb
+14
-0
forecast.rb
lib/weather-model/source_schemas/forecast.rb
+2
-2
storer.rb
lib/weather-model/storer.rb
+29
-3
No files found.
lib/weather-model/json_columns.rb
0 → 100644
View file @
afebacf0
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/forecast.rb
View file @
afebacf0
...
...
@@ -18,8 +18,8 @@ module WeatherModel
index:
[
:cityname
,
:date
],
expire:
11
*
24
*
60
*
60
,
unique:
nil
,
father
:
nil
,
son
:
nil
,
belongs_to
:
nil
,
has_many
:
nil
,
},
factory_name: :city_forecast
,
...
...
lib/weather-model/storer.rb
View file @
afebacf0
...
...
@@ -3,15 +3,27 @@ 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
]
set_options
settings
define_ohm
settings
define_factory
settings
end
def
self
.
set_options
settings
[
:has_many
,
:belongs_to
,
:json_column
].
each
do
|
option
|
attrs
=
settings
[
:options
][
option
]
attrs
.
is_a?
(
Array
)
?
attrs
.
each
{
|
attr
|
method
(
option
).
call
(
attr
)
if
attr
}
:
method
(
option
).
call
(
attrs
)
if
attrs
end
end
def
self
.
define_ohm
settings
const_set
(
'Ohm'
,
Class
.
new
(
SourceOhm
)).
use_settings
(
settings
)
...
...
@@ -31,7 +43,9 @@ module WeatherModel
end
class
SourceOhm
<
::
Ohm
::
Model
extend
JsonColumns
include
::
Ohm
::
Expire
def
self
.
use_settings
settings
attribute
:mysql_id
...
...
@@ -42,13 +56,17 @@ module WeatherModel
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
}
[
:index
,
:unique
,
:expire
,
:son
,
:dad
].
each
do
|
option
|
end
def
self
.
set_options
settings
[
:index
,
:unique
,
:expire
,
:has_many
,
:belongs_to
,
:json_column
].
each
do
|
option
|
attrs
=
settings
[
:options
][
option
]
attrs
.
is_a?
(
Array
)
?
attrs
.
each
{
|
attr
|
method
(
option
).
call
(
attr
)
if
attr
}
:
method
(
option
).
call
(
attrs
)
if
attrs
end
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def
update
attributes
...
...
@@ -63,12 +81,12 @@ module WeatherModel
private
def
self
.
son
his_son
# input like :Post
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
)
end
def
self
.
dad
his_dad
# input like :Post
def
self
.
belongs_to
his_dad
# input like :Post
# reference :post, :Post
reference
(
his_dad
.
to_s
.
underscore
.
to_sym
,
his_dad
)
if
his_dad
&&
SourceSchema
.
const_defined?
(
his_dad
)
end
...
...
@@ -82,6 +100,14 @@ 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
...
...
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