Module: Ladder::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveTriples::Identifiable, Serializable, Mongoid::Document
- Included in:
- Dynamic
- Defined in:
- lib/ladder/resource.rb,
lib/ladder/resource/dynamic.rb,
lib/ladder/resource/serializable.rb
Defined Under Namespace
Modules: ClassMethods, Dynamic, Serializable
Class Method Summary collapse
-
.from_uri(uri) ⇒ Ladder::Resource
Return a persisted instance of a Ladder::Resource from its RDF subject URI, without knowing the resource class.
Instance Method Summary collapse
-
#<<(statement) ⇒ void
Push an RDF::Statement into the object.
-
#klass_from_predicate(predicate) ⇒ Ladder::Resource, ...
Retrieve the class for a relation, based on its defined RDF predicate.
-
#update_resource(opts = {}) ⇒ ActiveTriples::Resource
Update the delegated ActiveTriples::Resource from ActiveModel properties & relations.
Methods included from Serializable
#as_framed_jsonld, #as_jsonld, #as_qname
Class Method Details
.from_uri(uri) ⇒ Ladder::Resource
Return a persisted instance of a Ladder::Resource from its RDF subject URI, without knowing the resource class.
If there is no persisted instance for the URI, but the class is identifiable, then return a new instance of that class
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/ladder/resource.rb', line 238 def self.from_uri(uri) klasses = ActiveTriples::Resource.descendants.select(&:name) klass = klasses.find { |k| uri.to_s.include? k.base_uri.to_s } if klass object_id = uri.to_s.match(/[0-9a-fA-F]{24}/).to_s # Retrieve the object if it's persisted, otherwise return a new one (eg. embedded) return klass.parent.where(id: object_id).exists? ? klass.parent.find(object_id) : klass.parent.new end end |
Instance Method Details
#<<(statement) ⇒ void
This method returns an undefined value.
Push an RDF::Statement into the object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ladder/resource.rb', line 44 def <<(statement) # ActiveTriples::Resource expects: RDF::Statement, Hash, or Array statement = RDF::Statement.from(statement) unless statement.is_a? RDF::Statement # Only push statement if the statement's predicate is defined on the class field_name = field_from_predicate(statement.predicate) return unless field_name # If the object is a URI, see if it is a retrievable model object value = Ladder::Resource.from_uri(statement.object) if statement.object.is_a? RDF::URI # TODO: tidy this code # subject (RDF::Term) - A symbol is converted to an interned Node. # predicate (RDF::URI) # object (RDF::Resource) - if not a Resource, it is coerced to Literal or Node # depending on if it is a symbol or something other than a Term. value = yield if block_given? value ||= statement.object.to_s enum = send(field_name) if enum.is_a?(Enumerable) enum.send(:push, value) unless enum.include? value else send("#{field_name}=", value) end end |
#klass_from_predicate(predicate) ⇒ Ladder::Resource, ...
Retrieve the class for a relation, based on its defined RDF predicate
77 78 79 80 81 82 83 84 85 |
# File 'lib/ladder/resource.rb', line 77 def klass_from_predicate(predicate) field_name = field_from_predicate(predicate) return unless field_name relation = relations[field_name] return unless relation relation.class_name.constantize end |
#update_resource(opts = {}) ⇒ ActiveTriples::Resource
Update the delegated ActiveTriples::Resource from ActiveModel properties & relations
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ladder/resource.rb', line 28 def update_resource(opts = {}) resource_class.properties.each do |field_name, property| value = update_from_field(field_name) if fields[field_name] value = update_from_relation(field_name, opts[:related]) if relations[field_name] resource.set_value(property.predicate, value) # if value end resource end |