class Loquacious::Configuration::Iterator

Provides an external iteraotr for a Loquacious::Configuration object. The iterator allows the user to retrieve all the configuration settings along with their descriptions and values.

cfg = Configuration.for('foo') {
  bar  'value', :desc => 'the bar attribute'
  baz  42,      :desc => 'the baz attribute'
}

i = Iterator.new(cfg)
i.each do |node|
  puts "#{node.name} :: #{node.desc}"
end

Results in

bar :: the bar attribute
baz :: the baz attribute

Constants

Frame

Structure describing a single iteration stack frame. A new stack frame is created when we descend into a nested Configuration object.

Node

This is a single node in a Configuration object. It corresponds to a single configuration attribute.

Public Instance Methods

each( attribute = nil ) { |node| ... } click to toggle source

Iterate over each node in the configuration object yielding each to the supplied block in turn. The return value of the block is returned from this method. nil is returned if there are no nodes in the iterator.

If an attribute is given, then the iteration starts at that particular attribute and recurse if it is a nested configuration. Otherwise, only that attribute is yielded to the block.

# File lib/loquacious/configuration/iterator.rb, line 49
def each( attribute = nil )
  reset
  rv = nil

  if attribute and !attribute.empty?
    node = while (n = next_node) do
             break n if n.name == attribute
           end
    return if node.nil?

    rv = yield node
    return rv unless node.config?

    stack.clear
    stack << new_frame(node.obj, node.name) if node.config?
  end

  while (node = next_node) do
    rv = yield node
  end
  return rv
end
find( attribute ) click to toggle source

Find the given named attribute in the iterator. Returns a node representing the attribute; or nil is returned if the named attribute could not be found.

# File lib/loquacious/configuration/iterator.rb, line 76
def find( attribute )
  attribute = attribute.to_s
  return if attribute.empty?

  node = self.each {|n| break n if n.name == attribute}
  reset
  return node
end

Public Class Methods

new( config ) click to toggle source

Create a new iterator that will operate on the config (configuration object). The iterator allows the attributes of the configuration object to be accessed – this includes nested configuration objects.

# File lib/loquacious/configuration/iterator.rb, line 34
def initialize( config )
  @config = config
  @stack = []
  reset
end