class CFA::AugeasWriter::LocatedEntry

{AugeasElement} together with information about its location and a few helper methods to detect siblings.

@example data for an already existing comment living under /main

entry.orig_key # => "#comment[15]"
entry.path # => "/main/#comment[15]"
entry.key # => "#comment"
entry.entry_tree # => AugeasTree.new
entry.entry_value # => "old boring comment"

@example data for a new comment under /main

entry.orig_key # => nil
entry.path # => nil
entry.key # => "#comment"
entry.entry_tree # => AugeasTree.new
entry.entry_value # => "new boring comment"

@example data for new tree placed at /main

entry.orig_key # => "main"
entry.path # => "/main"
entry.key # => "main"
entry.entry_tree # => entry[:value]
entry.entry_value # => nil

Attributes

entry[R]
prefix[R]
tree[R]

Public Class Methods

new(tree, entry, prefix) click to toggle source
# File lib/cfa/augeas_parser/writer.rb, line 62
def initialize(tree, entry, prefix)
  @tree = tree
  @entry = entry
  @prefix = prefix
  detect_tree_value_modification
end

Public Instance Methods

any_following?() click to toggle source

@return [true, false] returns true if there is any following entry

in the Augeas tree
# File lib/cfa/augeas_parser/writer.rb, line 103
def any_following?
  following_entries.any? { |e| e[:operation] != :remove }
end
entry_tree() click to toggle source

@return [AugeasTree] the Augeas tree nested under this entry.

If there is no such tree, it creates an empty one.
# File lib/cfa/augeas_parser/writer.rb, line 109
def entry_tree
  value = entry[:value]
  case value
  when AugeasTree then value
  when AugeasTreeValue then value.tree
  else AugeasTree.new
  end
end
entry_value() click to toggle source

@return [String, nil] the Augeas value of this entry. Can be nil. If the value is an {AugeasTree} then return nil.

# File lib/cfa/augeas_parser/writer.rb, line 120
def entry_value
  value = entry[:value]
  case value
  when AugeasTree then nil
  when AugeasTreeValue then value.value
  else value
  end
end
key() click to toggle source
# File lib/cfa/augeas_parser/writer.rb, line 80
def key
  return @key if @key

  @key = @entry[:key]
  @key = @key[0..-3] if @key.end_with?("[]")
  @key
end
orig_key() click to toggle source
# File lib/cfa/augeas_parser/writer.rb, line 69
def orig_key
  entry[:orig_key]
end
path() click to toggle source
# File lib/cfa/augeas_parser/writer.rb, line 73
def path
  return @path if @path
  return nil unless orig_key

  @path = @prefix + "/" + orig_key
end
preceding_existing() click to toggle source

@return [LocatedEntry, nil]

a preceding entry that already exists in the Augeas tree
or nil if it does not exist.
# File lib/cfa/augeas_parser/writer.rb, line 91
def preceding_existing
  preceding_entry = preceding_entries.reverse_each.find do |entry|
    entry[:operation] != :add
  end

  return nil unless preceding_entry

  LocatedEntry.new(tree, preceding_entry, prefix)
end

Private Instance Methods

detect_tree_value_modification() click to toggle source

For {AugeasTreeValue} we have a problem with detection of value modification as it is enclosed in a diferent object. So propagate it to this entry here.

# File lib/cfa/augeas_parser/writer.rb, line 134
def detect_tree_value_modification
  return unless entry[:value].is_a?(AugeasTreeValue)
  return if entry[:operation] != :keep

  entry[:operation] = entry[:value].modified? ? :modify : :keep
end
following_entries() click to toggle source

the entries following this entry

# File lib/cfa/augeas_parser/writer.rb, line 149
def following_entries
  tree.all_data[(index + 1)..-1]
end
index() click to toggle source

the index of this entry in its tree

# File lib/cfa/augeas_parser/writer.rb, line 154
def index
  @index ||= tree.all_data.index(entry)
end
preceding_entries() click to toggle source

the entries preceding this entry

# File lib/cfa/augeas_parser/writer.rb, line 142
def preceding_entries
  return [] if index.zero? # first entry

  tree.all_data[0..(index - 1)]
end