Module: ReDuxml

Includes:
ConDuxml
Included in:
Evaluator
Defined in:
lib/re_duxml.rb,
lib/re_duxml/evaluate.rb,
lib/re_duxml/evaluate/parser.rb

Defined Under Namespace

Classes: Evaluator, Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eObject (readonly)

Returns the value of attribute e.



46
47
48
# File 'lib/re_duxml.rb', line 46

def e
  @e
end

#src_docObject (readonly)

Returns the value of attribute src_doc.



46
47
48
# File 'lib/re_duxml.rb', line 46

def src_doc
  @src_doc
end

Instance Method Details

#resolve(doc_or_node, parent_params = {}) ⇒ Doc

Returns instantiated XML with parameters resolved with given values.

Parameters:

  • doc_or_node (Doc, String)

    XML to load; can be Doc, String path or String XML; also XML Element when recursing

Returns:

  • (Doc)

    instantiated XML with parameters resolved with given values



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/re_duxml.rb', line 13

def resolve(doc_or_node, parent_params={})
  @e ||= Evaluator.new
  if doc_or_node.is_a?(Element)
    resolved_node = doc_or_node.stub
    resolved_node.attributes.each do |attr, val|
      resolved_node[attr] = resolve_str(val, parent_params)
    end
    this_params = get_params(doc_or_node, parent_params)
    new_children = doc_or_node.nodes.collect do |child|
      if child.respond_to?(:nodes) # if this node is an XML element...
        new_child = child.clone
        new_child[:if] = resolve_str(new_child[:if], this_params) if new_child[:if]
        if new_child.if? # if this node should exist
          new_child[:if] = nil if new_child[:if] == 'true'
          child_params = get_params(new_child, this_params)
          new_instances = new_child.activate
          i = -1
          new_instances.collect do |inst|
            i += 1
            resolve(inst, child_params.merge({'iterator' => i.to_s}))
          end
        end
      else # this is a text node
        resolve_str(child, this_params)
      end
    end.flatten.compact
    resolved_node << new_children
  else
    @src_doc = get_doc doc_or_node
    @doc = Doc.new << resolve(src_doc.root, parent_params)
  end
end