class CFA::ReplacePlacer

Finds a specific element using a {Matcher} and replaces it with the new element. Appends at the end if a match is not found.

Useful in key-value configuration files where a specific key needs to be assigned.

Public Class Methods

new(matcher) click to toggle source

@param [Matcher] matcher

# File lib/cfa/placer.rb, line 94
def initialize(matcher)
  @matcher = matcher
end

Public Instance Methods

new_element(tree) click to toggle source

(see Placer#new_element)

# File lib/cfa/placer.rb, line 99
def new_element(tree)
  index = tree.all_data.index(&@matcher)
  res = create_element

  if index
    # remove old one and add new one, as it can have different key
    # which cause problem to simple modify
    tree.all_data[index][:operation] = :remove
    tree.all_data.insert(index + 1, res)
  else
    tree.all_data << res
  end

  res
end