module LooseAttr::ActiveRecordExt
Public Class Methods
included(model)
click to toggle source
# File lib/loose_attr/active_record_ext.rb, line 3 def self.included(model) model.class_eval do before_save :set_ext_field after_find :set_hashed_ext end model.extend ClassMethods end
Private Instance Methods
cast(value, cast_type, option)
click to toggle source
# File lib/loose_attr/active_record_ext.rb, line 49 def cast(value, cast_type, option) return nil if value.blank? case cast_type when :string value.to_s when :integer value.to_i when :boolean value.to_b when :date DateTime.strptime(value, option[:format]).in_time_zone end end
hashed_ext()
click to toggle source
# File lib/loose_attr/active_record_ext.rb, line 36 def hashed_ext @hashed_ext ||= ::Hashie::Mash.new end
set_ext_field()
click to toggle source
# File lib/loose_attr/active_record_ext.rb, line 40 def set_ext_field self.send("#{self.class.loose_attr_column_name}=", hashed_ext.to_json) if hashed_ext.present? end
set_hashed_ext()
click to toggle source
# File lib/loose_attr/active_record_ext.rb, line 44 def set_hashed_ext ext_field = read_attribute(self.class.loose_attr_column_name) @hashed_ext = ::Hashie::Mash.new(JSON.parse(ext_field)) if ext_field.present? end