lib/simple_model/attributes.rb in simple_model-1.3.0 vs lib/simple_model/attributes.rb in simple_model-1.4.0
- old
+ new
@@ -17,12 +17,12 @@
:has_float => {:cast_to => :to_f, :alias => :has_floats},
:has_int => {:cast_to => :to_i, :alias => :has_ints},
:has_time => {:cast_to => :to_time, :alias => :has_times}
}.freeze
- def initialize(*attrs)
- attrs = attrs.extract_options!
+ def initialize(attrs={})
+ attrs ||= {}
attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize
set(attrs)
defaults = default_attributes_for_init
set(defaults)
self.class.after_initialize.call(self) if self.class.after_initialize
@@ -67,18 +67,26 @@
attr_key = to_attr_key(attr)
if allow_attribute_action?(val,opts)
ab = opts[:allow_blank]
val = fetch_default_value(opts[:default]) unless skip_set_default?(attr,opts,val,ab)
unless (opts[:boolean] ? (!ab && val.blank? && (val != false)) : (!ab && val.blank?))
- val = opts[:on_set].call(self,val) if opts.has_key?(:on_set)
+ val = process_on_set(opts[:on_set],val) if opts.has_key?(:on_set)
send("#{attr}_will_change!") if initialized?(attr) && val != attributes[attr_key]
attributes[attr_key] = val
end
val
end
end
+ def process_on_set(on_set,val)
+ if on_set.is_a?(Symbol)
+ val.__send__(on_set)
+ else
+ on_set.call(self,val)
+ end
+ end
+
# TODO: Returning just val in Rails 3 HashWithIndifferentAccess causes weird issue where if value is an array the array is reset,
# revert to return to just val when Rails 3 support is dropped to improve performance
def get_attribute(attr,opts=nil)
opts ||= fetch_attribute_options(attr)
attr_key = to_attr_key(attr)
@@ -224,12 +232,12 @@
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
define_method(method) do |*attributes|
options = attributes.extract_options!
options = method_options[:options].merge(options) if method_options[:options]
options = default_attribute_settings.merge(options)
- options[:on_set] = lambda {|obj,val| val.send(method_options[:cast_to]) } if method_options[:cast_to]
- create_attribute_methods(attributes,options)
+ options[:on_set] = method_options[:cast_to] if method_options[:cast_to]
+ create_attribute_methods(attributes,options.freeze)
end
module_eval("alias #{method_options[:alias]} #{method}") if method_options[:alias]
end
# Creates a new instance where the attributes store is set to object
@@ -393,39 +401,23 @@
raise TypeError "after_initalize must be a Proc" unless after_initialize.is_a?(Proc)
@after_initialize = after_initialize
end
# Must inherit super's defined_attributes and alias_attributes
- # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
- # hack to keep things working when a class inherits from a super that
- # has ActiveModel::Dirty included
def inherited(base)
base.defined_attributes = defined_attributes.merge(base.defined_attributes)
base.alias_attributes = alias_attributes.merge(base.alias_attributes)
super
- # Rails 3.0 Hack
- if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR < 1)
- base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
- base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
- end
end
end # end ClassMethods
- # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
- # hack to keep things working when a class includes a module that has
# ActiveModel::Dirty included
def self.included(base)
base.extend(Attributes::ClassMethods)
base.send(:include, ActiveModel::Validations)
base.send(:include, ActiveModel::Conversion)
base.extend ActiveModel::Naming
base.extend ActiveModel::Callbacks
base.send(:include, ActiveModel::Validations::Callbacks)
-
- # Rails 3.0 Hack
- if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR < 1)
- base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
- base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
- end
end
end
end