module NoBrainer::Document::Dirty

Public Instance Methods

_create(*args) click to toggle source

1) We should save the changes as seen through read_attribute, because the user sees attributes through the read_attribute getters, but it’s near impossible because we would need to wrap the user defined getters, so we’ll go through _read_attribute. 2) We want to detect changes based on @_attributes to track things like undefined -> nil. Going through the getters will not give us that.

Calls superclass method
# File lib/no_brainer/document/dirty.rb, line 10
def _create(*args)
  super.tap { clear_dirtiness }
end
_read_attribute(name) click to toggle source
Calls superclass method
# File lib/no_brainer/document/dirty.rb, line 69
def _read_attribute(name)
  super.tap do |value|
    # This take care of string/arrays/hashes that could change without going
    # through the setter.
    attribute_may_change(name, value) if value.respond_to?(:size)
  end
end
_update(*args) click to toggle source
Calls superclass method
# File lib/no_brainer/document/dirty.rb, line 14
def _update(*args)
  super.tap { clear_dirtiness }
end
_write_attribute(name, value) click to toggle source
Calls superclass method
# File lib/no_brainer/document/dirty.rb, line 77
def _write_attribute(name, value)
  attribute_may_change(name)
  super
end
attribute_may_change(attr, current_value = None) click to toggle source
# File lib/no_brainer/document/dirty.rb, line 49
def attribute_may_change(attr, current_value = None)
  if current_value == None
    current_value = begin
      assert_access_field(attr)
      _read_attribute(attr)
    rescue NoBrainer::Error::MissingAttribute => e
      e
    end
  end

  unless @_old_attributes.key?(attr)
    @_old_attributes[attr] = current_value.deep_dup
  end
end
attribute_will_change!(*) click to toggle source
# File lib/no_brainer/document/dirty.rb, line 64
def attribute_will_change!(*)
  # Provided for comatibility. See issue #190
  :not_implemented_in_no_brainer_see_issue_190
end
changed() click to toggle source
# File lib/no_brainer/document/dirty.rb, line 33
def changed
  changes.keys
end
changed?() click to toggle source
# File lib/no_brainer/document/dirty.rb, line 29
def changed?
  changes.present?
end
changes() click to toggle source
# File lib/no_brainer/document/dirty.rb, line 37
def changes
  result = {}.with_indifferent_access
  @_old_attributes.each do |attr, old_value|
    current_value = _read_attribute(attr)
    if current_value != old_value || !@_old_attributes_keys.include?(attr)
      result[attr] = [old_value, current_value]
    end
  end
  result
end
clear_dirtiness(options={}) click to toggle source
# File lib/no_brainer/document/dirty.rb, line 18
def clear_dirtiness(options={})
  if options[:keep_ivars] && options[:missing_attributes].try(:[], :pluck)
    attrs = options[:missing_attributes][:pluck].keys
    @_old_attributes = @_old_attributes.reject { |k,v| attrs.include?(k) }
  else
    @_old_attributes = {}.with_indifferent_access
  end

  @_old_attributes_keys = @_attributes.keys # to track undefined -> nil changes
end