Class: Kustomize::Json6902Patch::AppendOp

Inherits:
Op
  • Object
show all
Defined in:
lib/kustomize/json_6902_patch/append_op.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Op

#parse_lens

Constructor Details

#initialize(array_path:, elements:) ⇒ AppendOp



20
21
22
23
# File 'lib/kustomize/json_6902_patch/append_op.rb', line 20

def initialize(array_path:, elements:)
  @lens = parse_lens(array_path)
  @new_elements = elements
end

Class Method Details

.create(patch_spec) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kustomize/json_6902_patch/append_op.rb', line 4

def self.create(patch_spec)
  elements =
    if e = patch_spec['element']
      [e]
    elsif es = patch_spec['elements']
      es
    else
      raise ArgumentError, "must specify one of 'element' or 'elements' in: #{patch_spec.inspect}"
    end

  new(
    array_path: patch_spec['path'],
    elements: elements
  )
end

Instance Method Details

#apply(rc) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kustomize/json_6902_patch/append_op.rb', line 25

def apply(rc)
  @lens.update_in(rc) do |orig_arr|
    new_arr = orig_arr.dup || []

    @new_elements.each do |elem|
      new_arr.push(elem)
    end

    [:set, new_arr]
  end
end