Class: ActiveTriples::Resource
- Inherits:
-
RDF::Graph
- Object
- RDF::Graph
- ActiveTriples::Resource
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming, ActiveModel::Translation, Configurable, Deprecation
- Includes:
- ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations, NestedAttributes, Properties, Reflection
- Defined in:
- lib/active_triples/resource.rb
Overview
Defines a generic RDF ‘Resource` as an RDF::Graph with property configuration, accessors, and some other methods for managing resources as discrete subgraphs which can be maintained by a Hydra datastream model.
Resources can be instances of ActiveTriples::Resource directly, but more often they will be instances of subclasses with registered properties and configuration. e.g.
class License < Resource
configure repository: :default
property :title, predicate: RDF::DC.title, class_name: RDF::Literal do |index|
index.as :displayable, :facetable
end
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
-
.from_uri(uri, vals = nil) ⇒ ActiveTriples::Resource
Adapter for a consistent interface for creating a new Resource from a URI.
- .type_registry ⇒ Object
Instance Method Summary collapse
-
#[](uri_or_term_property) ⇒ Object
Returns an array of values belonging to the property requested.
-
#[]=(uri_or_term_property, value) ⇒ Object
Adds or updates a property with supplied values.
- #attributes ⇒ Object
- #attributes=(values) ⇒ Object
-
#base_uri ⇒ String?
The base URI the resource will use when setting its subject.
- #destroy ⇒ Object (also: #destroy!)
- #destroy_child(child) ⇒ Object
-
#destroyed? ⇒ true, false
Indicates if the Resource has been destroyed.
-
#dump(*args) ⇒ String
Returns a serialized string representation of self.
-
#fetch ⇒ ActiveTriples::Resource
Load data from the #rdf_subject URI.
-
#fields ⇒ Array<Symbol>
Lists fields registered as properties on the object.
- #final_parent ⇒ Object
- #get_term(args) ⇒ Object
-
#get_values(*args) ⇒ Object
Returns an array of values belonging to the property requested.
-
#graph ⇒ self
deprecated
Deprecated.
redundant, simply returns self.
-
#id ⇒ Object
A string identifier for the resource.
-
#initialize(*args, &block) ⇒ Resource
constructor
Initialize an instance of this resource class.
- #mark_for_destruction ⇒ Object
- #marked_for_destruction? ⇒ Boolean
-
#new_record? ⇒ true, false
Indicates if the record is ‘new’ (has not yet been persisted).
- #node? ⇒ Boolean
- #persist!(opts = {}) ⇒ Object
-
#persisted? ⇒ true, false
Indicates if the resource is persisted.
-
#rdf_label ⇒ Object
Looks for labels in various default fields, prioritizing configured label fields.
-
#rdf_subject ⇒ RDF::URI, RDF::Node
(also: #to_term)
A URI or Node which the resource’s properties are about.
- #reflections ⇒ Object
-
#reload ⇒ true, false
Repopulates the graph from the repository or parent resource.
- #serializable_hash(options = nil) ⇒ Object
-
#set_subject!(uri_or_str) ⇒ Object
Set a new rdf_subject for the resource.
-
#set_value(*args) ⇒ Object
Adds or updates a property with supplied values.
-
#solrize ⇒ String
The string representation of the resource.
- #type ⇒ Object
- #type=(type) ⇒ Object
-
#writable? ⇒ true, false
Specifies whether the object is currently writable.
Methods included from Configurable
configure, rdf_type, transform_type
Methods included from Reflection
Constructor Details
#initialize(*args, &block) ⇒ Resource
Initialize an instance of this resource class. Defaults to a blank node subject. In addition to RDF::Graph parameters, you can pass in a URI and/or a parent to build a resource from a existing data.
You can pass in only a parent with:
Resource.new(nil, parent)
75 76 77 78 79 80 81 82 83 |
# File 'lib/active_triples/resource.rb', line 75 def initialize(*args, &block) resource_uri = args.shift unless args.first.is_a?(Hash) self.parent = args.shift unless args.first.is_a?(Hash) set_subject!(resource_uri) if resource_uri super(*args, &block) reload # Append type to graph if necessary. self.get_values(:type) << self.class.type if self.class.type.kind_of?(RDF::URI) && type.empty? end |
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
35 36 37 |
# File 'lib/active_triples/resource.rb', line 35 def parent @parent end |
Class Method Details
.from_uri(uri, vals = nil) ⇒ ActiveTriples::Resource
Adapter for a consistent interface for creating a new Resource from a URI. Similar functionality should exist in all objects which can become a Resource.
52 53 54 |
# File 'lib/active_triples/resource.rb', line 52 def from_uri(uri,vals=nil) new(uri, vals) end |
.type_registry ⇒ Object
39 40 41 |
# File 'lib/active_triples/resource.rb', line 39 def type_registry @@type_registry ||= {} end |
Instance Method Details
#[](uri_or_term_property) ⇒ Object
Returns an array of values belonging to the property requested. Elements in the array may RdfResource objects or a valid datatype.
317 318 319 |
# File 'lib/active_triples/resource.rb', line 317 def [](uri_or_term_property) get_term([uri_or_term_property]) end |
#[]=(uri_or_term_property, value) ⇒ Object
This method will delete existing statements with the correct subject and predicate from the graph
Adds or updates a property with supplied values.
294 295 296 |
# File 'lib/active_triples/resource.rb', line 294 def []=(uri_or_term_property, value) self[uri_or_term_property].set(value) end |
#attributes ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/active_triples/resource.rb', line 106 def attributes attrs = {} attrs['id'] = id if id fields.map { |f| attrs[f.to_s] = get_values(f) } unregistered_predicates.map { |uri| attrs[uri.to_s] = get_values(uri) } attrs end |
#attributes=(values) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_triples/resource.rb', line 125 def attributes=(values) raise ArgumentError, "values must be a Hash, you provided #{values.class}" unless values.kind_of? Hash values = values.with_indifferent_access set_subject!(values.delete(:id)) if values.has_key?(:id) and node? values.each do |key, value| if reflections.reflect_on_property(key) set_value(rdf_subject, key, value) elsif .keys.map { |k| "#{k}_attributes" }.include?(key) send("#{key}=".to_sym, value) else raise ArgumentError, "No association found for name `#{key}'. Has it been defined yet?" end end end |
#base_uri ⇒ String?
Returns the base URI the resource will use when setting its subject. ‘nil` if none is used.
180 181 182 |
# File 'lib/active_triples/resource.rb', line 180 def base_uri self.class.base_uri end |
#destroy ⇒ Object Also known as: destroy!
360 361 362 363 364 365 |
# File 'lib/active_triples/resource.rb', line 360 def destroy clear persist! if repository parent.destroy_child(self) if parent @destroyed = true end |
#destroy_child(child) ⇒ Object
376 377 378 379 380 |
# File 'lib/active_triples/resource.rb', line 376 def destroy_child(child) statements.each do |statement| delete_statement(statement) if statement.subject == child.rdf_subject || statement.object == child.rdf_subject end end |
#destroyed? ⇒ true, false
Indicates if the Resource has been destroyed.
372 373 374 |
# File 'lib/active_triples/resource.rb', line 372 def destroyed? @destroyed ||= false end |
#dump(*args) ⇒ String
Returns a serialized string representation of self. Extends the base implementation builds a JSON-LD context if the specified format is :jsonld and a context is provided by #jsonld_context
150 151 152 153 154 155 156 |
# File 'lib/active_triples/resource.rb', line 150 def dump(*args) if args.first == :jsonld and respond_to?(:jsonld_context) args << {} unless args.last.is_a?(Hash) args.last[:context] ||= jsonld_context end super end |
#fetch ⇒ ActiveTriples::Resource
Load data from the #rdf_subject URI. Retrieved data will be parsed into the Resource’s graph from available RDF::Readers and available from property accessors if if predicates are registered.
osu = ActiveTriples::Resource.new('http://dbpedia.org/resource/Oregon_State_University')
osu.fetch
osu.rdf_label.first
# => "Oregon State University"
226 227 228 229 |
# File 'lib/active_triples/resource.rb', line 226 def fetch load(rdf_subject) self end |
#fields ⇒ Array<Symbol>
Lists fields registered as properties on the object.
210 211 212 |
# File 'lib/active_triples/resource.rb', line 210 def fields properties.keys.map(&:to_sym).reject{|x| x == :type} end |
#final_parent ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/active_triples/resource.rb', line 96 def final_parent @final_parent ||= begin parent = self.parent while parent && parent.parent && parent.parent != parent parent = parent.parent end parent end end |
#get_term(args) ⇒ Object
322 323 324 325 326 327 |
# File 'lib/active_triples/resource.rb', line 322 def get_term(args) @term_cache ||= {} term = Term.new(self, args) @term_cache["#{term.send(:rdf_subject)}/#{term.property}/#{term.term_args}"] ||= term @term_cache["#{term.send(:rdf_subject)}/#{term.property}/#{term.term_args}"] end |
#get_values(*args) ⇒ Object
Returns an array of values belonging to the property requested. Elements in the array may RdfResource objects or a valid datatype.
Handles two argument patterns. The recommended pattern is:
get_values(property)
For backwards compatibility, there is support for explicitly passing the rdf_subject to be used in th statement:
get_values(uri, property)
309 310 311 |
# File 'lib/active_triples/resource.rb', line 309 def get_values(*args) get_term(args) end |
#graph ⇒ self
redundant, simply returns self.
Returns the current object.
91 92 93 94 |
# File 'lib/active_triples/resource.rb', line 91 def graph Deprecation.warn Resource, "graph is redundant & deprecated. It will be removed in ActiveTriples 0.2.0.", caller self end |
#id ⇒ Object
A string identifier for the resource
168 169 170 |
# File 'lib/active_triples/resource.rb', line 168 def id node? ? nil : rdf_subject.to_s end |
#mark_for_destruction ⇒ Object
396 397 398 |
# File 'lib/active_triples/resource.rb', line 396 def mark_for_destruction @marked_for_destruction = true end |
#marked_for_destruction? ⇒ Boolean
400 401 402 |
# File 'lib/active_triples/resource.rb', line 400 def marked_for_destruction? @marked_for_destruction end |
#new_record? ⇒ true, false
Indicates if the record is ‘new’ (has not yet been persisted).
386 387 388 |
# File 'lib/active_triples/resource.rb', line 386 def new_record? not persisted? end |
#node? ⇒ Boolean
172 173 174 175 |
# File 'lib/active_triples/resource.rb', line 172 def node? return true if rdf_subject.kind_of? RDF::Node false end |
#persist!(opts = {}) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/active_triples/resource.rb', line 231 def persist!(opts={}) return if @persisting return false if opts[:validate] && !valid? @persisting = true run_callbacks :persist do raise "failed when trying to persist to non-existant repository or parent resource" unless repository erase_old_resource repository << self @persisted = true end @persisting = false true end |
#persisted? ⇒ true, false
Indicates if the resource is persisted.
250 251 252 253 254 |
# File 'lib/active_triples/resource.rb', line 250 def persisted? @persisted ||= false return (@persisted and parent.persisted?) if parent @persisted end |
#rdf_label ⇒ Object
Looks for labels in various default fields, prioritizing configured label fields.
196 197 198 199 200 201 202 203 204 |
# File 'lib/active_triples/resource.rb', line 196 def rdf_label labels = Array(self.class.rdf_label) labels += default_labels labels.each do |label| values = get_values(label) return values unless values.empty? end node? ? [] : [rdf_subject.to_s] end |
#rdf_subject ⇒ RDF::URI, RDF::Node Also known as: to_term
Returns a URI or Node which the resource’s properties are about.
161 162 163 |
# File 'lib/active_triples/resource.rb', line 161 def rdf_subject @rdf_subject ||= RDF::Node.new end |
#reflections ⇒ Object
121 122 123 |
# File 'lib/active_triples/resource.rb', line 121 def reflections self.class end |
#reload ⇒ true, false
Repopulates the graph from the repository or parent resource.
260 261 262 263 264 265 266 267 268 |
# File 'lib/active_triples/resource.rb', line 260 def reload @term_cache ||= {} return false unless repository self << repository.query(subject: rdf_subject) unless empty? @persisted = true end true end |
#serializable_hash(options = nil) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/active_triples/resource.rb', line 114 def serializable_hash( = nil) attrs = (fields.map { |f| f.to_s }) << 'id' hash = super(:only => attrs) unregistered_predicates.map { |uri| hash[uri.to_s] = get_values(uri) } hash end |
#set_subject!(uri_or_str) ⇒ Object
Set a new rdf_subject for the resource.
This raises an error if the current subject is not a blank node, and returns false if it can’t figure out how to make a URI from the param. Otherwise it creates a URI for the resource and rebuilds the graph with the updated URI.
Will try to build a uri as an extension of the class’s base_uri if appropriate.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/active_triples/resource.rb', line 341 def set_subject!(uri_or_str) raise "Refusing update URI when one is already assigned!" unless node? or rdf_subject == RDF::URI(nil) # Refusing set uri to an empty string. return false if uri_or_str.nil? or (uri_or_str.to_s.empty? and not uri_or_str.kind_of? RDF::URI) # raise "Refusing update URI! This object is persisted to a datastream." if persisted? old_subject = rdf_subject @rdf_subject = get_uri(uri_or_str) each_statement do |statement| if statement.subject == old_subject delete(statement) self << RDF::Statement.new(rdf_subject, statement.predicate, statement.object) elsif statement.object == old_subject delete(statement) self << RDF::Statement.new(statement.subject, statement.predicate, rdf_subject) end end end |
#set_value(*args) ⇒ Object
This method will delete existing statements with the correct subject and predicate from the graph
Adds or updates a property with supplied values.
Handles two argument patterns. The recommended pattern is:
set_value(property, values)
For backwards compatibility, there is support for explicitly passing the rdf_subject to be used in the statement:
set_value(uri, property, values)
281 282 283 284 285 286 287 288 |
# File 'lib/active_triples/resource.rb', line 281 def set_value(*args) # Add support for legacy 3-parameter syntax if args.length > 3 || args.length < 2 raise ArgumentError, "wrong number of arguments (#{args.length} for 2-3)" end values = args.pop get_term(args).set(values) end |
#solrize ⇒ String
Returns the string representation of the resource.
392 393 394 |
# File 'lib/active_triples/resource.rb', line 392 def solrize node? ? rdf_label : rdf_subject.to_s end |
#type ⇒ Object
184 185 186 |
# File 'lib/active_triples/resource.rb', line 184 def type self.get_values(:type).to_a end |
#type=(type) ⇒ Object
188 189 190 191 |
# File 'lib/active_triples/resource.rb', line 188 def type=(type) raise "Type must be an RDF::URI" unless type.kind_of? RDF::URI self.update(RDF::Statement.new(rdf_subject, RDF.type, type)) end |
#writable? ⇒ true, false
Specifies whether the object is currently writable.
61 62 63 |
# File 'lib/active_triples/resource.rb', line 61 def writable? !frozen? end |