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).
-
#initialize(source) ⇒ ParentStrategy
constructor
A new instance of ParentStrategy.
-
#loaded? ⇒ Boolean
Indicates if the resource has been loaded from the repository (used for lazy load).
-
#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.
19 20 21 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 19 def initialize(source) @source = source end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
14 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 14 attr_reader :source, :parent |
#source ⇒ Object (readonly)
the source to persist with this strategy
14 15 16 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 14 def source @source end |
Instance Method Details
#ancestors ⇒ Enumerator<RDFSource>
70 71 72 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 70 def ancestors Ancestors.new(source).to_enum end |
#destroy ⇒ Object
Destroys the resource by removing it graph and references from the parent.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 45 def destroy final_parent.delete(source.statements) parent.statements.each do |statement| parent.delete_statement(statement) if statement.subject == source.rdf_subject || statement.object == source.rdf_subject end super { source.clear } 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.
61 62 63 64 65 66 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 61 def erase_old_resource final_parent.statements.each do |statement| final_parent.send(:delete_statement, statement) if statement.subject == source.rdf_subject end 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.
78 79 80 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 78 def final_parent ancestors.to_a.last end |
#loaded? ⇒ Boolean
Indicates if the resource has been loaded from the repository (used for lazy load)
36 37 38 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 36 def loaded? @loaded ||= false end |
#persist! ⇒ true
Persists the resource to the final parent.
97 98 99 100 101 102 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 97 def persist! return false if final_parent.frozen? erase_old_resource final_parent << source @persisted = true end |
#persisted? ⇒ Boolean
Resources using this strategy are persisted only if their parent is also persisted.
28 29 30 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 28 def persisted? super && parent.persisted? end |
#reload ⇒ Boolean
Repopulates the graph from parent.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 108 def reload return false if source.frozen? if loaded? || !persisted? source << final_parent.query(subject: source.rdf_subject) else RepositoryStrategy.new(source).reload source.persist! @loaded=true end @persisted = true unless source.empty? true end |