Sha256: 2bc921668d27e9503dc493dbc3e195d427e7007c3862eda1b198eaf7b39ce679
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 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]) klass = 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 = klass ? klass.new : {} 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 end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
store_field-1.0.0 | lib/store_field.rb |