Class: TreeDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_diff.rb

Defined Under Namespace

Classes: Change

Constant Summary collapse

Mold =
Class.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_object) ⇒ TreeDiff

Returns a new instance of TreeDiff.



59
60
61
62
63
64
65
# File 'lib/tree_diff.rb', line 59

def initialize(original_object)
  check_observations

  self.class.class_variable_set(:@@conditions, [])
  @old_object_as_mold = create_mold Mold.new, original_object, self.class.original_observations
  @current_object = original_object
end

Class Method Details

.condition(path, &condition) ⇒ Object



48
49
50
51
# File 'lib/tree_diff.rb', line 48

def self.condition(path, &condition)
  class_variable_set(:@@conditions, []) unless conditions
  conditions << [path, condition]
end

.condition_for(path) ⇒ Object



53
54
55
56
57
# File 'lib/tree_diff.rb', line 53

def self.condition_for(path)
  return unless (condition = conditions.detect { |c| c.first == path })

  condition.last # A stored block (proc)
end

.conditionsObject



24
25
26
27
# File 'lib/tree_diff.rb', line 24

def self.conditions
  class_variable_set(:@@conditions, nil) unless class_variable_defined?(:@@conditions)
  class_variable_get(:@@conditions)
end

.observationsObject



19
20
21
22
# File 'lib/tree_diff.rb', line 19

def self.observations
  class_variable_set(:@@observations, nil) unless class_variable_defined?(:@@observations)
  class_variable_get(:@@observations)
end

.observe(*defs) ⇒ Object



29
30
31
32
33
# File 'lib/tree_diff.rb', line 29

def self.observe(*defs)
  class_variable_set(:@@original_observations, defs)
  class_variable_set(:@@observations, [])
  observe_chain [], defs
end

.original_observationsObject



14
15
16
17
# File 'lib/tree_diff.rb', line 14

def self.original_observations
  class_variable_set(:@@original_observations, nil) unless class_variable_defined?(:@@original_observations)
  class_variable_get(:@@original_observations)
end

Instance Method Details

#changed_pathsObject



79
80
81
# File 'lib/tree_diff.rb', line 79

def changed_paths
  changes_as_objects.map(&:path)
end

#changesObject



71
72
73
# File 'lib/tree_diff.rb', line 71

def changes
  iterate_observations(@old_object_as_mold, @current_object)
end

#changes_as_objectsObject



75
76
77
# File 'lib/tree_diff.rb', line 75

def changes_as_objects
  changes.map { |c| Change.new(c.fetch(:path), c.fetch(:old), c.fetch(:new)) }
end

#changes_at(path) ⇒ Object



83
84
85
# File 'lib/tree_diff.rb', line 83

def changes_at(path)
  changes_as_objects.detect { |c| c.path == path }
end

#saw_any_change?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tree_diff.rb', line 67

def saw_any_change?
  changes.present?
end