module Tripod::Resource
module for all domain objects that need to be persisted to the database
as resources
Attributes
Public Class Methods
Instantiate a Resource
.
@example Instantiate a new Resource
Person.new('http://swirrl.com/ric.rdf#me')
@example Instantiate a new Resource
in a particular graph
Person.new('http://swirrl.com/ric.rdf#me', :graph_uri => 'http://swirrl.com/graph/people')
@example Instantiate a new Resource
in a particular graph (DEPRECATED)
Person.new('http://swirrl.com/ric.rdf#me', 'http://swirrl.com/graph/people')
@param [ String, RDF::URI ] uri The uri of the resource. @param [ Hash, String, RDF::URI ] opts An options hash (see above), or the graph_uri
where this resource will be saved to. If graph URI is ommitted and can't be derived from the object's class, this resource cannot be persisted.
@return [ Resource
] A new Resource
# File lib/tripod/resource.rb, line 37 def initialize(uri, opts={}) if opts.is_a?(Hash) graph_uri = opts.fetch(:graph_uri, nil) ignore_graph = opts.fetch(:ignore_graph, false) else graph_uri = opts end raise Tripod::Errors::UriNotSet.new('uri missing') unless uri @uri = RDF::URI(uri.to_s) @repository = RDF::Repository.new @new_record = true run_callbacks :initialize do graph_uri ||= self.class.get_graph_uri unless ignore_graph @graph_uri = RDF::URI(graph_uri) if graph_uri set_rdf_type end end
Public Instance Methods
default comparison is via the uri
# File lib/tripod/resource.rb, line 58 def <=>(other) uri.to_s <=> other.uri.to_s end
performs equality checking on the uris
# File lib/tripod/resource.rb, line 63 def ==(other) self.class == other.class && uri.to_s == other.uri.to_s end
performs equality checking on the class
# File lib/tripod/resource.rb, line 69 def ===(other) other.class == Class ? self.class === other : self == other end
delegates to ==
# File lib/tripod/resource.rb, line 74 def eql?(other) self == (other) end
# File lib/tripod/resource.rb, line 78 def hash identity.hash end
a resource is absolutely identified by it's class and id.
# File lib/tripod/resource.rb, line 83 def identity [ self.class, self.uri.to_s ] end
# File lib/tripod/resource.rb, line 97 def to_a [ self ] end
Return the key value for the resource.
@example Return the key.
resource.to_key
@return [ Object ] The uri of the resource or nil if new.
# File lib/tripod/resource.rb, line 93 def to_key (persisted? || destroyed?) ? [ uri.to_s ] : nil end