Class: Ldpath::Program

Inherits:
Object
  • Object
show all
Includes:
Functions
Defined in:
lib/ldpath/program.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Functions

#concat, #contains, #count, #earliest, #endsWith, #eq, #equals, #equalsIgnoreCase, #first, #flatten, #ge, #get, #gt, #isEmpty, #last, #latest, #le, #lt, #max, #min, #ne, #predicates, #replace, #round, #startsWith, #strJoin, #strLeft, #strRight, #strlen, #subList, #substr, #sum, #wc, #xpath

Constructor Details

#initialize(mappings, options = {}) ⇒ Program

Returns a new instance of Program.



25
26
27
28
29
30
31
# File 'lib/ldpath/program.rb', line 25

def initialize(mappings, options = {})
  @mappings ||= mappings
  @cache = options[:cache] || RDF::Util::Cache.new
  @prefixes = options[:prefixes] || {}
  @filters = options[:filters] || []
  @loaded = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



24
25
26
# File 'lib/ldpath/program.rb', line 24

def cache
  @cache
end

#filtersObject (readonly)

Returns the value of attribute filters.



24
25
26
# File 'lib/ldpath/program.rb', line 24

def filters
  @filters
end

#loadedObject (readonly)

Returns the value of attribute loaded.



24
25
26
# File 'lib/ldpath/program.rb', line 24

def loaded
  @loaded
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



24
25
26
# File 'lib/ldpath/program.rb', line 24

def mappings
  @mappings
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



24
25
26
# File 'lib/ldpath/program.rb', line 24

def prefixes
  @prefixes
end

Class Method Details

.parse(program, transform_context = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/ldpath/program.rb', line 6

def parse(program, transform_context = {})
  parsed = parser.parse(program)
  ast = transform.apply parsed, transform_context

  Ldpath::Program.new ast.compact, transform_context
end

Instance Method Details

#evaluate(uri, context = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ldpath/program.rb', line 51

def evaluate(uri, context = nil)
  h = {}
  context ||= load_graph(uri.to_s)

  unless filters.empty?
    return h unless filters.all? { |f| f.evaluate(self, uri, context) }
  end

  mappings.each do |m|
    h[m.name] ||= []
    h[m.name] += case m.selector
                 when Selector
                   m.selector.evaluate(self, uri, context).map do |x|
                     next x unless m.field_type
                     RDF::Literal.new(x.to_s, datatype: m.field_type).canonicalize.object
                   end
                 else
                   Array(m.selector)
                 end
  end

  h.merge(meta)
end

#func_call(fname, uri, context, *arguments) ⇒ Object



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

def func_call(fname, uri, context, *arguments)
  if function_method? fname
    public_send(fname, uri, context, *arguments)
  else
    raise "No such function: #{fname}"
  end
end

#load_graph(uri) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ldpath/program.rb', line 39

def load_graph(uri)
  cache[uri] ||= begin
    Ldpath.logger.debug "[#{object_id}] Loading #{uri.inspect}"

    reader_types = RDF::Format.reader_types.reject { |t| t.to_s =~ /html/ }.map do |t|
      t.to_s =~ /text\/(?:plain|html)/ ? "#{t};q=0.5" : t
    end

    RDF::Graph.load(uri, headers: { 'Accept' => reader_types.join(", ") }).tap { loaded[uri] = true }
  end
end

#loading(uri, context) ⇒ Object



33
34
35
36
37
# File 'lib/ldpath/program.rb', line 33

def loading(uri, context)
  if uri.to_s =~ /^http/ && !loaded[uri.to_s]
    context << load_graph(uri.to_s)
  end
end

#metaObject



75
76
77
# File 'lib/ldpath/program.rb', line 75

def meta
  @meta ||= {}
end