Commit a8e20da9 by ivan Lan

Update ohm obj after a mysql obj been saved

parent c1e38114
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
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
},
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
end
......
......@@ -12,33 +12,41 @@ module WeatherModel
end
class Ohm < ::Ohm::Model
include ::Ohm::Expire
def self.use_settings settings
include ::Ohm::Expire
# TTL = 5
# attribute :mysqlid
settings.keys.each { |x|
attribute x
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 self.son 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
......
......@@ -4,17 +4,18 @@ require "ohm"
RSpec.describe WeatherModel do
include WeatherModel
# start an ohm
Ohm.redis = Redic.new("redis://127.0.0.1:6379")
# run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'weather-model-test.sqlite3'
)
ActiveRecord::Migration[5.0].subclasses.each do |migrate|
migrate.migrate rescue nil
before do
# start an ohm
Ohm.redis = Redic.new("redis://127.0.0.1:6379")
# run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'weather-model-test.sqlite3'
)
ActiveRecord::Migration[5.0].subclasses.each do |migrate|
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
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