Class: ConfigFilesApi::AugeasTree

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

Overview

Represent parsed augeas config tree with user friendly methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAugeasTree

Returns a new instance of AugeasTree.



55
56
57
# File 'lib/config_files_api/augeas_parser.rb', line 55

def initialize
  @data = []
end

Instance Attribute Details

#dataObject (readonly)

low level access to augeas structure



53
54
55
# File 'lib/config_files_api/augeas_parser.rb', line 53

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object



73
74
75
76
77
78
# File 'lib/config_files_api/augeas_parser.rb', line 73

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

  nil
end

#[]=(key, value) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/config_files_api/augeas_parser.rb', line 80

def []=(key, value)
  entry = @data.find { |d| d[:key] == key }
  if entry
    entry[:value] = value
  else
    @data << {
      key:   key,
      value: value
    }
  end
end

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



67
68
69
70
71
# File 'lib/config_files_api/augeas_parser.rb', line 67

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

#collection(key) ⇒ Object



59
60
61
# File 'lib/config_files_api/augeas_parser.rb', line 59

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

#delete(key) ⇒ Object



63
64
65
# File 'lib/config_files_api/augeas_parser.rb', line 63

def delete(key)
  @data.reject! { |entry| entry[:key] == key }
end

#dump_tree(prefix = "") ⇒ Object

Note:

for debugging purpose only



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/config_files_api/augeas_parser.rb', line 125

def dump_tree(prefix = "")
  arrays = {}

  @data.each_with_object("") do |entry, res|
    aug_key = obtain_aug_key(prefix, entry, arrays)
    if entry[:value].is_a? AugeasTree
      res << entry[:value].dump_tree(aug_key)
    else
      res << aug_key << "\n"
    end
  end
end

#load_from_augeas(aug, prefix) ⇒ Object

Note:

for internal usage only



98
99
100
101
102
103
104
105
106
107
# File 'lib/config_files_api/augeas_parser.rb', line 98

def load_from_augeas(aug, prefix)
  matches = aug.match("#{prefix}/*")

  @data = matches.map do |aug_key|
    {
      key:   load_key(prefix, aug_key),
      value: load_value(aug, aug_key)
    }
  end
end

#save_to_augeas(aug, prefix) ⇒ Object

Note:

for internal usage only



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/config_files_api/augeas_parser.rb', line 111

def save_to_augeas(aug, prefix)
  arrays = {}

  @data.each do |entry|
    aug_key = obtain_aug_key(prefix, entry, arrays)
    if entry[:value].is_a? AugeasTree
      entry[:value].save_to_augeas(aug, aug_key)
    else
      report_error(aug) unless aug.set(aug_key, entry[:value])
    end
  end
end

#select(matcher) ⇒ Object



92
93
94
# File 'lib/config_files_api/augeas_parser.rb', line 92

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