Module: Ladder::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveTriples::Identifiable, Configurable, 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) ⇒ Object?
Push an RDF::Statement into the object.
-
#field_from_predicate(predicate) ⇒ String?
Retrieve the attribute name for a field or relation, based on its defined RDF predicate.
-
#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, #as_turtle
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
309 310 311 312 313 314 315 316 317 318 |
# File 'lib/ladder/resource.rb', line 309 def self.from_uri(uri) klass = Ladder::Config.models.find { |k| uri.to_s.include? k.resource_class.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.where(id: object_id).exists? ? klass.find(object_id) : klass.new end end |
Instance Method Details
#<<(statement) ⇒ Object?
Push an RDF::Statement into the object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ladder/resource.rb', line 43 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 objects = statement.object.is_a?(RDF::Node) && block_given? ? yield : statement.object update_field(field_name, *objects) if fields[field_name] update_relation(field_name, *objects) if relations[field_name] end |
#field_from_predicate(predicate) ⇒ String?
Retrieve the attribute name for a field or relation, based on its defined RDF predicate
78 79 80 81 82 83 |
# File 'lib/ladder/resource.rb', line 78 def field_from_predicate(predicate) defined_prop = resource_class.properties.find { |_field_name, term| term.predicate == predicate } return unless defined_prop defined_prop.first end |
#klass_from_predicate(predicate) ⇒ Ladder::Resource, ...
Retrieve the class for a relation, based on its defined RDF predicate
62 63 64 65 66 67 68 69 70 |
# File 'lib/ladder/resource.rb', line 62 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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ladder/resource.rb', line 27 def update_resource(opts = {}) resource_class.properties.each do |field_name, property| values = update_from_field(field_name) if fields[field_name] values = update_from_relation(field_name, opts[:related]) if relations[field_name] resource.set_value(property.predicate, values) end resource end |