module Tripod::Resource

module for all domain objects that need to be persisted to the database

 as resources

Attributes

graph_uri[R]
new_record[R]
uri[R]

Public Class Methods

new(uri, opts={}) click to toggle source

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

<=>(other) click to toggle source

 default comparison is via the uri

# File lib/tripod/resource.rb, line 58
def <=>(other)
  uri.to_s <=> other.uri.to_s
end
==(other) click to toggle source

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
===(other) click to toggle source

performs equality checking on the class

# File lib/tripod/resource.rb, line 69
def ===(other)
  other.class == Class ? self.class === other : self == other
end
eql?(other) click to toggle source

delegates to ==

# File lib/tripod/resource.rb, line 74
def eql?(other)
  self == (other)
end
hash() click to toggle source
# File lib/tripod/resource.rb, line 78
def hash
  identity.hash
end
identity() click to toggle source

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
to_a() click to toggle source
# File lib/tripod/resource.rb, line 97
def to_a
  [ self ]
end
to_key() click to toggle source

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