Class: AbideDevUtils::XCCDF::Diff::ChangeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/xccdf/diff.rb

Overview

Represents a change in a diff.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(change:, key:, value:, value_to: nil) ⇒ ChangeSet

Returns a new instance of ChangeSet.



22
23
24
25
26
27
28
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 22

def initialize(change:, key:, value:, value_to: nil)
  validate_change(change)
  @change = change
  @key = key
  @value = value
  @value_to = value_to
end

Instance Attribute Details

#changeObject (readonly)

Returns the value of attribute change.



20
21
22
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 20

def change
  @change
end

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 20

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



20
21
22
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 20

def value
  @value
end

#value_toObject (readonly)

Returns the value of attribute value_to.



20
21
22
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 20

def value_to
  @value_to
end

Instance Method Details

#can_merge?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 43

def can_merge?(other)
  return false unless (change == '-' && other.change == '+') || (change == '+' && other.change == '-')
  return false unless key == other.key || value_hash_equality(other)

  true
end

#merge(other) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 50

def merge(other)
  unless can_merge?(other)
    raise ArgumentError, 'Cannot merge. Possible causes: change is identical; key or value do not match'
  end

  new_to_value = value == other.value ? nil : other.value
  ChangeSet.new(
    change: '~',
    key: key,
    value: value,
    value_to: new_to_value
  )
end

#merge!(other) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 64

def merge!(other)
  new_props = merge(other)
  @change = new_props.change
  @key = new_props.key
  @value = new_props.value
  @value_to = new_props.value_to
end

#to_hObject



34
35
36
37
38
39
40
41
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 34

def to_h
  {
    change: change,
    key: key,
    value: value,
    value_to: value_to,
  }
end

#to_sObject



30
31
32
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 30

def to_s
  value_change_string(value, value_to)
end