Module: LibXML::XML::XQuery::Uniqueness

Extended by:
Forwardable
Defined in:
lib/libxml/xquery/uniqueness.rb

Constant Summary collapse

DELEGATORS =
%w[to_a each length size]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/libxml/xquery/uniqueness.rb', line 79

def self.included(base)
  base.send(:alias_method, :_xquery_original_to_a, :to_a)

  # overwrite original methods
  instance_methods.each { |method|
    base.send(:define_method, method, instance_method(method))
  }
end

Instance Method Details

#to_aObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libxml/xquery/uniqueness.rb', line 37

def to_a
  if @_uniq
    # return unique nodes according to their #to_s
    seen = Hash.new { |h, k| h[k] = true; false }
    uniq = []

    _xquery_original_to_a.each { |n|
      uniq << n unless seen[n.to_s]
    }

    uniq
  else
    _xquery_original_to_a
  end
end

#uniqObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/libxml/xquery/uniqueness.rb', line 53

def uniq
  proxy = self.class.new.uniq!
  proxy.instance_variable_set(:@_this, self)

  class << proxy
    (instance_methods.map { |m| m.to_s } - DELEGATORS).each { |method|
      undef_method(method) unless method =~ /\A__/ || method == 'object_id'
    }

    def method_missing(*args)
      block_given? ? @_this.send(*args) { |a| yield a } : @_this.send(*args)
    end
  end

  proxy
end

#uniq!Object



70
71
72
73
# File 'lib/libxml/xquery/uniqueness.rb', line 70

def uniq!
  @_uniq = true
  self
end

#uniq?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/libxml/xquery/uniqueness.rb', line 75

def uniq?
  @_uniq
end