module JsonApiClient::Helpers::Dirty

Public Instance Methods

attribute_change(attr) click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 52
def attribute_change(attr)
  [changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
end
attribute_changed?(attr) click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 48
def attribute_changed?(attr)
  changed.include?(attr.to_s)
end
attribute_will_change!(attr) click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 31
def attribute_will_change!(attr)
  return if attribute_changed?(attr)
  set_attribute_was(attr, attributes[attr])
end
changed() click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 9
def changed
  changed_attributes.keys
end
changed?() click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 5
def changed?
  changed_attributes.present?
end
changed_attributes() click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 13
def changed_attributes
  @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end
clear_changes_information() click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 17
def clear_changes_information
  @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
end
forget_change!(attr) click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 21
def forget_change!(attr)
  @changed_attributes.delete(attr.to_s)
end
set_all_attributes_dirty() click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 25
def set_all_attributes_dirty
  attributes.each do |k, v|
    set_attribute_was(k, v)
  end
end
set_attribute_was(attr, value) click to toggle source
# File lib/json_api_client/helpers/dirty.rb, line 36
def set_attribute_was(attr, value)
  begin
    value = value.duplicable? ? value.clone : value
    changed_attributes[attr] = value
  rescue TypeError, NoMethodError
  end
end

Protected Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/json_api_client/helpers/dirty.rb, line 58
def method_missing(method, *args, &block)
  if method.to_s =~ /^(.*)_changed\?$/
    has_attribute?($1) ? attribute_changed?($1) : nil
  elsif method.to_s =~ /^(.*)_was$/
    has_attribute?($1) ? attribute_was($1) : nil
  else
    super
  end
end
set_attribute(name, value) click to toggle source
Calls superclass method
# File lib/json_api_client/helpers/dirty.rb, line 68
def set_attribute(name, value)
  attribute_will_change!(name) if value != attributes[name] || !attributes.has_key?(name)
  super
end