Module: Diffable::InstanceMethods

Defined in:
lib/diffable.rb

Instance Method Summary collapse

Instance Method Details

#diff(other) ⇒ Object

Produces a Hash containing the differences between the calling object and the object passed in as a parameter



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/diffable.rb', line 22

def diff(other)
  check_class_compatibility(self, other)
  
  self_attribs = self.get_attributes(self.class.excluded_fields)
  other_attribs = other.get_attributes(other.class.excluded_fields)
  
  change = compare_objects(self_attribs, other_attribs, self, other)
  
  #the last bit - no change, no report; simples
  if other.class.conditional_fields
    other.class.conditional_fields.each do |key|
      change[key.to_sym] = eval("other.#{key}") unless change.empty?
    end
  end
  change
end

#get_attributes(excluded) ⇒ Object

Fetches the attributes of the calling object, exluding the id field and any fields specified passed as an array of symbols via the excluded parameter



43
44
45
46
47
# File 'lib/diffable.rb', line 43

def get_attributes(excluded)
  attribs = attributes.dup
  attribs.delete_if { |key, value|
    (!excluded.nil? and excluded.include?(key)) or key == "id" }
end

#reflected_names(obj) ⇒ Object

Uses reflection to fetch the eligible associated objects for the current object, excluding parent objects and child objects that do not include the Diffable mixin



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/diffable.rb', line 53

def reflected_names(obj)
  classes = obj.reflections
  class_names = []
  classes.each do |key, cl|
    if eval(cl.class_name).respond_to?("diffable") \
       and cl.association_class != ActiveRecord::Associations::BelongsToAssociation
      class_names << key
    end
  end
  class_names
end