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

parent[R]

@!attribute [r] source

the source to persist with this strategy

@!attribute [r] parent

the target parent source for persistence
source[R]

@!attribute [r] source

the source to persist with this strategy

@!attribute [r] parent

the target parent source for persistence

Public Class Methods

new(source) click to toggle source

@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

ancestors() click to toggle source

@return [Enumerator<RDFSource>]

# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 76
def ancestors
  Ancestors.new(source).to_enum
end
destroy() click to toggle source

Destroys the resource by removing it graph and references from the parent.

@see 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
erase_old_resource() click to toggle source

@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
final_parent() click to toggle source

@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
graph() click to toggle source

@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
graph=(graph) click to toggle source

@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
parent=(parent) click to toggle source

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
persist!() click to toggle source

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
persisted?() click to toggle source

Resources using this strategy are persisted only if their parent is also persisted.

@see PersistenceStrategy#persisted?

# File lib/active_triples/persistence_strategies/parent_strategy.rb, line 52
def persisted?
  super && parent.persisted?
end
reload() click to toggle source

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