module SparkApi::Models::Dirty

Public Instance Methods

changed() click to toggle source
# File lib/spark_api/models/dirty.rb, line 9
def changed
  changed_attributes.keys
end
changed?() click to toggle source
# File lib/spark_api/models/dirty.rb, line 5
def changed?
  changed.any?
end
changed_attributes() click to toggle source

hash with changed attributes and their original values

# File lib/spark_api/models/dirty.rb, line 22
def changed_attributes
  @changed_attributes ||= {}
end
changes() click to toggle source
# File lib/spark_api/models/dirty.rb, line 13
def changes
  Hash[changed.map { |attr| [attr, attribute_change(attr)] }]
end
dirty_attributes() click to toggle source

hash with changed attributes and their new values

# File lib/spark_api/models/dirty.rb, line 27
def dirty_attributes
  changed.inject({}) { |h, k| h[k] = attributes[k.to_s]; h }
end
previous_changes() click to toggle source
# File lib/spark_api/models/dirty.rb, line 17
def previous_changes
  @previously_changed
end

Private Instance Methods

attribute_change(attr) click to toggle source
# File lib/spark_api/models/dirty.rb, line 42
def attribute_change(attr)
  [changed_attributes[attr], @attributes[attr.to_s]] if attribute_changed?(attr)
end
attribute_changed?(attr) click to toggle source
# File lib/spark_api/models/dirty.rb, line 38
def attribute_changed?(attr)
  changed.include?(attr)
end
attribute_will_change!(attr) click to toggle source
# File lib/spark_api/models/dirty.rb, line 46
def attribute_will_change!(attr)
  attr = attr.to_s
  begin
    value = @attributes[attr]
    value = value.duplicable? ? value.clone : value
  rescue TypeError, NoMethodError; end

  changed_attributes[attr] = value unless changed.include?(attr)
end
reset_dirty() click to toggle source
# File lib/spark_api/models/dirty.rb, line 33
def reset_dirty
  @previously_changed = changed_attributes
  @changed_attributes.clear
end