Class: Eco::Data::Hashes::DiffResult

Inherits:
Object
  • Object
show all
Includes:
Meta
Defined in:
lib/eco/data/hashes/diff_result.rb,
lib/eco/data/hashes/diff_result/meta.rb

Direct Known Subclasses

Locations::NodeDiff

Defined Under Namespace

Modules: Meta

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Meta

#case_sensitive?, #key

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_1Object (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_2Object (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_attrsArray<String>

Note:

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.

Returns:

  • (Array<String>)

    the set of attributes that are comparable in this class.



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_keysObject

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).

Returns:

  • (Value)

    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).

Returns:

  • (Value)

    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

Returns:

  • (Boolean)


37
38
39
# File 'lib/eco/data/hashes/diff_result.rb', line 37

def del?
  src_1 && !src_2
end

#diff?Boolean

Note:

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?.

Returns:

  • (Boolean)

    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?

Returns:

  • (Boolean)


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_attrsArray<Symbol>

Note:

when is new? or to be deleted (del?), there's nothing to compare.

Uniq access point to identify attrs that have changed.

Returns:

  • (Array<Symbol>)

    hash with the list of attrs that are different between src_1 and src_2.



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_hashHash

Note:

the key attribute will always be added (even if there's no change)

Returns hash with the differences as per src_2.

Returns:

  • (Hash)

    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

Note:

that new? and del? won't be considered as key's change

Is the key attr value updated?

Returns:

  • (Boolean)


68
69
70
# File 'lib/eco/data/hashes/diff_result.rb', line 68

def key?
  !(new? || del?) && diff_attr?(key)
end

#new?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/eco/data/hashes/diff_result.rb', line 33

def new?
  src_2 && !src_1
end

#update?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/eco/data/hashes/diff_result.rb', line 41

def update?
  !new? && !del? && diff?
end