class Gearbox::RDFCollection

Collects model values as RDF. This is a key part of making SPARQL our primary filtering, finding, and extension language.

Attributes

local_repository[W]
source[RW]

Public Class Methods

new() click to toggle source
# File lib/gearbox/rdf_collection.rb, line 14
def initialize
  @source = {}
end

Public Instance Methods

[](key) click to toggle source

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
[]=(key, obj)
Alias for: add_statement
add_statement(key, obj) click to toggle source

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
Also aliased as: []=
each(&block) click to toggle source

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
each_with_field_names(&block) click to toggle source
# File lib/gearbox/rdf_collection.rb, line 27
def each_with_field_names(&block)
  @source.each(&block)
end
has_key?(key, opts={}) click to toggle source

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
local_repository() click to toggle source
# File lib/gearbox/rdf_collection.rb, line 69
def local_repository
  @local_repository ||= RDF::Repository.new
end
merge!(hash) click to toggle source

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
query(string) click to toggle source
# File lib/gearbox/rdf_collection.rb, line 73
def query(string)
  SPARQL.execute(string, local_repository)
end

Private Instance Methods

normalize_key(obj) click to toggle source

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