Class: CFA::ReplacePlacer

Inherits:
Placer
  • Object
show all
Defined in:
lib/cfa/placer.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ ReplacePlacer

Returns a new instance of ReplacePlacer.

Parameters:



91
92
93
# File 'lib/cfa/placer.rb', line 91

def initialize(matcher)
  @matcher = matcher
end

Instance Method Details

#new_element(tree) ⇒ AugeasElement, Hash

Returns the new element; it is empty! Note that the return value is actually a Hash; AugeasElement documents its structure.

Parameters:

Returns:

  • (AugeasElement, Hash)

    the new element; it is empty! Note that the return value is actually a Hash; AugeasElement documents its structure.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cfa/placer.rb', line 96

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