Class: Eco::Data::Hashes::DiffResult
- Extended by:
- Language::Models::ClassHelpers
- Includes:
- DiffMeta
- Defined in:
- lib/eco/data/hashes/diff_result.rb
Direct Known Subclasses
Constant Summary
Constants included from Language::Models::ClassHelpers
Language::Models::ClassHelpers::NOT_USED
Instance Attribute Summary collapse
-
#src1 ⇒ Object
readonly
Returns the value of attribute src1.
-
#src2 ⇒ Object
readonly
Returns the value of attribute src2.
Instance Method Summary collapse
-
#attr(attr) ⇒ Value
The current value of
attr
(insrc2
). -
#attr_prev(attr) ⇒ Value
The previous value of
attr
(insrc1
). - #case_sensitive? ⇒ Boolean
-
#comparable_attrs ⇒ Array<String>
The set of attributes that are comparable in this instance.
-
#compared_attrs ⇒ Array<String>
The set of attributes that are comparable in this class.
- #del? ⇒ Boolean
-
#diff? ⇒ Boolean
Was there any change?.
-
#diff_attr?(attr) ⇒ Boolean
Is
attr
part of the attributes that change?. -
#diff_attrs ⇒ Array<Symbol>
Hash with the differences as per
src2
. -
#diff_hash ⇒ Hash
Hash with the differences as per
src2
. - #dup(src1: nil, src2: nil) ⇒ Object
-
#eq?(val1, val2) ⇒ Boolean
Whether
val1
is equal toval2
. -
#initialize(src1, src2) ⇒ DiffResult
constructor
A new instance of DiffResult.
- #key ⇒ Object
-
#key? ⇒ Boolean
Is the
key
attr value changing?. - #new? ⇒ Boolean
- #update? ⇒ Boolean
Methods included from Language::Models::ClassHelpers
class_resolver, inheritable_attrs, inheritable_class_vars, inherited, instance_variable_name, new_class, redef_without_warning, resolve_class, to_constant, used_param?
Constructor Details
#initialize(src1, src2) ⇒ DiffResult
Returns a new instance of DiffResult.
12 13 14 15 |
# File 'lib/eco/data/hashes/diff_result.rb', line 12 def initialize(src1, src2) @src1 = src1 @src2 = src2 end |
Instance Attribute Details
#src1 ⇒ Object (readonly)
Returns the value of attribute src1.
10 11 12 |
# File 'lib/eco/data/hashes/diff_result.rb', line 10 def src1 @src1 end |
#src2 ⇒ Object (readonly)
Returns the value of attribute src2.
10 11 12 |
# File 'lib/eco/data/hashes/diff_result.rb', line 10 def src2 @src2 end |
Instance Method Details
#attr(attr) ⇒ Value
Returns the current value of attr
(in src2
).
63 64 65 66 |
# File 'lib/eco/data/hashes/diff_result.rb', line 63 def attr(attr) return nil unless src2 src2[attr.to_s] end |
#attr_prev(attr) ⇒ Value
Returns the previous value of attr
(in src1
).
69 70 71 72 |
# File 'lib/eco/data/hashes/diff_result.rb', line 69 def attr_prev(attr) return nil unless src1 src1[attr.to_s] end |
#case_sensitive? ⇒ Boolean
21 22 23 |
# File 'lib/eco/data/hashes/diff_result.rb', line 21 def case_sensitive? self.class.case_sensitive? end |
#comparable_attrs ⇒ Array<String>
when is new?
or to be deleted (del?
), it returns empty array.
Returns the set of attributes that are comparable in this instance.
100 101 102 103 |
# File 'lib/eco/data/hashes/diff_result.rb', line 100 def comparable_attrs return [] if new? || del? compared_attrs end |
#compared_attrs ⇒ Array<String>
Returns the set of attributes that are comparable in this class.
106 107 108 109 110 |
# File 'lib/eco/data/hashes/diff_result.rb', line 106 def compared_attrs comp_attrs = self.class.compared_attrs.map(&:to_s).uniq return comp_attrs unless comp_attrs.empty? (src1&.keys || []) & (src2&.keys || []) end |
#del? ⇒ Boolean
35 36 37 |
# File 'lib/eco/data/hashes/diff_result.rb', line 35 def del? !!src1 && !src2 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?.
46 47 48 |
# File 'lib/eco/data/hashes/diff_result.rb', line 46 def diff? new? || del? || !diff_attrs.empty? end |
#diff_attr?(attr) ⇒ Boolean
Is attr
part of the attributes that change?
56 57 58 59 60 |
# File 'lib/eco/data/hashes/diff_result.rb', line 56 def diff_attr?(attr) return true if new? return true if del? diff_attrs.include?(attr.to_s) end |
#diff_attrs ⇒ Array<Symbol>
Returns hash with the differences as per src2
.
84 85 86 87 88 |
# File 'lib/eco/data/hashes/diff_result.rb', line 84 def diff_attrs @diff_attrs ||= comparable_attrs.each_with_object([]) do |attr, out| out << attr unless eq?(src1[attr], src2[attr]) 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 src2
.
76 77 78 79 80 81 |
# File 'lib/eco/data/hashes/diff_result.rb', line 76 def diff_hash target_attrs = [key] | compared_attrs return src2.slice(*target_attrs) if new? return src1.slice(key) if del? src2.slice(key, *diff_attrs) end |
#dup(src1: nil, src2: nil) ⇒ Object
25 26 27 28 29 |
# File 'lib/eco/data/hashes/diff_result.rb', line 25 def dup(src1: nil, src2: nil) src1 ||= self.src1 src2 ||= self.src2 self.class.new(src1.dup, src2.dup) end |
#eq?(val1, val2) ⇒ Boolean
Returns whether val1
is equal to val2
.
91 92 93 94 95 96 |
# File 'lib/eco/data/hashes/diff_result.rb', line 91 def eq?(val1, val2) return true if val1 == val2 return false if case_sensitive? return false if !val2 || !val1 val1.upcase == val2.upcase end |
#key ⇒ Object
17 18 19 |
# File 'lib/eco/data/hashes/diff_result.rb', line 17 def key self.class.key end |
#key? ⇒ Boolean
Is the key
attr value changing?
51 52 53 |
# File 'lib/eco/data/hashes/diff_result.rb', line 51 def key? !(new? || del?) && diff_attr?(key) end |
#new? ⇒ Boolean
31 32 33 |
# File 'lib/eco/data/hashes/diff_result.rb', line 31 def new? !src1 && !!src2 end |
#update? ⇒ Boolean
39 40 41 |
# File 'lib/eco/data/hashes/diff_result.rb', line 39 def update? !new? && !del? && diff? end |