Class: ETL::Parser::XPath::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/etl/parser/sax_parser.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePath

Initialize



137
138
139
# File 'lib/etl/parser/sax_parser.rb', line 137

def initialize
  @elements = []
end

Instance Attribute Details

#elementsObject

Get the elements in the path



134
135
136
# File 'lib/etl/parser/sax_parser.rb', line 134

def elements
  @elements
end

Class Method Details

.parse(s) ⇒ Object

Parse the string into an XPath::Path object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/etl/parser/sax_parser.rb', line 178

def self.parse(s)
  return s if s.is_a?(Path)
  path = Path.new
  parts = s.split('/')
  parts.each_with_index do |part, i|
    attributes = {}
    part.gsub!(/(.*)\[(.*)\]/, '\1')
    if !$2.nil?
      $2.split(",").each do |pair|
        key, value = pair.split("=")
        value = ".*" if value.nil?
        attributes[key] = Regexp.new(value)
      end
    end
    path.elements << Element.new(part, attributes)
  end
  path
end

Instance Method Details

#attributeObject

Return the name of the attribute referenced by the last element in this path. Returns nil if the last element does not reference an attribute.

Warning: the path must only reference a single attribute, otherwise the result of this method will be random, since attributes are stored in a Hash.



156
157
158
159
# File 'lib/etl/parser/sax_parser.rb', line 156

def attribute
  return nil unless is_attribute?
  elements.last.attributes.keys.first
end

#is_attribute?Boolean

Returns true if the last part of the path refers to an attribute

Returns:

  • (Boolean)


147
148
149
# File 'lib/etl/parser/sax_parser.rb', line 147

def is_attribute?
  elements.last.attributes.length > 0
end

#match?(s) ⇒ Boolean

Return true if this XPath::Path matches the given path string. This is a fail-fast match, so the first mismatch will cause the method to return false.

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/etl/parser/sax_parser.rb', line 163

def match?(s)
  path = Path.parse(s)
  return false unless path.elements.length == elements.length
  elements.each_with_index do |element, index|
    path_element = path.elements[index]
    return false if path_element.nil?
    return false if element.name != path_element.name
    path_element.attributes.each do |key, value|
      return false unless element.attributes[key] =~ value
    end
  end
  return true
end

#to_sObject

Convert to a string representation



142
143
144
# File 'lib/etl/parser/sax_parser.rb', line 142

def to_s
  @elements.map{ |e| e.to_s }.join("/")
end