Commit 1b5bf547 by ivan Lan

Add Ohm # to_h

parent ff582435
......@@ -64,8 +64,8 @@ module WeatherModel
index :mysql_id
unique :mysql_id
settings[:schema].keys.each { |attr|
attribute attr
settings[:schema].each_pair { |attr, type|
attribute attr, type_proc(type)
}
set_options settings
end
......@@ -79,6 +79,28 @@ module WeatherModel
end
end
def self.all
super.select{ |x| x.attributes.empty?}.map(&:delete)
super
end
def self.type_proc 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
}
end
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def update attributes
......@@ -86,12 +108,14 @@ module WeatherModel
obj.update_ttl self.class.instance_variable_get(:@expire)
end
def self.all
super.select{ |x| x.attributes.empty?}.map(&:delete)
super
def to_h
keys = attributes.keys - [:mysql_id, :updated_at]
Hash[keys.zip keys.map { |column_name| send(column_name) }]
end
private
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)
......@@ -103,7 +127,7 @@ module WeatherModel
end
def self.json_column column
attribute column, ->(value) {
attribute column, proc { |value|
value.is_a?(String) ?
JSON.parse(value.gsub('=>', ':')) :
value
......
......@@ -40,27 +40,29 @@ RSpec.describe WeatherModel do
it 'Forecast' do
@expire = 1
Forecast::CityForecast::Ohm.expire @expire
mysql_obj = Forecast::CityForecast.create({
mysql = Forecast::CityForecast
ohm = ohm
ohm.expire @expire
mysql_obj = mysql.create({
datetime: '2017-05-16',
cityname: 'shanghai',
temp_high: '100',
temp_low: '0',
weather_text1: '酷热',
weather_text2: '严寒',
weather_pic: 'http://xxx.com',
weather_text1: '严寒',
weather_pic: '酷热转严寒',
win_dir: '东南西北风',
win_speed: '1级',
})
expect(Forecast::CityForecast.count).to eq(1)
expect(Forecast::CityForecast::Ohm.all.count).to eq(1)
expect(Forecast::CityForecast::Ohm.all.first.temp_low).to eq(mysql_obj.temp_low.to_s)
expect(Forecast::CityForecast::Ohm.all.first.get_ttl).to eq(@expire)
expect(mysql.count).to eq(1)
expect(ohm.all.count).to eq(1)
expect(ohm.all.first.temp_low).to eq(mysql_obj.temp_low.to_s)
expect(ohm.all.first.get_ttl).to eq(@expire)
# 'ohm 对象 同步 mysql对象更新'
mysql_obj.update(temp_low: 99)
expect(Forecast::CityForecast::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新'
expect(ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新'
sleep 1.5
expect(Forecast::CityForecast::Ohm.all.count).to eq(0), 'TTL过期对象删除成功'
expect(ohm.all.count).to eq(0), 'TTL过期对象删除成功'
end
it 'aqi_forecast' do
......@@ -85,13 +87,36 @@ RSpec.describe WeatherModel do
expect(Aqi::AqiForecast::Ohm.all.first.list.class).to eq(Array), 'Ohm json_column 正常'
end
describe 'test Ohm # to_h' do
before do
@aqi_live = create(:aqi_live)
end
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.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[:aqi].class).to eq(Integer)
expect(hash[:co].class).to eq(Float)
expect(hash[:area].class).to eq(String)
end
end
it 'Test all source data' do
[
WeatherModel::Forecast::CityForecast,
WeatherModel::AutoStation::AutoStationTenMins,
WeatherModel::Aqi::AqiForecast,
WeatherModel::Aqi::AqiLive,
].each { |source| source.all; source.const_get('Ohm').all }
].each { |source|
# create factory
source.all; source.const_get('Ohm').all.first;
}
end
after do
......
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