Class: Ldpath::Program
- Inherits:
-
Object
- Object
- Ldpath::Program
- Includes:
- Functions
- Defined in:
- lib/ldpath/program.rb
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.
25 26 27 28 29 30 31 |
# File 'lib/ldpath/program.rb', line 25 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.
24 25 26 |
# File 'lib/ldpath/program.rb', line 24 def cache @cache end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
24 25 26 |
# File 'lib/ldpath/program.rb', line 24 def filters @filters end |
#loaded ⇒ Object (readonly)
Returns the value of attribute loaded.
24 25 26 |
# File 'lib/ldpath/program.rb', line 24 def loaded @loaded end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
24 25 26 |
# File 'lib/ldpath/program.rb', line 24 def mappings @mappings end |
#prefixes ⇒ Object (readonly)
Returns the value of attribute prefixes.
24 25 26 |
# File 'lib/ldpath/program.rb', line 24 def prefixes @prefixes end |
Class Method Details
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() 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 |
#meta ⇒ Object
75 76 77 |
# File 'lib/ldpath/program.rb', line 75 def @meta ||= {} end |