Commit afebacf0 by ivan Lan

Add JsonColumns & Add reference functions to Mysql

parent b6a7f1dc
module JsonColumns
def json_columns *columns
columns.each do |column|
define_method(column) {
value = self.read_attribute(column)
value && JSON.parse(value.gsub('=>', ':'))
}
define_method("#{column}=") { |value|
self.[]=( column, JSON.dump(value) )
}
end
end
end
\ No newline at end of file
......@@ -18,8 +18,8 @@ module WeatherModel
index: [:cityname, :date],
expire: 11 * 24 * 60 * 60,
unique: nil,
father: nil,
son: nil,
belongs_to: nil,
has_many: nil,
},
factory_name: :city_forecast,
......
......@@ -3,15 +3,27 @@ module WeatherModel
require 'active_record'
require 'ohm'
require "ohm/expire"
require 'weather-model/json_columns'
class Mysql < ::ActiveRecord::Base
extend JsonColumns
after_save :update_ohm
def self.use_settings settings
self.table_name = settings[:table_name]
set_options settings
define_ohm settings
define_factory settings
end
def self.set_options settings
[:has_many, :belongs_to, :json_column].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
end
def self.define_ohm settings
const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
......@@ -30,45 +42,51 @@ module WeatherModel
self.class.const_get('Ohm')
end
class SourceOhm < ::Ohm::Model
include ::Ohm::Expire
def self.use_settings settings
attribute :mysql_id
attribute :updated_at
index :mysql_id
unique :mysql_id
settings[:schema].keys.each { |attr|
attribute attr
}
[: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
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def update attributes
obj = super(attributes)
obj.update_ttl self.class.instance_variable_get(:@expire)
end
class SourceOhm < ::Ohm::Model
extend JsonColumns
include ::Ohm::Expire
def self.use_settings settings
attribute :mysql_id
attribute :updated_at
index :mysql_id
unique :mysql_id
settings[:schema].keys.each { |attr|
attribute attr
}
end
def self.all
super.select{ |x| x.attributes.empty?}.map(&:delete)
super
def self.set_options settings
[:index, :unique, :expire, :has_many, :belongs_to, :json_column].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
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def update attributes
obj = super(attributes)
obj.update_ttl self.class.instance_variable_get(:@expire)
end
def self.all
super.select{ |x| x.attributes.empty?}.map(&:delete)
super
end
private
def self.son his_son # input like :Post
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)
end
def self.dad his_dad # input like :Post
def self.belongs_to 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_dad)
end
......@@ -81,7 +99,15 @@ module WeatherModel
# find 找不到 会自动生成
ohm_obj = get_ohm.find(mysql_id: id).first.try(:update, ohm_attrs) || get_ohm.create(ohm_attrs)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique
end
end
# Forecast -> :forecasts
def has_many name
super name.to_s.underscore.pluralize.to_sym
end
# Forecast -> :forecast
def belongs_to name
super name.to_s.underscore.to_sym
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