Class: XPath::XPathNodeSet

Inherits:
Object
  • Object
show all
Includes:
XPathObject
Defined in:
lib/xml/xpath.rb

Direct Known Subclasses

LocationStep

Defined Under Namespace

Classes: LocationStep

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XPathObject

#<, #<=, #==, #>, #>=, #to_boolean, #to_number, #to_predicate, #to_string

Constructor Details

#initialize(context, *nodes) ⇒ XPathNodeSet

Returns a new instance of XPathNodeSet.



2746
2747
2748
2749
2750
2751
# File 'lib/xml/xpath.rb', line 2746

def initialize(context, *nodes)
  @context = context.dup
  @visitor = context.visitor
  nodes.sort! { |a,b| compare_position a, b }
  @nodes = nodes
end

Class Method Details

.def_comparison_operator(*ops) ⇒ Object



2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
# File 'lib/xml/xpath.rb', line 2778

def self.def_comparison_operator(*ops)
  ops.each { |op|
    module_eval <<_, __FILE__, __LINE__ + 1
    def #{op}(other)
      if other.is_a? XPathBoolean then
        other #{op} self.to_boolean
      else
        visitor = @visitor
        str = @context.make_string('')
        ret = false
        @nodes.each { |node|
          str.replace visitor.visit(node).string_value
          break if ret = (other #{op} str)
        }
        ret
      end
    end
_
  }
end

Instance Method Details

#**(other) ⇒ Object



2805
2806
2807
2808
2809
# File 'lib/xml/xpath.rb', line 2805

def **(other)
  super unless other.is_a? XPathNodeSet
  merge other.nodes
  self
end

#at(pos) ⇒ Object



2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
# File 'lib/xml/xpath.rb', line 2997

def at(pos)
  n = pos.to_i
  if n != pos or n <= 0 then
    node = nil
  else
    node = @nodes[n - 1]
  end
  @nodes.clear
  @nodes.push node if node
  self
end

#countObject



2812
2813
2814
# File 'lib/xml/xpath.rb', line 2812

def count
  @nodes.size
end

#each(&block) ⇒ Object



2820
2821
2822
# File 'lib/xml/xpath.rb', line 2820

def each(&block)
  @nodes.each(&block)
end

#firstObject



2816
2817
2818
# File 'lib/xml/xpath.rb', line 2816

def first
  @nodes[0]
end

#funcall(name) ⇒ Object

for XPointer



2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
# File 'lib/xml/xpath.rb', line 2825

def funcall(name)   # for XPointer
  raise "BUG" unless block_given?
  func = ('f_' + name.tr('-', '_')).intern
  super unless respond_to? func, true
  size = @nodes.size
  pos = 1
  c = @context.dup
  begin
    @nodes.collect! { |node|
      c.reuse node, pos, size
      pos += 1
      args = yield(c)
      send(func, node, *args)
    }
  rescue Object::ArgumentError
    if $@[1] == "#{__FILE__}:#{__LINE__-3}:in `send'" then
      raise XPath::ArgumentError, "#{$!} for `#{name}'"
    end
    raise
  end
  self
end

#predicateObject



2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
# File 'lib/xml/xpath.rb', line 2976

def predicate
  context = @context
  size = @nodes.size
  pos = 1
  result = nil
  newnodes = @nodes.reject { |node|
    context.reuse node, pos, size
    pos += 1
    result = yield(context)
    break if result.is_a? Numeric
    not result
  }
  if result.is_a? Numeric then
    at result
  else
    @nodes = newnodes
  end
  self
end

#select_all(axis) ⇒ Object



2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
# File 'lib/xml/xpath.rb', line 2961

def select_all(axis)
  iterator = get_iterator(axis)
  visitor = @visitor
  oldnodes = @nodes
  @nodes = []
  oldnodes.each { |start|
    nodes = []
    iterator.each(start, visitor) { |i| nodes.push i.node }
    nodes.reverse! if iterator.reverse_order?
    merge nodes
  }
  self
end

#step(axis) ⇒ Object



2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
# File 'lib/xml/xpath.rb', line 2945

def step(axis)
  iterator = get_iterator(axis)
  lstep = make_location_step
  lstep.set_iterator iterator
  oldnodes = @nodes
  @nodes = []
  oldnodes.each { |node|
    lstep.reuse node
    nodes = yield(lstep).nodes
    nodes.reverse! if iterator.reverse_order?
    merge nodes
  }
  self
end

#to_fObject



2765
2766
2767
# File 'lib/xml/xpath.rb', line 2765

def to_f
  to_string(@context).to_f
end

#to_rubyObject



2773
2774
2775
# File 'lib/xml/xpath.rb', line 2773

def to_ruby
  @nodes
end

#to_strObject



2757
2758
2759
2760
2761
2762
2763
# File 'lib/xml/xpath.rb', line 2757

def to_str
  if @nodes.empty? then
    ''
  else
    @visitor.visit(@nodes[0]).string_value
  end
end

#true?Boolean

Returns:

  • (Boolean)


2769
2770
2771
# File 'lib/xml/xpath.rb', line 2769

def true?
  not @nodes.empty?
end