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



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

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



89
90
91
# File 'lib/ldpath/functions.rb', line 89

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

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



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

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



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

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

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



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

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

def get(uri, context, list, idx)
  flatten(uri, context, list)[idx.to_i]
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



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

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



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

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



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

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

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

math



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

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



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

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

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

text



121
122
123
124
125
126
# File 'lib/ldpath/functions.rb', line 121

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



107
108
109
110
111
112
113
# File 'lib/ldpath/functions.rb', line 107

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



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

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

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



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

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

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



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

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

#strlen(uri, context, str) ⇒ Object



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

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

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



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

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



77
78
79
80
81
82
83
84
85
# File 'lib/ldpath/functions.rb', line 77

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

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

#substr(uri, context, str, left, right) ⇒ Object



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

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

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



115
116
117
# File 'lib/ldpath/functions.rb', line 115

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

#wc(uri, context, str) ⇒ Object



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

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

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



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

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