Commit a8e20da9 by ivan Lan

Update ohm obj after a mysql obj been saved

parent c1e38114
class CreateForecasts < ActiveRecord::Migration[5.0] class CreateForecasts < ActiveRecord::Migration[5.0]
def change def up
create_table :forecasts do |t| create_table :forecasts do |t|
t.datetime :datetime t.datetime :datetime
t.string :speed
t.integer :temp_low t.integer :temp_low
t.integer :temp_high t.integer :temp_high
t.string :weathertext t.string :weather_text
t.string :weatherpic t.string :weather_pic
t.string :cityname t.string :cityname
t.string :serialnumber
t.string :win_dir t.string :win_dir
t.string :win_speed t.string :win_speed
...@@ -19,4 +17,8 @@ class CreateForecasts < ActiveRecord::Migration[5.0] ...@@ -19,4 +17,8 @@ class CreateForecasts < ActiveRecord::Migration[5.0]
add_index :forecasts, :cityname add_index :forecasts, :cityname
add_index :forecasts, :datetime add_index :forecasts, :datetime
end end
def down
drop_table :forecasts
end
end end
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
...@@ -20,9 +20,22 @@ module WeatherModel ...@@ -20,9 +20,22 @@ module WeatherModel
}, },
options: { options: {
index: [:cityname], index: [:cityname],
expire: 5,
unique: :mysql_id,
father: nil, 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 end
end end
......
...@@ -12,33 +12,41 @@ module WeatherModel ...@@ -12,33 +12,41 @@ module WeatherModel
end end
class Ohm < ::Ohm::Model class Ohm < ::Ohm::Model
include ::Ohm::Expire
def self.use_settings settings def self.use_settings settings
include ::Ohm::Expire
# TTL = 5 attribute :mysql_id
# attribute :mysqlid attribute :updated_at
settings.keys.each { |x| index :mysql_id
attribute x settings[:schema].keys.each { |attr|
attribute attr
} }
[:index, :unique, :expire].each do |x| [:index, :unique, :expire, :son, :dad].each do |option|
method(x).call settings[x] 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
# expire TTL
end end
private private
def son his_son # input like :Post def self.son his_son # input like :Post
# collection :posts, :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 end
def dad his_dad # input like :Post def self.dad his_dad # input like :Post
# reference :post, :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
end end
private private
def update_ohm 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
end end
......
...@@ -4,17 +4,18 @@ require "ohm" ...@@ -4,17 +4,18 @@ require "ohm"
RSpec.describe WeatherModel do RSpec.describe WeatherModel do
include WeatherModel include WeatherModel
before do
# start an ohm # start an ohm
Ohm.redis = Redic.new("redis://127.0.0.1:6379") Ohm.redis = Redic.new("redis://127.0.0.1:6379")
# run migration # run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file } Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
ActiveRecord::Base.establish_connection( ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3', :adapter => 'sqlite3',
:database => 'weather-model-test.sqlite3' :database => 'weather-model-test.sqlite3'
) )
ActiveRecord::Migration[5.0].subclasses.each do |migrate| ActiveRecord::Migration[5.0].subclasses.each do |migrate|
migrate.migrate rescue nil migrate.migrate(:up)
end
end end
it "has a version number" do it "has a version number" do
...@@ -22,7 +23,29 @@ RSpec.describe WeatherModel do ...@@ -22,7 +23,29 @@ RSpec.describe WeatherModel do
end end
it 'Forecast' do 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
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment