Class: Philiprehberger::Differ::Changeset
- Inherits:
-
Object
- Object
- Philiprehberger::Differ::Changeset
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/differ/changeset.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
Instance Method Summary collapse
- #added ⇒ Object
- #apply(hash) ⇒ Object
- #changed ⇒ Object
- #changed? ⇒ Boolean
-
#count ⇒ Integer
Number of changes.
-
#each {|Change| ... } ⇒ Enumerator
Iterate over each Change.
-
#include?(path) ⇒ Boolean
Check if a specific path was changed.
-
#initialize(changes = []) ⇒ Changeset
constructor
A new instance of Changeset.
-
#paths ⇒ Array<String>
Array of all changed paths.
- #removed ⇒ Object
- #revert(hash) ⇒ Object
-
#summary ⇒ Hash{Symbol => Integer}
Summary counts by change type.
- #to_h ⇒ Object
- #to_json_patch ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(changes = []) ⇒ Changeset
Returns a new instance of Changeset.
10 11 12 |
# File 'lib/philiprehberger/differ/changeset.rb', line 10 def initialize(changes = []) @changes = changes end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
8 9 10 |
# File 'lib/philiprehberger/differ/changeset.rb', line 8 def changes @changes end |
Instance Method Details
#added ⇒ Object
56 57 58 |
# File 'lib/philiprehberger/differ/changeset.rb', line 56 def added @changes.select { |c| c.type == :added } end |
#apply(hash) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/philiprehberger/differ/changeset.rb', line 68 def apply(hash) result = deep_dup(hash) sorted = sort_for_apply(@changes) sorted.each { |c| apply_change(result, c) } result end |
#changed ⇒ Object
64 65 66 |
# File 'lib/philiprehberger/differ/changeset.rb', line 64 def changed @changes.select { |c| c.type == :changed } end |
#changed? ⇒ Boolean
52 53 54 |
# File 'lib/philiprehberger/differ/changeset.rb', line 52 def changed? !@changes.empty? end |
#count ⇒ Integer
Number of changes.
25 26 27 |
# File 'lib/philiprehberger/differ/changeset.rb', line 25 def count @changes.length end |
#each {|Change| ... } ⇒ Enumerator
Iterate over each Change.
18 19 20 |
# File 'lib/philiprehberger/differ/changeset.rb', line 18 def each(&) @changes.each(&) end |
#include?(path) ⇒ Boolean
Check if a specific path was changed.
40 41 42 43 |
# File 'lib/philiprehberger/differ/changeset.rb', line 40 def include?(path) path_s = path.to_s @changes.any? { |c| c.path.to_s == path_s } end |
#paths ⇒ Array<String>
Array of all changed paths.
32 33 34 |
# File 'lib/philiprehberger/differ/changeset.rb', line 32 def paths @changes.map(&:path) end |
#removed ⇒ Object
60 61 62 |
# File 'lib/philiprehberger/differ/changeset.rb', line 60 def removed @changes.select { |c| c.type == :removed } end |
#revert(hash) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/philiprehberger/differ/changeset.rb', line 75 def revert(hash) result = deep_dup(hash) sorted = sort_for_revert(@changes) sorted.each { |c| revert_change(result, c) } result end |
#summary ⇒ Hash{Symbol => Integer}
Summary counts by change type.
48 49 50 |
# File 'lib/philiprehberger/differ/changeset.rb', line 48 def summary { added: added.length, removed: removed.length, changed: changed.length } end |
#to_h ⇒ Object
82 83 84 |
# File 'lib/philiprehberger/differ/changeset.rb', line 82 def to_h { changes: @changes.map(&:to_h) } end |
#to_json_patch ⇒ Object
90 91 92 |
# File 'lib/philiprehberger/differ/changeset.rb', line 90 def to_json_patch Formatters::JsonPatch.format(self) end |
#to_text ⇒ Object
86 87 88 |
# File 'lib/philiprehberger/differ/changeset.rb', line 86 def to_text Formatters::Text.format(self) end |