class ActiveTriples::MongoidStrategy
Persistence strategy for projecting `RDFSource` to `Mongoid::Documents`.
Constants
- VERSION
Attributes
collection[R]
@!attribute [r] obj
the RDFSource to persist with this strategy
@!attribute [r] collection
the Mongoid::Document class that the object will project itself on when persisting
obj[R]
@!attribute [r] obj
the RDFSource to persist with this strategy
@!attribute [r] collection
the Mongoid::Document class that the object will project itself on when persisting
Public Class Methods
new(obj)
click to toggle source
@param obj [RDFSource] the `RDFSource` to persist with the strategy.
# File lib/active_triples/mongoid_strategy.rb, line 21 def initialize(obj) @obj = obj @collection = set_klass end
Public Instance Methods
destroy()
click to toggle source
Delete the Document from the collection and mark the Resource as destroyed
Calls superclass method
# File lib/active_triples/mongoid_strategy.rb, line 35 def destroy super { erase_old_resource } end
erase_old_resource()
click to toggle source
Delete the Document from the collection
# File lib/active_triples/mongoid_strategy.rb, line 28 def erase_old_resource persisted_document.destroy end
persist!()
click to toggle source
Persists the object to the repository
@return [true] returns true if the save did not error
# File lib/active_triples/mongoid_strategy.rb, line 43 def persist! # Persist ALL objects as a @graph unless obj.empty? doc = collection.find_or_initialize_by(id: obj.id) doc.attributes = JSON.parse(obj.dump(:jsonld, standard_prefixes: true, useNativeTypes: true)) doc.save end @persisted = true end
reload()
click to toggle source
Repopulates the graph from the repository.
@return [Boolean]
# File lib/active_triples/mongoid_strategy.rb, line 59 def reload # Retrieve document from #collection if it exists doc = persisted_document.first obj << JSON::LD::API.toRDF(doc.as_document, rename_bnodes: false) unless doc.nil? @persisted = true end
Private Instance Methods
delegate_klass(klass_name)
click to toggle source
Define a Mongoid::Document delegate class
# File lib/active_triples/mongoid_strategy.rb, line 89 def delegate_klass(klass_name) klass = self.class.const_set(klass_name, Class.new) klass.include Mongoid::Document klass.include Mongoid::Attributes::Dynamic klass.store_in collection: obj.model_name.plural klass end
persisted_document()
click to toggle source
@return [Mongoid::Criteria] criteria matching the document
# File lib/active_triples/mongoid_strategy.rb, line 71 def persisted_document collection.where(id: obj.id) end
set_klass()
click to toggle source
Return the delegated class for the object's model
# File lib/active_triples/mongoid_strategy.rb, line 77 def set_klass klass_name = obj.model_name.name.demodulize.to_sym if self.class.constants.include? klass_name return self.class.const_get(klass_name) else return delegate_klass(klass_name) end end