Class: Ldpath::Program
- Inherits:
-
Object
- Object
- Ldpath::Program
- Includes:
- Functions
- Defined in:
- lib/ldpath/program.rb
Constant Summary collapse
- ParseError =
Class.new StandardError
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#loaded ⇒ Object
readonly
Returns the value of attribute loaded.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#prefixes ⇒ Object
readonly
Returns the value of attribute prefixes.
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate(uri, context = nil) ⇒ Object
- #func_call(fname, uri, context, *arguments) ⇒ Object
-
#initialize(mappings, options = {}) ⇒ Program
constructor
A new instance of Program.
- #load_graph(uri) ⇒ Object
- #loading(uri, context) ⇒ Object
- #meta ⇒ Object
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, = {}) @mappings ||= mappings @cache = [:cache] || RDF::Util::Cache.new @prefixes = [:prefixes] || {} @filters = [:filters] || [] @loaded = {} end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
31 32 33 |
# File 'lib/ldpath/program.rb', line 31 def cache @cache end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
31 32 33 |
# File 'lib/ldpath/program.rb', line 31 def filters @filters end |
#loaded ⇒ Object (readonly)
Returns the value of attribute loaded.
31 32 33 |
# File 'lib/ldpath/program.rb', line 31 def loaded @loaded end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
31 32 33 |
# File 'lib/ldpath/program.rb', line 31 def mappings @mappings end |
#prefixes ⇒ Object (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 |
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() 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 |
#meta ⇒ Object
82 83 84 |
# File 'lib/ldpath/program.rb', line 82 def @meta ||= {} end |