Class: Gearbox::RDFCollection
- Inherits:
-
Object
- Object
- Gearbox::RDFCollection
- Includes:
- RDF::Queryable
- Defined in:
- lib/gearbox/rdf_collection.rb
Overview
Collects model values as RDF. This is a key part of making SPARQL our primary filtering, finding, and extension language.
Instance Attribute Summary collapse
- #local_repository ⇒ Object
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#[](key) ⇒ RDF::Statement?
Get RDF::Statement from the underlying collection.
-
#add_statement(key, obj) ⇒ Object
(also: #[]=)
Set RDF::Statements to the underlying collection.
-
#each(&block) ⇒ nil, Enumerator
Enumerates on the RDF Statements.
- #each_with_field_names(&block) ⇒ Object
-
#has_key?(key, opts = {}) ⇒ Boolean
Lookup whether the key exists.
-
#initialize ⇒ RDFCollection
constructor
A new instance of RDFCollection.
-
#merge!(hash) ⇒ nil
Merges a hash of RDF::Statements into the underlying collection.
- #query(string) ⇒ Object
Constructor Details
#initialize ⇒ RDFCollection
Returns a new instance of RDFCollection.
14 15 16 |
# File 'lib/gearbox/rdf_collection.rb', line 14 def initialize @source = {} end |
Instance Attribute Details
#local_repository ⇒ Object
69 70 71 |
# File 'lib/gearbox/rdf_collection.rb', line 69 def local_repository @local_repository ||= RDF::Repository.new end |
#source ⇒ Object
Returns the value of attribute source.
31 32 33 |
# File 'lib/gearbox/rdf_collection.rb', line 31 def source @source end |
Instance Method Details
#[](key) ⇒ RDF::Statement?
Get RDF::Statement from the underlying collection. Normalizes the key.
47 48 49 |
# File 'lib/gearbox/rdf_collection.rb', line 47 def [](key) @source[normalize_key(key)] end |
#add_statement(key, obj) ⇒ Object Also known as: []=
Set RDF::Statements to the underlying collection. Normalizes the keys.
36 37 38 39 40 41 |
# File 'lib/gearbox/rdf_collection.rb', line 36 def add_statement(key, obj) if obj.is_a?(RDF::Statement) @source[normalize_key(key)] = obj local_repository << obj end end |
#each(&block) ⇒ nil, Enumerator
Enumerates on the RDF Statements. Necessary for RDF::Enumerable to add all of the internal and external iterator goodies available there (like each_subject and has_subject?).
23 24 25 |
# File 'lib/gearbox/rdf_collection.rb', line 23 def each(&block) local_repository.each(&block) end |
#each_with_field_names(&block) ⇒ Object
27 28 29 |
# File 'lib/gearbox/rdf_collection.rb', line 27 def each_with_field_names(&block) @source.each(&block) end |
#has_key?(key, opts = {}) ⇒ Boolean
Lookup whether the key exists.
55 56 57 58 |
# File 'lib/gearbox/rdf_collection.rb', line 55 def has_key?(key, opts={}) key = normalize_key(key) if opts.fetch(:normalize, true) @source.has_key?(key) end |
#merge!(hash) ⇒ nil
Merges a hash of RDF::Statements into the underlying collection. Uses the add_statement to filter the values of the hash.
64 65 66 |
# File 'lib/gearbox/rdf_collection.rb', line 64 def merge!(hash) hash.each_with_field_names {|key, obj| add_statement(key, obj)} end |
#query(string) ⇒ Object
73 74 75 |
# File 'lib/gearbox/rdf_collection.rb', line 73 def query(string) SPARQL.execute(string, local_repository) end |