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
6cb4249a
Commit
6cb4249a
authored
May 22, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add input_proc to Ohm method my_attribute
parent
90983ca7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
9 deletions
+46
-9
warning.rb
lib/weather-model/source_schemas/warning.rb
+6
-0
storer.rb
lib/weather-model/storer.rb
+38
-7
weather-model_spec.rb
spec/weather-model_spec.rb
+2
-2
No files found.
lib/weather-model/source_schemas/warning.rb
0 → 100644
View file @
6cb4249a
module
WeatherModel
module
SourceSchema
end
end
\ No newline at end of file
lib/weather-model/storer.rb
View file @
6cb4249a
...
...
@@ -2,7 +2,8 @@ module WeatherModel
module
Storer
require
'active_record'
require
'ohm'
require
"ohm/expire"
require
'ohm/expire'
require
'rails'
class
Mysql
<
::
ActiveRecord
::
Base
after_save
:update_ohm
...
...
@@ -64,9 +65,9 @@ module WeatherModel
index
:mysql_id
unique
:mysql_id
settings
[
:schema
].
each_pair
{
|
attr
,
type
|
attribute
attr
,
type_proc
(
type
)
}
settings
[
:schema
].
each_pair
do
|
attr
,
type
|
my_attribute
attr
,
type
end
set_options
settings
end
...
...
@@ -84,6 +85,13 @@ module WeatherModel
super
end
# if method_defined? :datatime=
# def datatime= val
# val = val.strftime("%Y-%m-%d %H:%M:%S") if val.is_a?(Time)
# super val
# end
# end
# # Write the dictionary of key-value pairs to the model.
def
method_missing
(
method
,
*
args
)
if
method
.
to_s
.
end_with?
'='
...
...
@@ -93,20 +101,43 @@ module WeatherModel
end
end
def
self
.
type_proc
type
def
self
.
my_attribute
name
,
type
attributes
<<
name
unless
attributes
.
include?
(
name
)
output_proc
=
output_proc_sellector
(
type
)
input_proc
=
input_proc_sellector
(
type
)
define_method
(
name
)
do
output_proc
[
@attributes
[
name
]]
end
define_method
(
:"
#{
name
}
="
)
do
|
value
|
@attributes
[
name
]
=
input_proc
[
value
]
end
end
def
self
.
output_proc_sellector
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
}
else
proc
{
|
val
|
val
}
end
end
def
self
.
input_proc_sellector
type
case
type
when
:datetime
proc
{
|
val
|
val
.
is_a?
(
Time
)
?
(
val
.
getgm
+
8
.
hours
).
strftime
(
"%Y-%m-%d %H:%M:%S"
)
:
val
}
else
proc
{
|
val
|
val
}
end
end
...
...
spec/weather-model_spec.rb
View file @
6cb4249a
...
...
@@ -94,12 +94,12 @@ RSpec.describe WeatherModel do
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
.
time_point
.
class
).
to
eq
(
String
)
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
[
:time_point
].
class
).
to
eq
(
String
)
expect
(
hash
[
:aqi
].
class
).
to
eq
(
Integer
)
expect
(
hash
[
:co
].
class
).
to
eq
(
Float
)
expect
(
hash
[
:area
].
class
).
to
eq
(
String
)
...
...
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