Sha256: 5f5ea281aeec8bbd400db4f346e46518981463eceb9769a615a06e959f1f6f50

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require File.join(File.dirname(__FILE__), 'abstract_store')

module RdfContext
  # List storage, most efficient, but slow storage model. Works well for basic parse and serialize.
  class ListStore < AbstractStore
    def initialize(identifier = nil, configuration = {})
      super
      @triples = []
    end
    
    def inspect
      "ListStore[id=#{identifier}, size=#{@triples.length}]"
    end
    
    # 
    # Adds an extant triple to a graph.
    #
    # _context_ and _quoted_ are ignored
    def add(triple, context, quoted = false)
      @triples << triple unless contains?(triple, context)
    end
    
    # Remove a triple from the graph
    #
    # If the triple does not provide a context attribute, removes the triple
    # from all contexts.
    def remove(triple, context, quoted = false)
      if triple
        @triples.delete(triple)
      else
        @triples = []
      end
    end

    # Check to see if this graph contains the specified triple
    def contains?(triple, context = nil)
      !@triples.find_index(triple).nil?
    end

    # Triples from graph, optionally matching subject, predicate, or object.
    # Delegated from Graph. See Graph#triples for details.
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdf_context-0.4.2 lib/rdf_context/store/list_store.rb