Class: CFA::AugeasTree
- Inherits:
-
Object
- Object
- CFA::AugeasTree
- Defined in:
- lib/cfa/augeas_parser.rb
Overview
Represents a parsed Augeas config tree with user friendly methods
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ String, ...
Finds given key in tree.
-
#[]=(key, value) ⇒ Object
Replace the first value for key with value.
-
#add(key, value, placer = AppendPlacer.new) ⇒ Object
Adds the given value for key in the tree.
-
#all_data ⇒ Object
low level access to all AugeasElement including ones marked for removal.
-
#collection(key) ⇒ AugeasCollection
Collection for key.
-
#data ⇒ Array<Hash{Symbol => Object}>
Low level access to Augeas structure.
- #delete(matcher) ⇒ Object
-
#initialize ⇒ AugeasTree
constructor
A new instance of AugeasTree.
-
#select(matcher) ⇒ Array<AugeasElement>
Matching elements.
-
#unique_id ⇒ Object
Gets new unique id in numberic sequence.
Constructor Details
#initialize ⇒ AugeasTree
Returns a new instance of AugeasTree.
220 221 222 |
# File 'lib/cfa/augeas_parser.rb', line 220 def initialize @data = [] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/cfa/augeas_parser.rb', line 301 def ==(other) return false if self.class != other.class other_data = other.data # do not compute again data.each_with_index do |entry, index| other_entry = other_data[index] return false unless other_entry return false if entry[:key] != other_entry[:key] return false if entry[:value] != other_entry[:value] end true end |
#[](key) ⇒ String, ...
Finds given key in tree.
277 278 279 280 281 282 |
# File 'lib/cfa/augeas_parser.rb', line 277 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.
289 290 291 292 293 |
# File 'lib/cfa/augeas_parser.rb', line 289 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.
266 267 268 269 270 271 |
# File 'lib/cfa/augeas_parser.rb', line 266 def add(key, value, placer = AppendPlacer.new) element = placer.new_element(self) element[:key] = key element[:value] = value element[:operation] = :add end |
#all_data ⇒ Object
low level access to all AugeasElement including ones marked for removal
216 217 218 |
# File 'lib/cfa/augeas_parser.rb', line 216 def all_data @data end |
#collection(key) ⇒ AugeasCollection
Returns collection for key.
239 240 241 |
# File 'lib/cfa/augeas_parser.rb', line 239 def collection(key) AugeasCollection.new(self, key) end |
#data ⇒ Array<Hash{Symbol => Object}>
Low level access to Augeas structure
An ordered mapping, represented by an Array of AugeasElement, but without any removed elements.
211 212 213 |
# File 'lib/cfa/augeas_parser.rb', line 211 def data @data.reject { |e| e[:operation] == :remove }.freeze end |
#delete(matcher) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/cfa/augeas_parser.rb', line 244 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.
297 298 299 |
# File 'lib/cfa/augeas_parser.rb', line 297 def select(matcher) data.select(&matcher) end |
#unique_id ⇒ Object
Gets new unique id in numberic sequence. Useful for augeas models that using sequences like /etc/hosts . It have keys like “1”, “2” and when adding new one it need to find new key.
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/cfa/augeas_parser.rb', line 227 def unique_id # check all_data instead of data, as we have to not reuse deleted key ids = Set.new(all_data.map { |e| e[:key] }) id = 1 loop do return id.to_s unless ids.include?(id.to_s) id += 1 end end |