Class: XML::XXPath::AttrNameStep

Inherits:
Step
  • Object
show all
Defined in:
lib/xml/xxpath/steps.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Step

#creator, inherited

Constructor Details

#initialize(axis, attr_name) ⇒ AttrNameStep

Returns a new instance of AttrNameStep.



204
205
206
207
# File 'lib/xml/xxpath/steps.rb', line 204

def initialize(axis,attr_name)
  super(axis)
  @attr_name = attr_name
end

Class Method Details

.compile(axis, string) ⇒ Object



199
200
201
202
# File 'lib/xml/xxpath/steps.rb', line 199

def self.compile axis, string
  /^@(.*)$/ === string or return nil
  self.new axis,$1
end

Instance Method Details

#create_on(node, create_new) ⇒ Object



213
214
215
216
217
218
# File 'lib/xml/xxpath/steps.rb', line 213

def create_on(node,create_new)
  if create_new and node.attributes[@attr_name]
    raise XXPathError, "XPath (@#{@attr_name}): create_new and attribute already exists"
  end
  XML::XXPath::Accessors::Attribute.new(node,@attr_name,true)
end

#matches?(node) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/xml/xxpath/steps.rb', line 209

def matches? node
  node.class==XML::XXPath::Accessors::Attribute and node.name==@attr_name
end

#reader(prev_reader, creator_from_here) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/xml/xxpath/steps.rb', line 220

def reader(prev_reader,creator_from_here)
  if @axis==:child
    proc {|nodes|
      next_nodes = []
      nodes.each do |node|
        attr=XML::XXPath::Accessors::Attribute.new(node,@attr_name,false)
        next_nodes << attr if attr
      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