class ActiveTriples::ParentStrategy
Persistence strategy for projecting `RDFSource`s onto the graph of an owning parent source. This allows individual resources to be treated as within the scope of another `RDFSource`.
Attributes
@!attribute [r] source
the source to persist with this strategy
@!attribute [r] parent
the target parent source for persistence
@!attribute [r] source
the source to persist with this strategy
@!attribute [r] parent
the target parent source for persistence
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/parent_strategy.rb, line 21 def initialize(source) @graph = source.graph @source = source end
Public Instance Methods
@return [Enumerator<RDFSource>]
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 76 def ancestors Ancestors.new(source).to_enum end
Destroys the resource by removing it graph and references from the parent.
@see PersistenceStrategy#destroy
ActiveTriples::PersistenceStrategy#destroy
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 61 def destroy super do graph.delete [source.to_term, nil, nil] parent.delete [parent, nil, source.to_term] end end
@abstract Clear out any old assertions in the datastore / repository about this node or statement thus preparing to receive the updated assertions.
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 72 def erase_old_resource; end
@return [#persist!] the last parent in a chain from `parent` (e.g.
the parent's parent's parent). This is the RDF::Mutable that the resource will project itself on when persisting.
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 84 def final_parent ancestors.to_a.last end
@return [ActiveTriples::BufferedTransaction] a transaction on parent with
buffered changes, with reads projected against an "Extended Bounded Description" of the strategy's `#source`.
@see ActiveTriples::ExtendedBoundedDescription
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 41 def graph @graph ||= BufferedTransaction.begin(parent, mutable: true, subject: source.to_term) end
@see PeristenceStrategy#graph=
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 28 def graph=(graph) final_parent.insert(graph || source.to_a) @graph = BufferedTransaction.begin(parent, mutable: true, subject: source.to_term) end
Sets the target “parent” source for persistence operations.
@param parent [RDFSource] source with a persistence strategy,
must be mutable.
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 93 def parent=(parent) raise NilParentError if parent.nil? raise UnmutableParentError unless parent.is_a? RDF::Mutable raise UnmutableParentError unless parent.mutable? @parent = parent reload end
Persists the resource to the final parent.
@return [true] true if the save did not error
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 106 def persist! raise NilParentError unless parent return false if final_parent.frozen? graph.execute parent.persist! if ancestors.find { |a| a.is_a?(ActiveTriples::List::ListResource) } reload @persisted = true end
Resources using this strategy are persisted only if their parent is also persisted.
@see PersistenceStrategy#persisted?
ActiveTriples::PersistenceStrategy#persisted?
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 52 def persisted? super && parent.persisted? end
Repopulates the graph from parent.
@return [Boolean]
# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 124 def reload return false if source.frozen? final_parent.persistence_strategy.class.new(source).reload self.graph = source.to_a @persisted = true unless graph.empty? true end