Class: XML::XXPath::AttrStep

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, #reader

Constructor Details

#initialize(axis, attr_name, attr_value) ⇒ AttrStep

Returns a new instance of AttrStep.



98
99
100
101
# File 'lib/xml/xxpath/steps.rb', line 98

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

Class Method Details

.compile(axis, string) ⇒ Object



93
94
95
96
# File 'lib/xml/xxpath/steps.rb', line 93

def self.compile axis, string
  /^(?:\.|self::\*)\[@(.*?)='(.*?)'\]$/ === string or return nil
  self.new axis,$1,$2
end

Instance Method Details

#create_on(node, create_new) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/xml/xxpath/steps.rb', line 107

def create_on(node,create_new)
  if create_new
    raise XXPathError, "XPath: .[@'#{@attr_name}'='#{@attr_value}']: create_new but context node already exists"
  end
  # TODO: raise if node.attributes[@attr_name] already exists?
  node.attributes[@attr_name]=@attr_value
  node
end

#matches?(node) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/xml/xxpath/steps.rb', line 103

def matches? node
  node.is_a?(REXML::Element) and node.attributes[@attr_name]==@attr_value
end