lib/matic.rb in matic-0.1.0 vs lib/matic.rb in matic-0.1.1

- old
+ new

@@ -8,64 +8,62 @@ def collection_name self.name.tableize end def field(attr_name) - # Plagiarized from ActiveModel - attribute_method_matchers.each do |matcher| - unless instance_method_already_implemented?(matcher.method_name(attr_name)) - generate_method = "define_method_#{matcher.prefix}attribute#{matcher.suffix}" + generate_attribute_methods(attr_name) - if respond_to?(generate_method) - send(generate_method, attr_name) - else - method_name = matcher.method_name(attr_name) - - generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 - if method_defined?(:#{method_name}) - undef :#{method_name} - end - def #{method_name}(*args) - send(:#{matcher.method_missing_target}, '#{attr_name}', *args) - end - STR - end - end - end - define_method(attr_name) do self[attr_name.to_s] end define_method("#{attr_name}=") do |val| - eval("#{attr_name}_will_change!") unless val == self[attr_name.to_s] + unless val == self[attr_name.to_s] + eval("#{attr_name}_will_change!") + end + self[attr_name.to_s] = val end end + + private + + def generate_attribute_methods(attr_name) + attribute_method_matchers.each do |matcher| + method_name = matcher.method_name(attr_name) + + generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1 + def #{method_name}(*args) + send(:#{matcher.method_missing_target}, '#{attr_name}', *args) + end + STR + end + end end def insert(opts={}) - if super - @previously_changed = changes - @changed_attributes.clear - end + clear_changes if super end def insert!(opts={}) insert(opts.merge(:safe => true)) end def update(opts={}, update_doc=@doc) - if super - @previously_changed = changes - @changed_attributes.clear - end + clear_changes if super end def update!(opts={}, update_doc=@doc) update(opts.merge(:safe => true), update_doc) end def save is_new ? insert : update + end + + private + + def clear_changes + @previously_changed = changes + @changed_attributes.clear end end