Module: Ldpath::Functions

Included in:
Result
Defined in:
lib/ldpath/functions.rb

Instance Method Summary collapse

Instance Method Details

#concat(_uri, _context, *args) ⇒ Object



4
5
6
# File 'lib/ldpath/functions.rb', line 4

def concat(_uri, _context, *args)
  deep_flatten_compact(*args).to_a.join
end

#contains(_uri, _context, str, substr) ⇒ Object



163
164
165
# File 'lib/ldpath/functions.rb', line 163

def contains(_uri, _context, str, substr)
  Array(str).map { |x| x.include? substr }
end

#count(_uri, _context, *args) ⇒ Object



16
17
18
# File 'lib/ldpath/functions.rb', line 16

def count(_uri, _context, *args)
  deep_flatten_compact(*args).count
end

#earliest(_uri, _context, *args) ⇒ Object

dates



94
95
96
# File 'lib/ldpath/functions.rb', line 94

def earliest(_uri, _context, *args)
  deep_flatten_compact(*args).min
end

#endsWith(_uri, _context, str, suffix) ⇒ Object



171
172
173
# File 'lib/ldpath/functions.rb', line 171

def endsWith(_uri, _context, str, suffix)
  Array(str).map { |x| x.end_with? suffix }
end

#eq(_uri, _context, *args) ⇒ Object



20
21
22
23
24
25
# File 'lib/ldpath/functions.rb', line 20

def eq(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:eq" unless rem.empty?

  a == b
end

#equals(_uri, _context, str, other) ⇒ Object



155
156
157
# File 'lib/ldpath/functions.rb', line 155

def equals(_uri, _context, str, other)
  Array(str).map { |x| x == other }
end

#equalsIgnoreCase(_uri, _context, str, other) ⇒ Object



159
160
161
# File 'lib/ldpath/functions.rb', line 159

def equalsIgnoreCase(_uri, _context, str, other)
  Array(str).map { |x| x.casecmp(other) }
end

#first(_uri, _context, *args) ⇒ Object



8
9
10
# File 'lib/ldpath/functions.rb', line 8

def first(_uri, _context, *args)
  deep_flatten_compact(*args).first
end

#flatten(uri, context, lists) ⇒ Object

collections



63
64
65
66
67
68
69
70
71
# File 'lib/ldpath/functions.rb', line 63

def flatten(uri, context, lists)
  return to_enum(:flatten, uri, context, lists) unless block_given?

  deep_flatten_compact(lists).each do |x|
    RDF::List.new(subject: x, graph: context).to_a.each do |i|
      yield i
    end
  end
end

#ge(_uri, _context, *args) ⇒ Object



55
56
57
58
59
60
# File 'lib/ldpath/functions.rb', line 55

def ge(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:ge" unless rem.empty?

  a >= b
end

#get(uri, context, list, idx) ⇒ Object



73
74
75
76
77
# File 'lib/ldpath/functions.rb', line 73

def get(uri, context, list, idx)
  idx = idx.respond_to?(:to_i) ? idx.to_i : idx.to_s.to_i

  flatten(uri, context, list).to_a[idx]
end

#gt(_uri, _context, *args) ⇒ Object



48
49
50
51
52
53
# File 'lib/ldpath/functions.rb', line 48

def gt(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:gt" unless rem.empty?

  a > b
end

#isEmpty(_uri, _context, str) ⇒ Object



175
176
177
# File 'lib/ldpath/functions.rb', line 175

def isEmpty(_uri, _context, str)
  Array(str).map(&:empty?)
end

#last(_uri, _context, *args) ⇒ Object



12
13
14
# File 'lib/ldpath/functions.rb', line 12

def last(_uri, _context, *args)
  deep_flatten_compact(*args).to_a.last
end

#latest(_uri, _context, *args) ⇒ Object



98
99
100
# File 'lib/ldpath/functions.rb', line 98

def latest(_uri, _context, *args)
  deep_flatten_compact(*args).max
end

#le(_uri, _context, *args) ⇒ Object



41
42
43
44
45
46
# File 'lib/ldpath/functions.rb', line 41

def le(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:le" unless rem.empty?

  a <= b
end

#lt(_uri, _context, *args) ⇒ Object



34
35
36
37
38
39
# File 'lib/ldpath/functions.rb', line 34

def lt(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:lt" unless rem.empty?

  a < b
end

#max(_uri, _context, *args) ⇒ Object



108
109
110
# File 'lib/ldpath/functions.rb', line 108

def max(_uri, _context, *args)
  deep_flatten_compact(*args).max
end

#min(_uri, _context, *args) ⇒ Object

math



104
105
106
# File 'lib/ldpath/functions.rb', line 104

def min(_uri, _context, *args)
  deep_flatten_compact(*args).min
end

#ne(_uri, _context, *args) ⇒ Object



27
28
29
30
31
32
# File 'lib/ldpath/functions.rb', line 27

def ne(_uri, _context, *args)
  a, b, *rem = deep_flatten_compact(*args).first(3)
  raise "Too many arguments to fn:ne" unless rem.empty?

  a != b
end

#predicates(uri, context, *_args) ⇒ Object



179
180
181
# File 'lib/ldpath/functions.rb', line 179

def predicates(uri, context, *_args)
  context.query([uri, nil, nil]).map(&:predicate).uniq
end

#replace(_uri, _context, str, pattern, replacement) ⇒ Object

text



124
125
126
127
128
129
# File 'lib/ldpath/functions.rb', line 124

def replace(_uri, _context, str, pattern, replacement)
  regex = Regexp.parse(pattern)
  Array(str).map do |x|
    x.gsub(regex, replacement)
  end
end

#round(_uri, _context, *args) ⇒ Object



112
113
114
115
116
# File 'lib/ldpath/functions.rb', line 112

def round(_uri, _context, *args)
  deep_flatten_compact(*args).map do |i|
    i.respond_to?(:round) ? i.round : i
  end
end

#startsWith(_uri, _context, str, suffix) ⇒ Object



167
168
169
# File 'lib/ldpath/functions.rb', line 167

def startsWith(_uri, _context, str, suffix)
  Array(str).map { |x| x.start_with? suffix }
end

#strJoin(_uri, _context, str, sep = "", prefix = "", suffix = "") ⇒ Object



151
152
153
# File 'lib/ldpath/functions.rb', line 151

def strJoin(_uri, _context, str, sep = "", prefix = "", suffix = "")
  prefix + Array(str).join(sep) + suffix
end

#strLeft(_uri, _context, str, left) ⇒ Object



139
140
141
# File 'lib/ldpath/functions.rb', line 139

def strLeft(_uri, _context, str, left)
  Array(str).map { |x| x[0..left.to_i] }
end

#strlen(_uri, _context, str) ⇒ Object



131
132
133
# File 'lib/ldpath/functions.rb', line 131

def strlen(_uri, _context, str)
  Array(str).map(&:length)
end

#strRight(_uri, _context, str, right) ⇒ Object



143
144
145
# File 'lib/ldpath/functions.rb', line 143

def strRight(_uri, _context, str, right)
  Array(str).map { |x| x[right.to_i..x.length] }
end

#subList(uri, context, list, idx_start, idx_end = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ldpath/functions.rb', line 79

def subList(uri, context, list, idx_start, idx_end = nil)
  arr = flatten(uri, context, list).to_a

  idx_start = idx_start.respond_to?(:to_i) ? idx_start.to_i : idx_start.to_s.to_i
  idx_end &&= idx_end.respond_to?(:to_i) ? idx_end.to_i : idx_end.to_s.to_i

  if idx_end
    arr[(idx_start.to_i..(idx_end - idx_start))]
  else
    arr.drop(idx_start)
  end
end

#substr(_uri, _context, str, left, right) ⇒ Object



147
148
149
# File 'lib/ldpath/functions.rb', line 147

def substr(_uri, _context, str, left, right)
  Array(str).map { |x| x[left.to_i..right.to_i] }
end

#sum(_uri, _context, *args) ⇒ Object



118
119
120
# File 'lib/ldpath/functions.rb', line 118

def sum(_uri, _context, *args)
  args.inject(0) { |acc, elem| acc + elem }
end

#wc(_uri, _context, str) ⇒ Object



135
136
137
# File 'lib/ldpath/functions.rb', line 135

def wc(_uri, _context, str)
  Array(str).map { |x| x.split.length }
end

#xpath(_uri, _context, xpath, node) ⇒ Object



183
184
185
186
187
188
# File 'lib/ldpath/functions.rb', line 183

def xpath(_uri, _context, xpath, node)
  x = Array(xpath).flatten.first
  Array(node).flatten.compact.map do |n|
    Nokogiri::XML(n.to_s).xpath(x.to_s, prefixes.map { |k, v| [k, v.to_s] }).map(&:text)
  end
end