Class: CFA::AugeasTree

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

Overview

Represents a parsed Augeas config tree with user friendly methods

Instance Method Summary collapse

Constructor Details

#initializeAugeasTree

Returns a new instance of AugeasTree.



157
158
159
# File 'lib/cfa/augeas_parser.rb', line 157

def initialize
  @data = []
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



223
224
225
226
227
228
229
230
231
232
# File 'lib/cfa/augeas_parser.rb', line 223

def ==(other)
  return false if self.class != other.class
  other_data = other.data # do not compute again
  data.each_with_index do |entry, index|
    return false if entry[:key] != other_data[index][:key]
    return false if entry[:value] != other_data[index][:value]
  end

  true
end

#[](key) ⇒ String, ...

Finds given key in tree.

Parameters:

  • key (String)

Returns:



199
200
201
202
203
204
# File 'lib/cfa/augeas_parser.rb', line 199

def [](key)
  entry = @data.find { |d| d[:key] == key && d[:operation] != :remove }
  return entry[:value] if entry

  nil
end

#[]=(key, value) ⇒ Object

Replace the first value for key with value. Append a new element if key did not exist. If key was previously removed, then put it back to its old position.

Parameters:



211
212
213
214
215
# File 'lib/cfa/augeas_parser.rb', line 211

def []=(key, value)
  new_entry = entry_to_modify(key, value)
  new_entry[:key] = key
  new_entry[:value] = value
end

#add(key, value, placer = AppendPlacer.new) ⇒ Object

Adds the given value for key in the tree.

By default an AppendPlacer is used which produces duplicate keys but ReplacePlacer can be used to replace the first duplicate.

Parameters:

  • key (String)
  • value (String, AugeasTree, AugeasTreeValue)
  • placer (Placer) (defaults to: AppendPlacer.new)

    determines where to insert value in tree. Useful e.g. to specify order of keys or placing comment above of given key.



188
189
190
191
192
193
# File 'lib/cfa/augeas_parser.rb', line 188

def add(key, value, placer = AppendPlacer.new)
  element = placer.new_element(self)
  element[:key] = key
  element[:value] = value
  element[:operation] = :add
end

#all_dataObject

low level access to all AugeasElement including ones marked for removal



153
154
155
# File 'lib/cfa/augeas_parser.rb', line 153

def all_data
  @data
end

#collection(key) ⇒ AugeasCollection

Returns collection for key.

Returns:



162
163
164
# File 'lib/cfa/augeas_parser.rb', line 162

def collection(key)
  AugeasCollection.new(self, key)
end

#dataArray<Hash{Symbol => Object}>

Low level access to Augeas structure

An ordered mapping, represented by an Array of AugeasElement, but without any removed elements.

Returns:

  • (Array<Hash{Symbol => Object}>)

    a frozen array as it is just a copy of the real data

See Also:



148
149
150
# File 'lib/cfa/augeas_parser.rb', line 148

def data
  @data.select { |e| e[:operation] != :remove }.freeze
end

#delete(matcher) ⇒ Object

Parameters:



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cfa/augeas_parser.rb', line 167

def delete(matcher)
  return if matcher.nil?
  unless matcher.is_a?(CFA::Matcher)
    matcher = CFA::Matcher.new(key: matcher)
  end
  to_remove = @data.select(&matcher)

  to_delete, to_mark = to_remove.partition { |e| e[:operation] == :add }
  @data -= to_delete
  to_mark.each { |e| e[:operation] = :remove }
end

#select(matcher) ⇒ Array<AugeasElement>

Returns matching elements.

Parameters:

Returns:



219
220
221
# File 'lib/cfa/augeas_parser.rb', line 219

def select(matcher)
  @data.select(&matcher)
end