Commit 1b5bf547 by ivan Lan

Add Ohm # to_h

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