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

Inherits:
Object
  • Object
show all
Extended by:
Language::Models::ClassHelpers
Includes:
DiffMeta
Defined in:
lib/eco/data/hashes/diff_result.rb

Direct Known Subclasses

Locations::NodeDiff

Constant Summary

Constants included from Language::Models::ClassHelpers

Language::Models::ClassHelpers::NOT_USED

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#src1Object (readonly)

Returns the value of attribute src1.



10
11
12
# File 'lib/eco/data/hashes/diff_result.rb', line 10

def src1
  @src1
end

#src2Object (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).

Returns:

  • (Value)

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

Returns:

  • (Value)

    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

Returns:

  • (Boolean)


21
22
23
# File 'lib/eco/data/hashes/diff_result.rb', line 21

def case_sensitive?
  self.class.case_sensitive?
end

#comparable_attrsArray<String>

Note:

when is new? or to be deleted (del?), it returns empty array.

Returns the set of attributes that are comparable in this instance.

Returns:

  • (Array<String>)

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

Returns the set of attributes that are comparable in this class.

Returns:

  • (Array<String>)

    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

Returns:

  • (Boolean)


35
36
37
# File 'lib/eco/data/hashes/diff_result.rb', line 35

def del?
  !!src1 && !src2
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?



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?

Returns:

  • (Boolean)


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

Returns hash with the differences as per src2.

Returns:

  • (Array<Symbol>)

    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_hashHash

Note:

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

Returns hash with the differences as per src2.

Returns:

  • (Hash)

    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.

Returns:

  • (Boolean)

    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

#keyObject



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?

Returns:

  • (Boolean)


51
52
53
# File 'lib/eco/data/hashes/diff_result.rb', line 51

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

#new?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/eco/data/hashes/diff_result.rb', line 31

def new?
  !src1 && !!src2
end

#update?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/eco/data/hashes/diff_result.rb', line 39

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