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 ...@@ -18,8 +18,8 @@ module WeatherModel
index: [:cityname, :date], index: [:cityname, :date],
expire: 11 * 24 * 60 * 60, expire: 11 * 24 * 60 * 60,
unique: nil, unique: nil,
father: nil, belongs_to: nil,
son: nil, has_many: nil,
}, },
factory_name: :city_forecast, factory_name: :city_forecast,
......
...@@ -3,15 +3,27 @@ module WeatherModel ...@@ -3,15 +3,27 @@ module WeatherModel
require 'active_record' require 'active_record'
require 'ohm' require 'ohm'
require "ohm/expire" require "ohm/expire"
require 'weather-model/json_columns'
class Mysql < ::ActiveRecord::Base class Mysql < ::ActiveRecord::Base
extend JsonColumns
after_save :update_ohm after_save :update_ohm
def self.use_settings settings def self.use_settings settings
self.table_name = settings[:table_name] self.table_name = settings[:table_name]
set_options settings
define_ohm settings define_ohm settings
define_factory settings define_factory settings
end 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 def self.define_ohm settings
const_set('Ohm', Class.new(SourceOhm)).use_settings(settings) const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
...@@ -30,45 +42,51 @@ module WeatherModel ...@@ -30,45 +42,51 @@ module WeatherModel
self.class.const_get('Ohm') self.class.const_get('Ohm')
end end
class SourceOhm < ::Ohm::Model class SourceOhm < ::Ohm::Model
include ::Ohm::Expire extend JsonColumns
def self.use_settings settings include ::Ohm::Expire
attribute :mysql_id def self.use_settings settings
attribute :updated_at
index :mysql_id attribute :mysql_id
unique :mysql_id attribute :updated_at
index :mysql_id
settings[:schema].keys.each { |attr| unique :mysql_id
attribute attr
} settings[:schema].keys.each { |attr|
[:index, :unique, :expire, :son, :dad].each do |option| attribute attr
attrs = settings[:options][option] }
attrs.is_a?(Array) ? end
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 def self.set_options settings
super.select{ |x| x.attributes.empty?}.map(&:delete) [:index, :unique, :expire, :has_many, :belongs_to, :json_column].each do |option|
super 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
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 private
def self.son 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)
end end
def self.dad his_dad # input like :Post def self.belongs_to 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_dad) reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad)
end end
...@@ -81,7 +99,15 @@ module WeatherModel ...@@ -81,7 +99,15 @@ module WeatherModel
# find 找不到 会自动生成 # find 找不到 会自动生成
ohm_obj = get_ohm.find(mysql_id: id).first.try(:update, ohm_attrs) || get_ohm.create(ohm_attrs) 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 # 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 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