Class: Gearbox::RDFCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeRDFCollection

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_repositoryObject



69
70
71
# File 'lib/gearbox/rdf_collection.rb', line 69

def local_repository
  @local_repository ||= RDF::Repository.new
end

#sourceObject

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.

Parameters:

  • key. (String, Symbol)

    Normalized.

Returns:

  • (RDF::Statement, nil)

    Found statement, if it exists.



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.

Parameters:

  • key (String, Symbol)
  • obj. (RDF::Statement)

    RDF::Statement that will be added.



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?).

Parameters:

  • block (Block)

    Optional block. Creates an external iterator if omitted.

Returns:

  • (nil, Enumerator)

    Returns either nil, or an external iterator.



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.

Parameters:

  • key (String, Symbol)
  • opts. (Hash, nil)

    :normalize => false will lookup the key as provided.

Returns:

  • (Boolean)


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.

Parameters:

  • hash. (Hash)

    Collection of statements.

Returns:

  • (nil)


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