Class: CFA::ReplacePlacer
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
-
#initialize(matcher) ⇒ ReplacePlacer
constructor
A new instance of ReplacePlacer.
- #new_element(tree) ⇒ AugeasElement, Hash
Constructor Details
#initialize(matcher) ⇒ ReplacePlacer
Returns a new instance of ReplacePlacer.
94 95 96 |
# File 'lib/cfa/placer.rb', line 94 def initialize(matcher) @matcher = matcher end |
Instance Method Details
#new_element(tree) ⇒ AugeasElement, Hash
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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 |