Class: ActiveTriples::ParentStrategy
- Inherits:
-
Object
- Object
- ActiveTriples::ParentStrategy
- Includes:
- PersistenceStrategy
- Defined in:
- lib/active_triples/persistence_strategies/parent_strategy.rb
Overview
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`.
Defined Under Namespace
Classes: Ancestors, NilParentError, UnmutableParentError
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#source ⇒ Object
readonly
the source to persist with this strategy.
Instance Method Summary collapse
- #ancestors ⇒ Enumerator<RDFSource>
-
#destroy ⇒ Object
Destroys the resource by removing it graph and references from the parent.
-
#erase_old_resource ⇒ Object
abstract
about this node or statement thus preparing to receive the updated assertions.
-
#final_parent ⇒ #persist!
The last parent in a chain from ‘parent` (e.g. the parent’s parent’s parent).
-
#graph ⇒ ActiveTriples::BufferedTransaction
A transaction on parent with buffered changes, with reads projected against an “Extended Bounded Description” of the strategy’s ‘#source`.
- #graph=(graph) ⇒ Object
-
#initialize(source) ⇒ ParentStrategy
constructor
A new instance of ParentStrategy.
-
#persist! ⇒ true
Persists the resource to the final parent.
-
#persisted? ⇒ Boolean
Resources using this strategy are persisted only if their parent is also persisted.
-
#reload ⇒ Boolean
Repopulates the graph from parent.
Methods included from PersistenceStrategy
Constructor Details
#initialize(source) ⇒ ParentStrategy
Returns a new instance of ParentStrategy.
21 22 23 24 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 21 def initialize(source) @graph = source.graph @source = source end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
16 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 16 attr_reader :source, :parent |
#source ⇒ Object (readonly)
the source to persist with this strategy
16 17 18 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 16 def source @source end |
Instance Method Details
#ancestors ⇒ Enumerator<RDFSource>
76 77 78 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 76 def ancestors Ancestors.new(source).to_enum end |
#destroy ⇒ Object
Destroys the resource by removing it graph and references from the parent.
61 62 63 64 65 66 |
# 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 ⇒ Object
Clear out any old assertions in the datastore / repository
about this node or statement thus preparing to receive the updated assertions.
72 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 72 def erase_old_resource; end |
#final_parent ⇒ #persist!
Returns 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.
84 85 86 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 84 def final_parent ancestors.to_a.last end |
#graph ⇒ ActiveTriples::BufferedTransaction
Returns a transaction on parent with buffered changes, with reads projected against an “Extended Bounded Description” of the strategy’s ‘#source`.
41 42 43 44 45 |
# 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) ⇒ Object
28 29 30 31 32 33 |
# 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 |
#persist! ⇒ true
Persists the resource to the final parent.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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? ⇒ Boolean
Resources using this strategy are persisted only if their parent is also persisted.
52 53 54 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 52 def persisted? super && parent.persisted? end |
#reload ⇒ Boolean
Repopulates the graph from parent.
124 125 126 127 128 129 130 131 132 133 |
# 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 |