Module: Ldpath::Functions

Included in:
Program
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)
  args.flatten.compact.join
end

#contains(uri, context, str, substr) ⇒ Object



165
166
167
# File 'lib/ldpath/functions.rb', line 165

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)
  args.flatten.compact.length
end

#earliest(uri, context, *args) ⇒ Object

dates



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

def earliest(uri, context, *args)
  args.flatten.min
end

#endsWith(uri, context, str, suffix) ⇒ Object



173
174
175
# File 'lib/ldpath/functions.rb', line 173

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
26
# File 'lib/ldpath/functions.rb', line 20

def eq(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:eq"
  end
  a == b
end

#equals(uri, context, str, other) ⇒ Object



157
158
159
# File 'lib/ldpath/functions.rb', line 157

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

#equalsIgnoreCase(uri, context, str, other) ⇒ Object



161
162
163
# File 'lib/ldpath/functions.rb', line 161

def equalsIgnoreCase(uri, context, str, other)
  Array(str).map { |x| x.downcase == other.downcase }
end

#first(uri, context, *args) ⇒ Object



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

def first(uri, context, *args)
  args.flatten.compact.first
end

#flatten(uri, context, lists) ⇒ Object

collections



69
70
71
# File 'lib/ldpath/functions.rb', line 69

def flatten(uri, context, lists)
  Array(lists).flatten.map { |x| RDF::List.new(x, context).to_a }.flatten
end

#ge(uri, context, *args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ldpath/functions.rb', line 60

def ge(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:ge"
  end
  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)[idx]
end

#gt(uri, context, *args) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ldpath/functions.rb', line 52

def gt(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:gt"
  end
  a > b
end

#isEmpty(uri, context, str) ⇒ Object



177
178
179
# File 'lib/ldpath/functions.rb', line 177

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)
  args.flatten.compact.last
end

#latest(uri, context, *args) ⇒ Object



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

def latest(uri, context, *args)
  args.flatten.max
end

#le(uri, context, *args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ldpath/functions.rb', line 44

def le(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:le"
  end
  a <= b
end

#lt(uri, context, *args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/ldpath/functions.rb', line 36

def lt(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:lt"
  end
  a < b
end

#max(uri, context, *args) ⇒ Object



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

def max(uri, context, *args)
  args.flatten.max
end

#min(uri, context, *args) ⇒ Object

math



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

def min(uri, context, *args)
  args.flatten.min
end

#ne(uri, context, *args) ⇒ Object



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

def ne(uri, context, *args)
  a, b, *rem = args.flatten
  unless rem.empty?
    raise "Too many arguments to fn:ne"
  end
  a != b
end

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



181
182
183
# File 'lib/ldpath/functions.rb', line 181

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

#replace(uri, context, str, pattern, replacement) ⇒ Object

text



126
127
128
129
130
131
# File 'lib/ldpath/functions.rb', line 126

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
117
118
# File 'lib/ldpath/functions.rb', line 112

def round(uri, context, *args)
  args.map do |n|
    Array(n).map do |i|
      i.respond_to? :round ? i.round : i
    end
  end
end

#startsWith(uri, context, str, suffix) ⇒ Object



169
170
171
# File 'lib/ldpath/functions.rb', line 169

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

#strJoin(uri, context, str, sep = "", prefix = "", suffix = "") ⇒ Object



153
154
155
# File 'lib/ldpath/functions.rb', line 153

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

#strLeft(uri, context, str, left) ⇒ Object



141
142
143
# File 'lib/ldpath/functions.rb', line 141

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

#strlen(uri, context, str) ⇒ Object



133
134
135
# File 'lib/ldpath/functions.rb', line 133

def strlen(uri, context, str)
  Array(str).map(&:length)
end

#strRight(uri, context, str, right) ⇒ Object



145
146
147
# File 'lib/ldpath/functions.rb', line 145

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)

  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



149
150
151
# File 'lib/ldpath/functions.rb', line 149

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

#sum(uri, context, *args) ⇒ Object



120
121
122
# File 'lib/ldpath/functions.rb', line 120

def sum(uri, context, *args)
  args.inject(0) { |sum, n| sum + n }
end

#wc(uri, context, str) ⇒ Object



137
138
139
# File 'lib/ldpath/functions.rb', line 137

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

#xpath(uri, context, xpath, node) ⇒ Object



185
186
187
188
189
190
# File 'lib/ldpath/functions.rb', line 185

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