module Property::DirtyProperties

This module implements ActiveRecord::Dirty functionalities for the properties hash.

Constants

CHANGED_REGEXP
WAS_REGEXP

Public Instance Methods

[]=(key, value) click to toggle source
Calls superclass method
# File lib/property/dirty.rb, line 40
def []=(key, value)
  @original_hash ||= self.dup
  super
end
changed() click to toggle source
# File lib/property/dirty.rb, line 54
def changed
  changes.keys
end
changed?() click to toggle source
# File lib/property/dirty.rb, line 50
def changed?
  !changes.empty?
end
changes() click to toggle source
# File lib/property/dirty.rb, line 58
def changes
  return {} unless @original_hash
  changes = {}

  # look for updated value
  each do |key, new_value|
    if new_value != (old_value = @original_hash[key])
      changes[key] = [old_value, new_value]
    end
  end

  # look for deleted value
  (@original_hash.keys - keys).each do |key|
    changes[key] = [@original_hash[key], nil]
  end

  changes
end
clear_changes!() click to toggle source

This method should be called to reset dirty information before dump

# File lib/property/dirty.rb, line 78
def clear_changes!
  remove_instance_variable(:@original_hash) if defined?(@original_hash)
end
delete(key) click to toggle source
Calls superclass method
# File lib/property/dirty.rb, line 45
def delete(key)
  @original_hash ||= self.dup
  super
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/property/dirty.rb, line 82
def method_missing(method, *args)
  if method.to_s =~ CHANGED_REGEXP
    !changes[$1].nil?
  elsif method.to_s =~ WAS_REGEXP
    (@original_hash || self)[$1]
  else
    super
  end
end