Class: Eco::Data::Hashes::DiffResult
- Includes:
- Meta
- Defined in:
- lib/eco/data/hashes/diff_result.rb,
lib/eco/data/hashes/diff_result/meta.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Meta
Instance Attribute Summary collapse
-
#src_1 ⇒ Object
readonly
Returns the value of attribute src_1.
-
#src_2 ⇒ Object
readonly
Returns the value of attribute src_2.
Instance Method Summary collapse
-
#all_compared_attrs ⇒ Array<String>
Uniq access point to scope if an
attr
is in the scope of the diff compare. -
#all_source_keys ⇒ Object
All the keys that the data comes with.
-
#attr(attr) ⇒ Value
The current value of
attr
(insrc_2
). -
#attr_prev(attr) ⇒ Value
The previous value of
attr
(insrc_1
). - #del? ⇒ Boolean
-
#diff? ⇒ Boolean
Was there any change?.
-
#diff_attr?(attr) ⇒ Boolean
Is
attr
part of the attributes that change?. -
#diff_attrs ⇒ Array<Symbol>
Uniq access point to identify attrs that have changed.
-
#diff_hash ⇒ Hash
Hash with the differences as per
src_2
. -
#dup(src_1: nil, src_2: nil) ⇒ Object
Deduplication to prevent mutability when manipulation is required.
-
#initialize(src_1, src_2) ⇒ DiffResult
constructor
A new instance of DiffResult.
-
#key? ⇒ Boolean
Is the
key
attr value updated?. - #new? ⇒ Boolean
- #update? ⇒ Boolean
Methods included from Meta
Constructor Details
#initialize(src_1, src_2) ⇒ DiffResult
Returns a new instance of DiffResult.
11 12 13 14 |
# File 'lib/eco/data/hashes/diff_result.rb', line 11 def initialize(src_1, src_2) @src_1 = src_1 @src_2 = src_2 end |
Instance Attribute Details
#src_1 ⇒ Object (readonly)
Returns the value of attribute src_1.
9 10 11 |
# File 'lib/eco/data/hashes/diff_result.rb', line 9 def src_1 @src_1 end |
#src_2 ⇒ Object (readonly)
Returns the value of attribute src_2.
9 10 11 |
# File 'lib/eco/data/hashes/diff_result.rb', line 9 def src_2 @src_2 end |
Instance Method Details
#all_compared_attrs ⇒ Array<String>
When the class all_compared_attrs
has not been deefined,
it uses all_source_keys
Uniq access point to scope if an attr
is in the scope of the diff compare.
Set of attributes that are general target to identify differences
between both sources.
96 97 98 99 100 |
# File 'lib/eco/data/hashes/diff_result.rb', line 96 def all_compared_attrs super().map(&:to_s).uniq.tap do |attrs| return all_source_keys unless attrs.any? end end |
#all_source_keys ⇒ Object
All the keys that the data comes with
103 104 105 |
# File 'lib/eco/data/hashes/diff_result.rb', line 103 def all_source_keys (src_1&.keys || []) & (src_2&.keys || []) end |
#attr(attr) ⇒ Value
Returns the current value of attr
(in src_2
).
17 18 19 |
# File 'lib/eco/data/hashes/diff_result.rb', line 17 def attr(attr) get_attr(src_2, attr) end |
#attr_prev(attr) ⇒ Value
Returns the previous value of attr
(in src_1
).
22 23 24 |
# File 'lib/eco/data/hashes/diff_result.rb', line 22 def attr_prev(attr) get_attr(src_1, attr) end |
#del? ⇒ Boolean
37 38 39 |
# File 'lib/eco/data/hashes/diff_result.rb', line 37 def del? src_1 && !src_2 end |
#diff? ⇒ Boolean
diff_attrs
may not include the key
attribute
This is always included via new?
(new key value) and del?
(missing key value)
Returns was there any change?.
62 63 64 |
# File 'lib/eco/data/hashes/diff_result.rb', line 62 def diff? new? || del? || diff_attrs.any? end |
#diff_attr?(attr) ⇒ Boolean
Is attr
part of the attributes that change?
73 74 75 76 77 78 |
# File 'lib/eco/data/hashes/diff_result.rb', line 73 def diff_attr?(attr) return true if new? return true if del? diff_attrs.include?(attr.to_s) end |
#diff_attrs ⇒ Array<Symbol>
when is new?
or to be deleted (del?
), there's nothing to compare.
Uniq access point to identify attrs that have changed.
49 50 51 52 53 54 55 56 57 |
# File 'lib/eco/data/hashes/diff_result.rb', line 49 def diff_attrs return (@diff_attrs = []) if new? || del? @diff_attrs ||= all_compared_attrs.each_with_object([]) do |kattr, out| next unless comparable_attr?(kattr) out << kattr unless eq?(attr_prev(kattr), attr(kattr)) end end |
#diff_hash ⇒ Hash
the key
attribute will always be added (even if there's no change)
Returns hash with the differences as per src_2
.
82 83 84 85 86 87 88 |
# File 'lib/eco/data/hashes/diff_result.rb', line 82 def diff_hash target_attrs = [key] | all_compared_attrs return slice_attrs(src_2, *target_attrs) if new? return slice_attrs(src_1, key) if del? slice_attrs(src_2, key, *diff_attrs) end |
#dup(src_1: nil, src_2: nil) ⇒ Object
Deduplication to prevent mutability when manipulation is required.
27 28 29 30 31 |
# File 'lib/eco/data/hashes/diff_result.rb', line 27 def dup(src_1: nil, src_2: nil) src_1 ||= self.src_1 src_2 ||= self.src_2 self.class.new(src_1.dup, src_2.dup) end |
#key? ⇒ Boolean
that new?
and del?
won't be considered as key's change
Is the key
attr value updated?
68 69 70 |
# File 'lib/eco/data/hashes/diff_result.rb', line 68 def key? !(new? || del?) && diff_attr?(key) end |
#new? ⇒ Boolean
33 34 35 |
# File 'lib/eco/data/hashes/diff_result.rb', line 33 def new? src_2 && !src_1 end |
#update? ⇒ Boolean
41 42 43 |
# File 'lib/eco/data/hashes/diff_result.rb', line 41 def update? !new? && !del? && diff? end |