Class: ActiveTriples::ParentStrategy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from PersistenceStrategy

#destroyed?

Constructor Details

#initialize(source) ⇒ ParentStrategy

Returns a new instance of ParentStrategy.

Parameters:

  • source (RDFSource, RDF::Enumerable)

    the ‘RDFSource` (or other `RDF::Enumerable` to persist with the strategy.



19
20
21
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 19

def initialize(source)
  @source = source
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



14
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 14

attr_reader :source, :parent

#sourceObject (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

#ancestorsEnumerator<RDFSource>

Returns:



70
71
72
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 70

def ancestors
  Ancestors.new(source).to_enum
end

#destroyObject

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_resourceObject

This method is abstract.

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.

Returns:

  • (#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.



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)

Returns:

  • (Boolean)

    true if loaded; else false.



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.

Returns:

  • (true)

    true if the save did not error



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.

Returns:

  • (Boolean)

See Also:



28
29
30
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 28

def persisted?
  super && parent.persisted?
end

#reloadBoolean

Repopulates the graph from parent.

Returns:

  • (Boolean)


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