Class: Loquacious::Configuration::Iterator
- Inherits:
-
Object
- Object
- Loquacious::Configuration::Iterator
- Defined in:
- lib/loquacious/configuration/iterator.rb
Overview
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') {
'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
Defined Under Namespace
Instance Method Summary collapse
-
#each(attribute = nil) ⇒ Object
Iterate over each node in the configuration object yielding each to the supplied block in turn.
-
#find(attribute) ⇒ Object
Find the given named attribute in the iterator.
-
#initialize(config) ⇒ Iterator
constructor
Create a new iterator that will operate on the config (configuration object).
Constructor Details
#initialize(config) ⇒ Iterator
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.
34 35 36 37 38 |
# File 'lib/loquacious/configuration/iterator.rb', line 34 def initialize( config ) @config = config @stack = [] reset end |
Instance Method Details
#each(attribute = nil) ⇒ Object
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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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) ⇒ Object
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.
76 77 78 79 80 81 82 83 |
# 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 |