class Gearbox::RDFCollection
Collects model values as RDF. This is a key part of making SPARQL our primary filtering, finding, and extension language.
Attributes
Public Class Methods
# File lib/gearbox/rdf_collection.rb, line 14 def initialize @source = {} end
Public Instance Methods
Get RDF::Statement from the underlying collection. Normalizes the key. @param [String, Symbol] key. Normalized. @return [RDF::Statement, nil] Found statement, if it exists.
# File lib/gearbox/rdf_collection.rb, line 47 def [](key) @source[normalize_key(key)] end
Set RDF::Statements to the underlying collection. Normalizes the keys. @param [String, Symbol] key @param [RDF::Statement] obj. RDF::Statement that will be added.
# 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
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?). @param [Block] block Optional block. Creates an external iterator if omitted. @return [nil, Enumerator] Returns either nil, or an external iterator.
# File lib/gearbox/rdf_collection.rb, line 23 def each(&block) local_repository.each(&block) end
# File lib/gearbox/rdf_collection.rb, line 27 def each_with_field_names(&block) @source.each(&block) end
Lookup whether the key exists. @param [String, Symbol] key @param [Hash, nil] opts. :normalize => false will lookup the key as provided. @return [Boolean]
# 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
# File lib/gearbox/rdf_collection.rb, line 69 def local_repository @local_repository ||= RDF::Repository.new end
Merges a hash of RDF::Statements into the underlying collection. Uses the add_statement
to filter the values of the hash. @param [Hash] hash. Collection of statements. @return [nil]
# File lib/gearbox/rdf_collection.rb, line 64 def merge!(hash) hash.each_with_field_names {|key, obj| add_statement(key, obj)} end
# File lib/gearbox/rdf_collection.rb, line 73 def query(string) SPARQL.execute(string, local_repository) end
Private Instance Methods
Normalizes the key. Converts to a lower case symbol with non-alpha-numerics replaced by underscores, removing trailing and preceding underscores. @private
# File lib/gearbox/rdf_collection.rb, line 81 def normalize_key(obj) obj.to_s.downcase.gsub(/[^A-Za-z0-9_]+/, '_').gsub(/(_$)|(^_)/, '').to_sym end