cfa 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4abe1cefacc4777f6016b432b3a306f16b1b7ac
4
- data.tar.gz: 359c1f958762e205c5313e0027dda5a09a10bc5c
3
+ metadata.gz: ed36ef61432f274fddde803ec94e699e1cdaa96c
4
+ data.tar.gz: 5ab4d242ebbbec5a794ed6b2309d76396f7a69ee
5
5
  SHA512:
6
- metadata.gz: c8bdb5a5aa8391dfb8c46448b760868e257b71e195daa38cc7d272f1729e52d7c20be9acd6fa2632b63ec4bfb736c7bba41a3293b6e7d3e12d319d0a786fd885
7
- data.tar.gz: 69296e9dbd626125d6e0612f580cbbd12266e004794231c9b61c980bd66114ee90f84cafd22393fc6b1000ce4b42d92f6ff724b2ce34c6ff96d77feba2c604cb
6
+ metadata.gz: b7f9e87b62d41a80f332c123317a3ab01368ea2c32c544b7338b3dd0977f9fe3d1ed28e5c8807c58a9ec50bdf3b77a3c9eb029a3ce2c56bb01fe634b646bfdda
7
+ data.tar.gz: c60aa0011596224752a97a0b8ecc6df8b883603e9c559df15920d3609ff9f763eec59c988bd8eca884938e83ffb6c0359145a3646863fd7c57ee88707c5363b8
@@ -1,3 +1,4 @@
1
+ require "set"
1
2
  require "augeas"
2
3
  require "forwardable"
3
4
  require "cfa/placer"
@@ -158,6 +159,19 @@ module CFA
158
159
  @data = []
159
160
  end
160
161
 
162
+ # Gets new unique id in numberic sequence. Useful for augeas models that
163
+ # using sequences like /etc/hosts . It have keys like "1", "2" and when
164
+ # adding new one it need to find new key.
165
+ def unique_id
166
+ # check all_data instead of data, as we have to not reuse deleted key
167
+ ids = Set.new(all_data.map { |e| e[:key] })
168
+ id = 1
169
+ loop do
170
+ return id.to_s unless ids.include?(id.to_s)
171
+ id += 1
172
+ end
173
+ end
174
+
161
175
  # @return [AugeasCollection] collection for *key*
162
176
  def collection(key)
163
177
  AugeasCollection.new(self, key)
@@ -217,7 +231,7 @@ module CFA
217
231
  # @param matcher [Matcher]
218
232
  # @return [Array<AugeasElement>] matching elements
219
233
  def select(matcher)
220
- @data.select(&matcher)
234
+ data.select(&matcher)
221
235
  end
222
236
 
223
237
  def ==(other)
@@ -184,7 +184,7 @@ module CFA
184
184
  # one after another then the latter cannot affect the former
185
185
  @operations.reverse_each do |operation|
186
186
  case operation[:type]
187
- when :remove then aug.rm(operation[:path])
187
+ when :remove then remove_entry(operation[:path])
188
188
  when :add
189
189
  located_entry = operation[:located_entry]
190
190
  add_entry(located_entry)
@@ -198,6 +198,28 @@ module CFA
198
198
 
199
199
  attr_reader :aug
200
200
 
201
+ # Removes entry from tree. If *path* does not exist, then tries if it
202
+ # has changed to a collection:
203
+ # If we remove and re-add a single key then because of the laziness
204
+ # Augeas will first see the addition, making a 2 member collection,
205
+ # so we need to remove "key[1]" instead of "key".
206
+ # @param path [String] original path name to remove
207
+ def remove_entry(path)
208
+ aug.rm(path_to_remove(path))
209
+ end
210
+
211
+ # Finds path to remove, as path can be meanwhile renumbered, see
212
+ # #remove_entry
213
+ def path_to_remove(path)
214
+ if aug.match(path).size == 1
215
+ path
216
+ elsif !aug.match(path + "[1]").empty?
217
+ path + "[1]"
218
+ else
219
+ raise "Unknown augeas path #{path}"
220
+ end
221
+ end
222
+
201
223
  # Adds entry to tree. At first it finds where to add it to be in correct
202
224
  # place and then sets its value. Recursive if needed. In recursive case
203
225
  # it is already known that whole sub-tree is also new and just added.
@@ -214,11 +236,11 @@ module CFA
214
236
  # @see https://github.com/hercules-team/augeas/wiki/Path-expressions
215
237
  def set_new_value(path, located_entry)
216
238
  aug.set(path, located_entry.entry_value)
217
- prefix = path[/(^.*)\/[^\/]+/, 1]
239
+ prefix = path[/(^.*)\[[^\]]*\]/, 1] || path
218
240
  # we need to get new path as set can look like [last() + 1]
219
241
  # which creates new entry and we do not want to add subtree to new
220
242
  # entries
221
- new_path = aug.match(prefix + "/*[last()]").first
243
+ new_path = aug.match(prefix + "[last()]").first
222
244
  add_subtree(located_entry.entry_tree, new_path)
223
245
  end
224
246
 
@@ -267,9 +289,19 @@ module CFA
267
289
  # @return [String] where value should be written.
268
290
  def insert_after(preceding, located_entry)
269
291
  aug.insert(preceding.path, located_entry.key, false)
270
- paths = aug.match(located_entry.prefix + "/*")
271
- paths_index = paths.index(preceding.path) + 1
272
- paths[paths_index]
292
+ path_after(preceding)
293
+ end
294
+
295
+ # Finds path immediately after preceding entry
296
+ # @param preceding [LocatedEntry]
297
+ def path_after(preceding)
298
+ paths = aug.match(preceding.prefix + "/*")
299
+ preceding_index = paths.index(preceding.path)
300
+ # it can happen, that insertion change previous entry from
301
+ # e.g. #comment to #comment[1]. Can happen only if it switch from
302
+ # single entry to collection
303
+ preceding_index ||= paths.index(preceding.path + "[1]")
304
+ paths[preceding_index + 1]
273
305
  end
274
306
  end
275
307
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josef Reidinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-20 00:00:00.000000000 Z
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-augeas
@@ -61,10 +61,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: 1.3.6
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.2.2
64
+ rubygems_version: 2.4.5.2
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: CFA (Config Files API) provides an easy way to create models on top of configuration
68
68
  files
69
69
  test_files: []
70
- has_rdoc: