Class: RubyTerraform::Models::Path

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, ValueEquality
Defined in:
lib/ruby_terraform/models/path.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: TraversalStep

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueEquality

#==, #hash

Constructor Details

#initialize(elements) ⇒ Path

Returns a new instance of Path.



24
25
26
# File 'lib/ruby_terraform/models/path.rb', line 24

def initialize(elements)
  @elements = elements.compact
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



22
23
24
# File 'lib/ruby_terraform/models/path.rb', line 22

def elements
  @elements
end

Class Method Details

.emptyObject



10
11
12
# File 'lib/ruby_terraform/models/path.rb', line 10

def empty
  new([])
end

Instance Method Details

#<=>(other) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/ruby_terraform/models/path.rb', line 97

def <=>(other)
  return 0 if self == other

  left, right = diff(other)
  return -1 if left.empty?
  return 1 if right.empty?

  compare_numbers_before_symbols(left.first, right.first)
end

#append(element) ⇒ Object



62
63
64
# File 'lib/ruby_terraform/models/path.rb', line 62

def append(element)
  self.class.new(elements + [element])
end

#before_location(index) ⇒ Object



56
57
58
59
60
# File 'lib/ruby_terraform/models/path.rb', line 56

def before_location(index)
  return self.class.new([]) if index.negative?

  self.class.new(elements[0...index])
end

#diff(other) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ruby_terraform/models/path.rb', line 70

def diff(other)
  left, right = match_lengths(elements, other.elements)
  pairwise = left.zip(right)
  difference = pairwise.drop_while { |e| e[0] == e[1] }
  difference = difference.empty? ? [[], []] : difference.transpose
  difference.map { |e| self.class.new(e) }
end

#drop(count = 1) ⇒ Object



66
67
68
# File 'lib/ruby_terraform/models/path.rb', line 66

def drop(count = 1)
  self.class.new(elements.drop(count))
end

#list_indicesObject



43
44
45
46
47
48
# File 'lib/ruby_terraform/models/path.rb', line 43

def list_indices
  elements.each_with_index.inject([]) do |acc, element_index|
    element, index = element_index
    element.is_a?(Numeric) ? acc + [[index, element]] : acc
  end
end

#read(object, default: nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/ruby_terraform/models/path.rb', line 88

def read(object, default: nil)
  return default if empty?

  result = object.dig(*elements)
  result.nil? ? default : result
rescue NoMethodError, TypeError
  default
end

#references_any_lists?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ruby_terraform/models/path.rb', line 28

def references_any_lists?
  elements.any? { |e| e.is_a?(Numeric) }
end

#references_any_maps?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ruby_terraform/models/path.rb', line 32

def references_any_maps?
  elements.any? { |e| e.is_a?(Symbol) }
end

#same_parent_collection?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/ruby_terraform/models/path.rb', line 36

def same_parent_collection?(other)
  return true if self == other

  left, right = diff(other)
  left.length == 1 && right.length == 1
end

#stateObject



107
108
109
# File 'lib/ruby_terraform/models/path.rb', line 107

def state
  [elements]
end

#to_location(index) ⇒ Object



50
51
52
53
54
# File 'lib/ruby_terraform/models/path.rb', line 50

def to_location(index)
  return self.class.new([]) if index.negative?

  self.class.new(elements[0..index])
end

#traverse(initial, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/ruby_terraform/models/path.rb', line 78

def traverse(initial, &block)
  initial_context = initial_traversal_context(initial)
  final_context = elements.inject(initial_context) do |context, element|
    state = block.call(context[:state], context[:step])
    next_traversal_context(state, context[:step], element)
  end

  final_context[:state]
end