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
a8e20da9
Commit
a8e20da9
authored
May 15, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update ohm obj after a mysql obj been saved
parent
c1e38114
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
78 additions
and
22 deletions
+78
-22
1_create_forecasts.rb
db/migrate/1_create_forecasts.rb
+7
-5
redic.rb
lib/weather-model/redic.rb
+10
-0
source_schema.rb
lib/weather-model/source_schema.rb
+14
-1
storer.rb
lib/weather-model/storer.rb
+21
-13
weather-model_spec.rb
spec/weather-model_spec.rb
+26
-3
weather-model-test.sqlite3
weather-model-test.sqlite3
+0
-0
No files found.
db/migrate/1_create_forecasts.rb
View file @
a8e20da9
class
CreateForecasts
<
ActiveRecord
::
Migration
[
5.0
]
def
change
def
up
create_table
:forecasts
do
|
t
|
t
.
datetime
:datetime
t
.
string
:speed
t
.
integer
:temp_low
t
.
integer
:temp_high
t
.
string
:weathertext
t
.
string
:weatherpic
t
.
string
:weather
_
text
t
.
string
:weather
_
pic
t
.
string
:cityname
t
.
string
:serialnumber
t
.
string
:win_dir
t
.
string
:win_speed
...
...
@@ -19,4 +17,8 @@ class CreateForecasts < ActiveRecord::Migration[5.0]
add_index
:forecasts
,
:cityname
add_index
:forecasts
,
:datetime
end
def
down
drop_table
:forecasts
end
end
lib/weather-model/redic.rb
0 → 100644
View file @
a8e20da9
class
Redic
def
expire
(
key
,
ttl
)
self
.
call
(
"EXPIRE"
,
key
,
ttl
)
end
def
ttl
(
key
)
self
.
call
(
"TTL"
,
key
)
end
end
\ No newline at end of file
lib/weather-model/source_schema.rb
View file @
a8e20da9
...
...
@@ -20,8 +20,21 @@ module WeatherModel
},
options:
{
index:
[
:cityname
],
expire:
5
,
unique: :mysql_id
,
father:
nil
,
son:
nil
son:
nil
,
},
factory:
{
datetime:
'2017-05-16'
,
cityname:
'shanghai'
,
temp_high:
'100'
,
temp_low:
'0'
,
weather_text:
'酷热严寒'
,
weather_pic:
'http://xxx.com'
,
win_dir:
'东南西北风'
,
win_speed:
'1级'
,
}
}
end
...
...
lib/weather-model/storer.rb
View file @
a8e20da9
...
...
@@ -12,33 +12,41 @@ module WeatherModel
end
class
Ohm
<
::
Ohm
::
Model
def
self
.
use_settings
settings
include
::
Ohm
::
Expire
# TTL = 5
# attribute :mysqlid
settings
.
keys
.
each
{
|
x
|
attribute
x
def
self
.
use_settings
settings
attribute
:mysql_id
attribute
:updated_at
index
:mysql_id
settings
[
:schema
].
keys
.
each
{
|
attr
|
attribute
attr
}
[
:index
,
:unique
,
:expire
].
each
do
|
x
|
method
(
x
).
call
settings
[
x
]
[
:index
,
:unique
,
:expire
,
:son
,
:dad
].
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
# expire TTL
end
private
def
son
his_son
# input like :Post
def
s
elf
.
s
on
his_son
# input like :Post
# collection :posts, :Post
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
SourceSchema
.
const_defined?
(
his_son
)
collection
(
his_son
.
to_s
.
underscore
.
pluralize
.
to_sym
,
his_son
)
if
his_son
&&
SourceSchema
.
const_defined?
(
his_son
)
end
def
dad
his_dad
# input like :Post
def
self
.
dad
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_son
)
reference
(
his_dad
.
to_s
.
underscore
.
to_sym
,
his_dad
)
if
his_dad
&&
SourceSchema
.
const_defined?
(
his_dad
)
end
end
private
def
update_ohm
# Ohm.create()
ohm_attrs
=
self
.
attributes
.
except
(
'created_at'
,
'id'
).
merge
(
mysql_id:
id
)
# find 找不到 会自动生成
ohm_obj
=
Ohm
.
find
(
mysql_id:
id
).
first
.
update
(
ohm_attrs
)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
end
end
...
...
spec/weather-model_spec.rb
View file @
a8e20da9
...
...
@@ -4,7 +4,7 @@ require "ohm"
RSpec
.
describe
WeatherModel
do
include
WeatherModel
before
do
# start an ohm
Ohm
.
redis
=
Redic
.
new
(
"redis://127.0.0.1:6379"
)
# run migration
...
...
@@ -14,7 +14,8 @@ RSpec.describe WeatherModel do
:database
=>
'weather-model-test.sqlite3'
)
ActiveRecord
::
Migration
[
5.0
].
subclasses
.
each
do
|
migrate
|
migrate
.
migrate
rescue
nil
migrate
.
migrate
(
:up
)
end
end
it
"has a version number"
do
...
...
@@ -22,7 +23,29 @@ RSpec.describe WeatherModel do
end
it
'Forecast'
do
p
Forecast
::
ShanghaiTen
.
all
Forecast
::
ShanghaiTen
.
create
({
datetime:
'2017-05-16'
,
cityname:
'shanghai'
,
temp_high:
'100'
,
temp_low:
'0'
,
weather_text:
'酷热严寒'
,
weather_pic:
'http://xxx.com'
,
win_dir:
'东南西北风'
,
win_speed:
'1级'
,
})
expect
(
Forecast
::
ShanghaiTen
.
count
).
to
eq
(
1
)
expect
(
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
count
).
to
eq
(
1
)
# p 'p Forecast::ShanghaiTen::Ohm.all.first.attributes'
# ttl 失败
p
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
to_a
p
Forecast
::
ShanghaiTen
::
Ohm
.
all
.
to_a
.
map
(
&
:get_ttl
)
end
after
do
ActiveRecord
::
Migration
[
5.0
].
subclasses
.
each
do
|
migrate
|
migrate
.
migrate
(
:down
)
end
end
end
weather-model-test.sqlite3
View file @
a8e20da9
No preview for this file type
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