Sha256: 327f56c6f772b13386b1ef959e420c5039c0f3624adf9a8e08744c95db5c2d0c
Contents?: true
Size: 1.36 KB
Versions: 1
Compression:
Stored size: 1.36 KB
Contents
require 'store_field/version' require 'store_field/railtie' require 'active_record' module StoreField extend ActiveSupport::Concern module ClassMethods def store_field(key, options = {}) raise ArgumentError.new(':in is invalid') if options[:in] and serialized_attributes[options[:in].to_s].nil? raise ArgumentError.new(':type is invalid') if options[:type] and ![ Hash, Set ].include?(options[:type]) store_attribute = options[:in] || serialized_attributes.keys.first raise ArgumentError.new('store method must be defined before store_field') if store_attribute.nil? # Accessor define_method(key) do value = send("#{store_attribute}")[key] if value.nil? value = store_field_init(options[:type]) send("#{store_attribute}")[key] = value end value end # Utility methods for Set if options[:type] == Set define_method("set_#{key}") {|value| send(key).add(value); self } define_method("unset_#{key}") {|value| send(key).delete(value); self } define_method("set_#{key}?") {|value| send(key).include?(value) } define_method("unset_#{key}?") {|value| !send(key).include?(value) } end end end def store_field_init(klass) return {} unless klass klass.new end end ActiveRecord::Base.send(:include, StoreField)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
store_field-0.0.1 | lib/store_field.rb |