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.
158 159 160 |
# File 'lib/cfa/augeas_parser.rb', line 158 def initialize @data = [] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/cfa/augeas_parser.rb', line 237 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.
213 214 215 216 217 218 |
# File 'lib/cfa/augeas_parser.rb', line 213 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.
225 226 227 228 229 |
# File 'lib/cfa/augeas_parser.rb', line 225 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.
202 203 204 205 206 207 |
# File 'lib/cfa/augeas_parser.rb', line 202 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
154 155 156 |
# File 'lib/cfa/augeas_parser.rb', line 154 def all_data @data end |
#collection(key) ⇒ AugeasCollection
Returns collection for key.
176 177 178 |
# File 'lib/cfa/augeas_parser.rb', line 176 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.
149 150 151 |
# File 'lib/cfa/augeas_parser.rb', line 149 def data @data.reject { |e| e[:operation] == :remove }.freeze end |
#delete(matcher) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/cfa/augeas_parser.rb', line 181 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.
233 234 235 |
# File 'lib/cfa/augeas_parser.rb', line 233 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.
165 166 167 168 169 170 171 172 173 |
# File 'lib/cfa/augeas_parser.rb', line 165 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 |