Class: XML::XXPath::NameAndIndexStep
- Defined in:
- lib/xml/xxpath/steps.rb
Overview
:nodoc:
Class Method Summary collapse
Instance Method Summary collapse
- #create_on(node, create_new) ⇒ Object
-
#initialize(axis, name, index) ⇒ NameAndIndexStep
constructor
A new instance of NameAndIndexStep.
- #matches?(node) ⇒ Boolean
- #reader(prev_reader, creator_from_here) ⇒ Object
Methods inherited from Step
Constructor Details
#initialize(axis, name, index) ⇒ NameAndIndexStep
Returns a new instance of NameAndIndexStep.
154 155 156 157 |
# File 'lib/xml/xxpath/steps.rb', line 154 def initialize(axis,name,index) super(axis) @name,@index = name,index end |
Class Method Details
.compile(axis, string) ⇒ Object
149 150 151 152 |
# File 'lib/xml/xxpath/steps.rb', line 149 def self.compile axis, string /^(.*?)\[(\d+)\]$/ === string or return nil self.new axis,$1,$2.to_i end |
Instance Method Details
#create_on(node, create_new) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/xml/xxpath/steps.rb', line 164 def create_on(node,create_new) name_matches = node.elements.select{|elt| elt.name==@name} if create_new and (name_matches.size >= @index) raise XXPathError, "XPath: #{@name}[#{@index}]: create_new and element already exists" end newnode = name_matches[0] (@index-name_matches.size).times do newnode = node.elements.add @name end newnode end |
#matches?(node) ⇒ Boolean
159 160 161 162 |
# File 'lib/xml/xxpath/steps.rb', line 159 def matches? node raise XXPathError, "can't use #{@name}[#{@index}] on root node" if node.parent.nil? node == node.parent.elements.select{|elt| elt.name==@name}[@index-1] end |
#reader(prev_reader, creator_from_here) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/xml/xxpath/steps.rb', line 176 def reader(prev_reader,creator_from_here) if @axis==:child index = @index - 1 proc {|nodes| next_nodes = [] nodes.each do |node| byname=node.elements.select{|elt| elt.name==@name} next_nodes << byname[index] if index<byname.size end if (next_nodes.empty?) throw :not_found, [nodes,creator_from_here] else prev_reader.call(next_nodes) end } else super(prev_reader,creator_from_here) end end |