Class: TreeDiff
- Inherits:
-
Object
- Object
- TreeDiff
- Defined in:
- lib/tree_diff.rb
Defined Under Namespace
Classes: Change
Constant Summary collapse
- Mold =
Class.new
Class Method Summary collapse
- .condition(path, &condition) ⇒ Object
- .condition_for(path) ⇒ Object
- .conditions ⇒ Object
- .observations ⇒ Object
- .observe(*defs) ⇒ Object
- .original_observations ⇒ Object
Instance Method Summary collapse
- #changed_paths ⇒ Object
- #changes ⇒ Object
- #changes_as_objects ⇒ Object
- #changes_at(path) ⇒ Object
-
#initialize(original_object) ⇒ TreeDiff
constructor
A new instance of TreeDiff.
- #saw_any_change? ⇒ Boolean
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 |
.conditions ⇒ Object
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 |
.observations ⇒ Object
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_observations ⇒ Object
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_paths ⇒ Object
84 85 86 |
# File 'lib/tree_diff.rb', line 84 def changed_paths changes_as_objects.map(&:path) end |
#changes ⇒ Object
76 77 78 |
# File 'lib/tree_diff.rb', line 76 def changes iterate_observations(@old_object_as_mold, @current_object) end |
#changes_as_objects ⇒ Object
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
72 73 74 |
# File 'lib/tree_diff.rb', line 72 def saw_any_change? changes.present? end |