Class: XmlNodeStream::Selector::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_node_stream/selector.rb

Overview

Match a partial path to a node.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Matcher

Returns a new instance of Matcher.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xml_node_stream/selector.rb', line 47

def initialize (path)
  case path
  when 'text()'
    @extractor = lambda{|node| node.value}
  when '%'
    @extractor = lambda{|node| node.descendants}
  when '*'
    @extractor = lambda{|node| node.children}
  when '.'
    @extractor = lambda{|node| node}
  when '..'
    @extractor = lambda{|node| node.parent ? node.parent : []}
  when ''
    @extractor = lambda{|node| root = Node.new(nil); root.children << node.root; root}
  else
    @extractor = lambda{|node| node.children.select{|child| child.name == path}}
  end
end

Instance Method Details

#select(context_nodes) ⇒ Object

Select all nodes that match a partial path.



67
68
69
# File 'lib/xml_node_stream/selector.rb', line 67

def select (context_nodes)
  context_nodes.collect{|node| @extractor.call(node) if node.is_a?(Node)}.flatten
end