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.
Constructor Details
#initialize ⇒ AugeasTree
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.
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.
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.
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_data ⇒ Object
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.
162 163 164 |
# File 'lib/cfa/augeas_parser.rb', line 162 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.
148 149 150 |
# File 'lib/cfa/augeas_parser.rb', line 148 def data @data.select { |e| e[:operation] != :remove }.freeze end |
#delete(matcher) ⇒ Object
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.
219 220 221 |
# File 'lib/cfa/augeas_parser.rb', line 219 def select(matcher) @data.select(&matcher) end |