Class: Ldpath::Program

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

Constant Summary collapse

ParseError =
Class.new StandardError

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.



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

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.



31
32
33
# File 'lib/ldpath/program.rb', line 31

def cache
  @cache
end

#filtersObject (readonly)

Returns the value of attribute filters.



31
32
33
# File 'lib/ldpath/program.rb', line 31

def filters
  @filters
end

#loadedObject (readonly)

Returns the value of attribute loaded.



31
32
33
# File 'lib/ldpath/program.rb', line 31

def loaded
  @loaded
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



31
32
33
# File 'lib/ldpath/program.rb', line 31

def mappings
  @mappings
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



31
32
33
# File 'lib/ldpath/program.rb', line 31

def prefixes
  @prefixes
end

Class Method Details

.load(program) ⇒ Object



14
15
16
17
18
# File 'lib/ldpath/program.rb', line 14

def load(program)
  parser.parse(program, reporter: Parslet::ErrorReporter::Deepest.new)
rescue Parslet::ParseFailed => e
  fail ParseError, e.cause.ascii_tree
end

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



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

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

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

Instance Method Details

#evaluate(uri, context = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ldpath/program.rb', line 58

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



86
87
88
89
90
91
92
# File 'lib/ldpath/program.rb', line 86

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ldpath/program.rb', line 46

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



40
41
42
43
44
# File 'lib/ldpath/program.rb', line 40

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

#metaObject



82
83
84
# File 'lib/ldpath/program.rb', line 82

def meta
  @meta ||= {}
end