Class: CFA::BeforePlacer

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

Overview

Finds a specific element using a Matcher and places the new element before it. Appends at the end if a match is not found.

Useful when a config option should be inserted to a specific location, or when assigning a comment to an option.

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ BeforePlacer

Returns a new instance of BeforePlacer.

Parameters:



40
41
42
# File 'lib/cfa/placer.rb', line 40

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.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cfa/placer.rb', line 45

def new_element(tree)
  index = tree.all_data.index(&@matcher)

  res = create_element
  if index
    tree.all_data.insert(index, res)
  else
    tree.all_data << res
  end

  res
end