module Ldpath::Functions

Public Instance Methods

concat(_uri, _context, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 16
def count(_uri, _context, *args)
  deep_flatten_compact(*args).count
end
earliest(_uri, _context, *args) click to toggle source

dates

# File lib/ldpath/functions.rb, line 94
def earliest(_uri, _context, *args)
  deep_flatten_compact(*args).min
end
endsWith(_uri, _context, str, suffix) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 8
def first(_uri, _context, *args)
  deep_flatten_compact(*args).first
end
flatten(uri, context, lists) { |i| ... } click to toggle source

collections

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 175
def isEmpty(_uri, _context, str)
  Array(str).map(&:empty?)
end
last(_uri, _context, *args) click to toggle source
# File lib/ldpath/functions.rb, line 12
def last(_uri, _context, *args)
  deep_flatten_compact(*args).to_a.last
end
latest(_uri, _context, *args) click to toggle source
# File lib/ldpath/functions.rb, line 98
def latest(_uri, _context, *args)
  deep_flatten_compact(*args).max
end
le(_uri, _context, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 108
def max(_uri, _context, *args)
  deep_flatten_compact(*args).max
end
min(_uri, _context, *args) click to toggle source

math

# File lib/ldpath/functions.rb, line 104
def min(_uri, _context, *args)
  deep_flatten_compact(*args).min
end
ne(_uri, _context, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

text

# 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) click to toggle source
# 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) click to toggle source
# 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 = "") click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 139
def strLeft(_uri, _context, str, left)
  Array(str).map { |x| x[0..left.to_i] }
end
strRight(_uri, _context, str, right) click to toggle source
# File lib/ldpath/functions.rb, line 143
def strRight(_uri, _context, str, right)
  Array(str).map { |x| x[right.to_i..x.length] }
end
strlen(_uri, _context, str) click to toggle source
# File lib/ldpath/functions.rb, line 131
def strlen(_uri, _context, str)
  Array(str).map(&:length)
end
subList(uri, context, list, idx_start, idx_end = nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ldpath/functions.rb, line 118
def sum(_uri, _context, *args)
  args.inject(0) { |acc, elem| acc + elem }
end
wc(_uri, _context, str) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

deep_flatten_compact(*args) { |y| ... } click to toggle source
# File lib/ldpath/functions.rb, line 192
def deep_flatten_compact(*args)
  return to_enum(:deep_flatten_compact, *args) unless block_given?

  args.each do |x|
    if x.is_a? Enumerable
      x.each { |y| yield y unless y.nil? }
    else
      yield x unless x.nil?
    end
  end
end