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 RDFSources 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.



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

#parentObject

Returns the value of attribute parent.



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

attr_reader :source, :parent

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

#ancestorsEnumerator<RDFSource>

Returns:



76
77
78
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 76

def ancestors
  Ancestors.new(source).to_enum
end

#destroyObject

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_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.



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.

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.



84
85
86
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 84

def final_parent
  ancestors.to_a.last
end

#graphActiveTriples::BufferedTransaction

Returns a transaction on parent with buffered changes, with reads projected against an "Extended Bounded Description" of the strategy's #source.

Returns:

  • (ActiveTriples::BufferedTransaction)

    a transaction on parent with buffered changes, with reads projected against an "Extended Bounded Description" of the strategy's #source.

See Also:



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

See Also:

  • PeristenceStrategy#graph=


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.

Returns:

  • (true)

    true if the save did not error

Raises:



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.

Returns:

  • (Boolean)

See Also:



52
53
54
# File 'lib/active_triples/persistence_strategies/parent_strategy.rb', line 52

def persisted?
  super && parent.persisted?
end

#reloadBoolean

Repopulates the graph from parent.

Returns:

  • (Boolean)


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