Module: XPath::DSL

Included in:
XPath, XPath, Expression
Defined in:
lib/xpath/dsl.rb

Constant Summary collapse

METHODS =
[
  # node set
  :count, :id, :local_name, :namespace_uri,
  # string
  :string, :concat, :starts_with, :contains, :substring_before,
  :substring_after, :substring, :string_length, :normalize_space,
  :translate,
  # boolean
  :boolean, :not, :true, :false, :lang,
  # number
  :number, :sum, :floor, :ceiling, :round,
]
OPERATORS =
[
  [:equals, :"=", :==],
  [:or, :or, :|],
  [:and, :and, :&],
  [:not_equals, :!=, :!=],
  [:lte, :<=, :<=],
  [:lt, :<, :<],
  [:gte, :>=, :>=],
  [:gt, :>, :>],
  [:plus, :+],
  [:minus, :-],
  [:multiply, :*, :*],
  [:divide, :div, :/],
  [:mod, :mod, :%],
]
AXES =
[
  :ancestor, :ancestor_or_self, :attribute, :descendant_or_self,
  :following, :following_sibling, :namespace, :parent, :preceding,
  :preceding_sibling, :self,
]

Instance Method Summary collapse

Instance Method Details

#anywhere(*expressions) ⇒ Object



19
20
21
# File 'lib/xpath/dsl.rb', line 19

def anywhere(*expressions)
  Expression.new(:anywhere, expressions)
end

#attr(expression) ⇒ Object



23
24
25
# File 'lib/xpath/dsl.rb', line 23

def attr(expression)
  Expression.new(:attribute, current, expression)
end

#axis(name, *element_names) ⇒ Object



15
16
17
# File 'lib/xpath/dsl.rb', line 15

def axis(name, *element_names)
  Expression.new(:axis, current, name, element_names)
end

#binary_operator(name, rhs) ⇒ Object



56
57
58
# File 'lib/xpath/dsl.rb', line 56

def binary_operator(name, rhs)
  Expression.new(:binary_operator, name, current, rhs)
end

#child(*expressions) ⇒ Object



11
12
13
# File 'lib/xpath/dsl.rb', line 11

def child(*expressions)
  Expression.new(:child, current, expressions)
end

#contains_word(word) ⇒ Object



145
146
147
# File 'lib/xpath/dsl.rb', line 145

def contains_word(word)
  function(:concat, " ", current.normalize_space, " ").contains(" #{word} ")
end

#css(selector) ⇒ Object



31
32
33
# File 'lib/xpath/dsl.rb', line 31

def css(selector)
  Expression.new(:css, current, Literal.new(selector))
end

#currentObject



3
4
5
# File 'lib/xpath/dsl.rb', line 3

def current
  Expression.new(:this_node)
end

#descendant(*expressions) ⇒ Object



7
8
9
# File 'lib/xpath/dsl.rb', line 7

def descendant(*expressions)
  Expression.new(:descendant, current, expressions)
end

#ends_with(suffix) ⇒ Object



141
142
143
# File 'lib/xpath/dsl.rb', line 141

def ends_with(suffix)
  function(:substring, current, function(:'string-length', current).minus(function(:'string-length', suffix)).plus(1)) == suffix
end

#function(name, *arguments) ⇒ Object



35
36
37
# File 'lib/xpath/dsl.rb', line 35

def function(name, *arguments)
  Expression.new(:function, name, *arguments)
end

#is(expression) ⇒ Object



52
53
54
# File 'lib/xpath/dsl.rb', line 52

def is(expression)
  Expression.new(:is, current, expression)
end

#lastObject



65
66
67
# File 'lib/xpath/dsl.rb', line 65

def last
  function(:last)
end

#method(name, *arguments) ⇒ Object



39
40
41
# File 'lib/xpath/dsl.rb', line 39

def method(name, *arguments)
  Expression.new(:function, name, current, *arguments)
end

#next_sibling(*expressions) ⇒ Object



157
158
159
# File 'lib/xpath/dsl.rb', line 157

def next_sibling(*expressions)
  axis(:"following-sibling")[1].axis(:self, *expressions)
end

#one_of(*expressions) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/xpath/dsl.rb', line 149

def one_of(*expressions)
  expressions.map do |e|
    current.equals(e)
  end.reduce do |a, b|
    a.or(b)
  end
end

#positionObject



69
70
71
# File 'lib/xpath/dsl.rb', line 69

def position
  function(:position)
end

#previous_sibling(*expressions) ⇒ Object



161
162
163
# File 'lib/xpath/dsl.rb', line 161

def previous_sibling(*expressions)
  axis(:"preceding-sibling")[1].axis(:self, *expressions)
end

#qnameObject



93
94
95
# File 'lib/xpath/dsl.rb', line 93

def qname
  method(:name)
end

#textObject



27
28
29
# File 'lib/xpath/dsl.rb', line 27

def text
  Expression.new(:text, current)
end

#union(*expressions) ⇒ Object Also known as: +



60
61
62
# File 'lib/xpath/dsl.rb', line 60

def union(*expressions)
  Union.new(*[self, expressions].flatten)
end

#where(expression) ⇒ Object Also known as: []



43
44
45
46
47
48
49
# File 'lib/xpath/dsl.rb', line 43

def where(expression)
  if expression
    Expression.new(:where, current, expression)
  else
    current
  end
end