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
].freeze
OPERATORS =
[
  %i[equals = ==],
  %i[or or |],
  %i[and and &],
  %i[not_equals != !=],
  %i[lte <= <=],
  %i[lt < <],
  %i[gte >= >=],
  %i[gt > >],
  %i[plus +],
  %i[minus -],
  %i[multiply * *],
  %i[divide div /],
  %i[mod mod %]
].freeze
AXES =
%i[
  ancestor ancestor_or_self attribute descendant_or_self
  following following_sibling namespace parent preceding
  preceding_sibling self
].freeze
UPPERCASE_LETTERS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ'
LOWERCASE_LETTERS =
'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ'

Instance Method Summary collapse

Instance Method Details

#anywhere(*expressions) ⇒ Object



21
22
23
# File 'lib/xpath/dsl.rb', line 21

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

#attr(expression) ⇒ Object



25
26
27
# File 'lib/xpath/dsl.rb', line 25

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

#axis(name, *element_names) ⇒ Object



17
18
19
# File 'lib/xpath/dsl.rb', line 17

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

#binary_operator(name, rhs) ⇒ Object



58
59
60
# File 'lib/xpath/dsl.rb', line 58

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

#child(*expressions) ⇒ Object



13
14
15
# File 'lib/xpath/dsl.rb', line 13

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

#contains_word(word) ⇒ Object



147
148
149
# File 'lib/xpath/dsl.rb', line 147

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

#css(selector) ⇒ Object



33
34
35
# File 'lib/xpath/dsl.rb', line 33

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

#currentObject



5
6
7
# File 'lib/xpath/dsl.rb', line 5

def current
  Expression.new(:this_node)
end

#descendant(*expressions) ⇒ Object



9
10
11
# File 'lib/xpath/dsl.rb', line 9

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

#ends_with(suffix) ⇒ Object



143
144
145
# File 'lib/xpath/dsl.rb', line 143

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

#function(name, *arguments) ⇒ Object



37
38
39
# File 'lib/xpath/dsl.rb', line 37

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

#is(expression) ⇒ Object



54
55
56
# File 'lib/xpath/dsl.rb', line 54

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

#lastObject



67
68
69
# File 'lib/xpath/dsl.rb', line 67

def last
  function(:last)
end

#lowercaseObject



154
155
156
# File 'lib/xpath/dsl.rb', line 154

def lowercase
  method(:translate, UPPERCASE_LETTERS, LOWERCASE_LETTERS)
end

#method(name, *arguments) ⇒ Object



41
42
43
# File 'lib/xpath/dsl.rb', line 41

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

#next_sibling(*expressions) ⇒ Object



166
167
168
# File 'lib/xpath/dsl.rb', line 166

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

#one_of(*expressions) ⇒ Object



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

def one_of(*expressions)
  expressions.map { |e| current.equals(e) }.reduce(:or)
end

#positionObject



71
72
73
# File 'lib/xpath/dsl.rb', line 71

def position
  function(:position)
end

#previous_sibling(*expressions) ⇒ Object



170
171
172
# File 'lib/xpath/dsl.rb', line 170

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

#qnameObject



95
96
97
# File 'lib/xpath/dsl.rb', line 95

def qname
  method(:name)
end

#textObject



29
30
31
# File 'lib/xpath/dsl.rb', line 29

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

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



62
63
64
# File 'lib/xpath/dsl.rb', line 62

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

#uppercaseObject



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

def uppercase
  method(:translate, LOWERCASE_LETTERS, UPPERCASE_LETTERS)
end

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



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

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