module Virtus::DirtyAttribute::ClassMethods

Public Instance Methods

attribute(name, type, options = {}) click to toggle source
Calls superclass method
# File lib/virtus/dirty_attribute.rb, line 24
def attribute(name, type, options = {})
  _create_writer_with_dirty_tracking(name)
  super
end

Private Instance Methods

_create_writer_with_dirty_tracking(name) click to toggle source
# File lib/virtus/dirty_attribute.rb, line 30
      def _create_writer_with_dirty_tracking(name)
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{name}=(new_regular_object)
            prev_object = instance_variable_get(:@#{name})
            new_virtus_object = super

            if attribute_dirty?(:#{name}) && original_attributes[:#{name}] == new_virtus_object
              attribute_clean!(:#{name})
            elsif prev_object != new_virtus_object
              unless original_attributes.key?(:#{name})
                original_attributes[:#{name}] = prev_object
              end

              attribute_dirty!(:#{name}, new_regular_object)
            end

            new_virtus_object
          end
        RUBY
      end