class ActiveTriples::RepositoryStrategy
Persistence strategy for projecting `RDFSource` to `RDF::Repositories`.
Attributes
@!attribute [r] source
the resource to persist with this strategy
Public Class Methods
@param source [RDFSource, RDF::Enumerable] the `RDFSource` (or other
`RDF::Enumerable` to persist with the strategy.
# File lib/active_triples/persistence_strategies/repository_strategy.rb, line 15 def initialize(source) @source = source end
Public Instance Methods
Deletes the resource from the repository.
ActiveTriples::PersistenceStrategy#destroy
# File lib/active_triples/persistence_strategies/repository_strategy.rb, line 22 def destroy super { source.clear } end
Clear out any old assertions in the repository about this node or statement thus preparing to receive the updated assertions.
# 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
Persists the resource to the repository
@return [true] returns true if the save did not error
# File lib/active_triples/persistence_strategies/repository_strategy.rb, line 44 def persist! erase_old_resource repository << source @persisted = true end
Repopulates the graph from the repository.
@return [Boolean]
# 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
@return [RDF::Repository] The RDF::Repository that the resource will project
itself on when persisting.
# File lib/active_triples/persistence_strategies/repository_strategy.rb, line 63 def repository @repository ||= set_repository end
Private Instance Methods
Finds an appropriate repository from the calling resource's configuration. If no repository is configured, builds an ephemeral in-memory repository and 'persists' there.
@todo find a way to move this logic out (PersistenceStrategyBuilder?).
so the dependency on Repositories is externalized.
# File lib/active_triples/persistence_strategies/repository_strategy.rb, line 76 def set_repository return RDF::Repository.new if source.class.repository.nil? repo = Repositories.repositories[source.class.repository] repo || raise(RepositoryNotFoundError, "The class #{source.class} expects a repository called #{source.class.repository}, but none was declared") end