Class: Kustomize::Json6902Patch::GsubOp

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Op

#parse_lens

Constructor Details

#initialize(paths:, pattern:, replacement:) ⇒ GsubOp

Returns a new instance of GsubOp.



12
13
14
15
16
# File 'lib/kustomize/json_6902_patch/gsub_op.rb', line 12

def initialize(paths:, pattern:, replacement:)
  @lenses = paths.map{ |path| parse_lens(path) }
  @pattern = pattern
  @replacement = replacement
end

Class Method Details

.create(patch_spec) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/kustomize/json_6902_patch/gsub_op.rb', line 4

def self.create(patch_spec)
  self.new(
    paths: patch_spec['paths'],
    pattern: Regexp.new(patch_spec['pattern'], Regexp::EXTENDED),
    replacement: patch_spec['replacement']
  )
end

Instance Method Details

#apply(rc) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kustomize/json_6902_patch/gsub_op.rb', line 18

def apply(rc)
  @lenses.inject(rc) do |doc, lens|
    lens.update_in(doc) do |orig_value|
      next(:keep) unless orig_value.kind_of?(String)

      new_value = orig_value.gsub(@pattern, @replacement)

      if new_value != orig_value
        [:set, new_value]
      else
        :keep
      end
    end
  end
end