Class: ActiveTriples::Resource
- Inherits:
-
RDF::Graph
- Object
- RDF::Graph
- ActiveTriples::Resource
- Extended by:
- 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! ⇒ 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)
74 75 76 77 78 79 80 81 82 |
# File 'lib/active_triples/resource.rb', line 74 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.
51 52 53 |
# File 'lib/active_triples/resource.rb', line 51 def from_uri(uri,vals=nil) new(uri, vals) end |
.type_registry ⇒ Object
38 39 40 |
# File 'lib/active_triples/resource.rb', line 38 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.
311 312 313 |
# File 'lib/active_triples/resource.rb', line 311 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.
288 289 290 |
# File 'lib/active_triples/resource.rb', line 288 def []=(uri_or_term_property, value) self[uri_or_term_property].set(value) end |
#attributes ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/active_triples/resource.rb', line 105 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/active_triples/resource.rb', line 124 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?
179 180 181 |
# File 'lib/active_triples/resource.rb', line 179 def base_uri self.class.base_uri end |
#destroy ⇒ Object Also known as: destroy!
354 355 356 357 358 359 |
# File 'lib/active_triples/resource.rb', line 354 def destroy clear persist! if repository parent.destroy_child(self) if parent @destroyed = true end |
#destroy_child(child) ⇒ Object
370 371 372 373 374 |
# File 'lib/active_triples/resource.rb', line 370 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.
366 367 368 |
# File 'lib/active_triples/resource.rb', line 366 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
149 150 151 152 153 154 155 |
# File 'lib/active_triples/resource.rb', line 149 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"
225 226 227 228 |
# File 'lib/active_triples/resource.rb', line 225 def fetch load(rdf_subject) self end |
#fields ⇒ Array<Symbol>
Lists fields registered as properties on the object.
209 210 211 |
# File 'lib/active_triples/resource.rb', line 209 def fields properties.keys.map(&:to_sym).reject{|x| x == :type} end |
#final_parent ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/active_triples/resource.rb', line 95 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
316 317 318 319 320 321 |
# File 'lib/active_triples/resource.rb', line 316 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)
303 304 305 |
# File 'lib/active_triples/resource.rb', line 303 def get_values(*args) get_term(args) end |
#graph ⇒ self
redundant, simply returns self.
Returns the current object.
90 91 92 93 |
# File 'lib/active_triples/resource.rb', line 90 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
167 168 169 |
# File 'lib/active_triples/resource.rb', line 167 def id node? ? nil : rdf_subject.to_s end |
#mark_for_destruction ⇒ Object
390 391 392 |
# File 'lib/active_triples/resource.rb', line 390 def mark_for_destruction @marked_for_destruction = true end |
#marked_for_destruction? ⇒ Boolean
394 395 396 |
# File 'lib/active_triples/resource.rb', line 394 def marked_for_destruction? @marked_for_destruction end |
#new_record? ⇒ true, false
Indicates if the record is ‘new’ (has not yet been persisted).
380 381 382 |
# File 'lib/active_triples/resource.rb', line 380 def new_record? not persisted? end |
#node? ⇒ Boolean
171 172 173 174 |
# File 'lib/active_triples/resource.rb', line 171 def node? return true if rdf_subject.kind_of? RDF::Node false end |
#persist! ⇒ Object
230 231 232 233 234 235 |
# File 'lib/active_triples/resource.rb', line 230 def persist! raise "failed when trying to persist to non-existant repository or parent resource" unless repository erase_old_resource repository << self @persisted = true end |
#persisted? ⇒ true, false
Indicates if the resource is persisted.
242 243 244 245 246 |
# File 'lib/active_triples/resource.rb', line 242 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.
195 196 197 198 199 200 201 202 203 |
# File 'lib/active_triples/resource.rb', line 195 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
160 161 162 |
# File 'lib/active_triples/resource.rb', line 160 def rdf_subject @rdf_subject ||= RDF::Node.new end |
#reflections ⇒ Object
120 121 122 |
# File 'lib/active_triples/resource.rb', line 120 def reflections self.class end |
#reload ⇒ true, false
Repopulates the graph from the repository or parent resource.
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/active_triples/resource.rb', line 252 def reload @term_cache ||= {} if self.class.repository == :parent return false if final_parent.nil? end self << repository.query(subject: rdf_subject) unless empty? @persisted = true end true end |
#serializable_hash(options = nil) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/active_triples/resource.rb', line 113 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.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/active_triples/resource.rb', line 335 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)
275 276 277 278 279 280 281 282 |
# File 'lib/active_triples/resource.rb', line 275 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
386 387 388 |
# File 'lib/active_triples/resource.rb', line 386 def solrize node? ? rdf_label : rdf_subject.to_s end |
#type ⇒ Object
183 184 185 |
# File 'lib/active_triples/resource.rb', line 183 def type self.get_values(:type).to_a end |
#type=(type) ⇒ Object
187 188 189 190 |
# File 'lib/active_triples/resource.rb', line 187 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.
60 61 62 |
# File 'lib/active_triples/resource.rb', line 60 def writable? !frozen? end |