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.



64
65
66
67
68
69
70
# File 'lib/tree_diff.rb', line 64

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



35
36
37
38
# File 'lib/tree_diff.rb', line 35

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

.condition_for(path) ⇒ Object



40
41
42
43
44
# File 'lib/tree_diff.rb', line 40

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



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

def changed_paths
  changes_as_objects.map(&:path)
end

#changesObject



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

def changes
  iterate_observations(@old_object_as_mold, @current_object)
end

#changes_as_objectsObject



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

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

#changes_at(path) ⇒ Object



88
89
90
# File 'lib/tree_diff.rb', line 88

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

#saw_any_change?Boolean

Returns:

  • (Boolean)


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

def saw_any_change?
  changes.present?
end