Parent

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 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
34:     def initialize( config )
35:       @config = config
36:       @stack = []
37:       reset
38:     end

Public Instance Methods

each( attribute = nil ) 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
49:     def each( attribute = nil )
50:       reset
51:       rv = nil
52: 
53:       if attribute and !attribute.empty?
54:         node = while (n = next_node) do
55:                  break n if n.name == attribute
56:                end
57:         return if node.nil?
58: 
59:         rv = yield node
60:         return rv unless node.config?
61: 
62:         stack.clear
63:         stack << new_frame(node.obj, node.name) if node.config?
64:       end
65: 
66:       while (node = next_node) do
67:         rv = yield node
68:       end
69:       return rv
70:     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
76:     def find( attribute )
77:       attribute = attribute.to_s
78:       return if attribute.empty?
79: 
80:       node = self.each {|n| break n if n.name == attribute}
81:       reset
82:       return node
83:     end

Private Instance Methods

new_frame( cfg, prefix = nil ) click to toggle source

Create a new stack frame from the given cfg (configuration object) and the optional prefix. The prefix is used to complete the full name for each attribute key in the configuration object.

     # File lib/loquacious/configuration/iterator.rb, line 118
118:     def new_frame( cfg, prefix = nil )
119:       keys = cfg.__desc.keys.map {|k| k.to_s}
120:       keys.sort!
121:       keys.map! {|k| k.to_sym}
122: 
123:       Frame.new(cfg, prefix.to_s, keys, 0)
124:     end
new_node( frame ) click to toggle source

Create the next iteration node from the given stack frame. Returns nil when there are no more nodes in the frame.

     # File lib/loquacious/configuration/iterator.rb, line 129
129:     def new_node( frame )
130:       key = frame.keys[frame.index]
131:       return if key.nil?
132: 
133:       cfg = frame.config
134:       name = frame.prefix.empty? ? key.to_s : frame.prefix + ".#{key}"
135:       Node.new(cfg, name, cfg.__desc[key], key)
136:     end
next_node() click to toggle source

Returns the next node from the current iteration stack frame. Returns nil if there are no more nodes in the iterator.

     # File lib/loquacious/configuration/iterator.rb, line 97
 97:     def next_node
 98:       frame = stack.last
 99:       node = new_node(frame)
100: 
101:       while node.nil?
102:         stack.pop
103:         return if stack.empty?
104:         frame = stack.last
105:         node = new_node(frame)
106:       end
107: 
108:       frame.index += 1
109:       stack << new_frame(node.obj, node.name) if node.config?
110: 
111:       return node
112:     end
reset() click to toggle source

Reset the iterator back to the beginning.

    # File lib/loquacious/configuration/iterator.rb, line 89
89:     def reset
90:       stack.clear
91:       stack << new_frame(@config)
92:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.