Class: RdfContext::ListStore

Inherits:
AbstractStore show all
Defined in:
lib/rdf_context/store/list_store.rb

Overview

List storage, most efficient, but slow storage model. Works well for basic parse and serialize.

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier, #nsbinding, #uri_binding

Instance Method Summary collapse

Methods inherited from AbstractStore

#bind, #bnodes, #close, #commit, #context_aware?, #contexts, #formula_aware?, #item, #namespace, #objects, #open, #predicates, #prefix, #rollback, #size, #subjects, #transaction_aware?

Constructor Details

#initialize(identifier = nil, configuration = {}) ⇒ ListStore

Returns a new instance of ListStore.



6
7
8
9
# File 'lib/rdf_context/store/list_store.rb', line 6

def initialize(identifier = nil, configuration = {})
  super
  @triples = []
end

Instance Method Details

#add(triple, context, quoted = false) ⇒ Triple

Add triple to store

Parameters:

  • triple (Triple)
  • context (Graph)

    (nil) ignored

  • quoted (Boolean) (defaults to: false)

    (false) ignored

Returns:



29
30
31
# File 'lib/rdf_context/store/list_store.rb', line 29

def add(triple, context, quoted = false)
  @triples << triple unless contains?(triple, context)
end

#contains?(triple, context = nil) ⇒ Boolean

Check to see if this store contains the specified triple

Parameters:

  • triple (Triple)
  • context (Graph) (defaults to: nil)

    (nil) ignored

Returns:

  • (Boolean)


49
50
51
# File 'lib/rdf_context/store/list_store.rb', line 49

def contains?(triple, context = nil)
  @triples.any? {|t| t == triple}
end

#destroy(configuration = {}) ⇒ Object

Destroy the store, as it can contain only one context



20
21
22
# File 'lib/rdf_context/store/list_store.rb', line 20

def destroy(configuration = {})
  @triples = []
end

#inspectListStore

Create a new ListStore Store, should be subclassed @param configuration Specific to type of storage

Parameters:

Returns:



15
16
17
# File 'lib/rdf_context/store/list_store.rb', line 15

def inspect
  "ListStore[id=#{identifier}, size=#{@triples.length}]"
end

#remove(triple, context, quoted = false)

This method returns an undefined value.

Remove a triple from the store

If the triple does not provide a context attribute, removes the triple from all contexts.

Parameters:

  • triple (Triple)
  • context (Graph)

    (nil) ignored

  • quoted (Boolean) (defaults to: false)

    (false) ignored



41
42
43
# File 'lib/rdf_context/store/list_store.rb', line 41

def remove(triple, context, quoted = false)
  @triples.delete(triple)
end

#triples(triple, context = nil) {|triple, context| ... } ⇒ Array<Triplle>

Triples from graph, optionally matching subject, predicate, or object. Delegated from Graph. See Graph#triples for details.

Parameters:

  • triple (Triple)
  • context (Graph) (defaults to: nil)

    (nil)

Yields:

  • (triple, context)

Yield Parameters:

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rdf_context/store/list_store.rb', line 62

def triples(triple, context = nil)
  subject = triple.subject
  predicate = triple.predicate
  object = triple.object
  
  if subject || predicate || object
    @triples.select do |t|
      next unless t == triple # Includes matching
      
      if block_given?
        yield t
      end
      t
    end.compact
  elsif block_given?
    @triples.each {|triple| yield triple}
  else
    @triples
  end
end