Class: ActiveTriples::RepositoryStrategy

Inherits:
Object
  • Object
show all
Includes:
PersistenceStrategy
Defined in:
lib/active_triples/persistence_strategies/repository_strategy.rb

Overview

Persistence strategy for projecting ‘RDFSource` to `RDF::Repositories`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PersistenceStrategy

#destroyed?, #graph, #graph=, #persisted?

Constructor Details

#initialize(source) ⇒ RepositoryStrategy

Returns a new instance of RepositoryStrategy.

Parameters:

  • source (RDFSource, RDF::Enumerable)

    the ‘RDFSource` (or other `RDF::Enumerable` to persist with the strategy.



15
16
17
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 15

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 10

def source
  @source
end

Instance Method Details

#destroyObject

Deletes the resource from the repository.



22
23
24
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 22

def destroy
  super { source.clear }
end

#erase_old_resourceObject

Clear out any old assertions in the repository about this node or statement thus preparing to receive the updated assertions.



29
30
31
32
33
34
35
36
37
38
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 29

def erase_old_resource
  if source.node?
    repository.statements.each do |statement|
      repository.send(:delete_statement, statement) if
        statement.subject == source
    end
  else
    repository.delete [source.to_term, nil, nil]
  end
end

#persist!true

Persists the resource to the repository

Returns:

  • (true)

    returns true if the save did not error



44
45
46
47
48
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 44

def persist!
  erase_old_resource
  repository << source
  @persisted = true
end

#reloadBoolean

Repopulates the graph from the repository.

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 54

def reload
  source << repository.query(subject: source)
  @persisted = true unless source.empty?
  true
end

#repositoryRDF::Repository

Returns The RDF::Repository that the resource will project itself on when persisting.

Returns:

  • (RDF::Repository)

    The RDF::Repository that the resource will project itself on when persisting.



63
64
65
# File 'lib/active_triples/persistence_strategies/repository_strategy.rb', line 63

def repository
  @repository ||= set_repository
end