Module: Tripod::Resource

Extended by:
ActiveSupport::Concern
Includes:
Components
Defined in:
lib/tripod/resource.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Attributes included from State

#destroyed

Attributes included from EagerLoading

#object_resources, #predicate_resources

Attributes included from Repository

#repository

Instance Method Summary collapse

Methods included from State

#destroyed?, #new_record?, #persisted?

Methods included from Serialization

#to_json, #to_nt, #to_rdf, #to_text, #to_ttl

Methods included from EagerLoading

#eager_load_object_triples!, #eager_load_predicate_triples!, #get_related_resource, #has_related_resource?

Methods included from Repository

#get_triples_for_this_resource, #hydrate!, #repository_as_graph, #retrieve_triples_from_database

Methods included from Persistence

#destroy, #save, #save!, #update_attribute, #update_attributes

Methods included from Attributes

#read_attribute, #write_attribute

Methods included from Predicates

#append_to_predicate, #predicates, #read_predicate, #remove_predicate, #write_predicate

Instance Attribute Details

#graph_uriObject (readonly)

Returns the value of attribute graph_uri.



23
24
25
# File 'lib/tripod/resource.rb', line 23

def graph_uri
  @graph_uri
end

#new_recordObject (readonly)

Returns the value of attribute new_record.



22
23
24
# File 'lib/tripod/resource.rb', line 22

def new_record
  @new_record
end

#uriObject (readonly)

Returns the value of attribute uri.



24
25
26
# File 'lib/tripod/resource.rb', line 24

def uri
  @uri
end

Instance Method Details

#<=>(other) ⇒ Object

 default comparison is via the uri



60
61
62
# File 'lib/tripod/resource.rb', line 60

def <=>(other)
  uri.to_s <=> uri.to_s
end

#==(other) ⇒ Object

performs equality checking on the uris



65
66
67
68
# File 'lib/tripod/resource.rb', line 65

def ==(other)
  self.class == other.class &&
    uri.to_s == other.uri.to_s
end

#===(other) ⇒ Object

performs equality checking on the class



71
72
73
# File 'lib/tripod/resource.rb', line 71

def ===(other)
  other.class == Class ? self.class === other : self == other
end

#eql?(other) ⇒ Boolean

delegates to ==

Returns:

  • (Boolean)


76
77
78
# File 'lib/tripod/resource.rb', line 76

def eql?(other)
  self == (other)
end

#hashObject



80
81
82
# File 'lib/tripod/resource.rb', line 80

def hash
  identity.hash
end

#identityObject

a resource is absolutely identified by it’s class and id.



85
86
87
# File 'lib/tripod/resource.rb', line 85

def identity
  [ self.class, self.uri.to_s ]
end

#initialize(uri, opts = {}) ⇒ Resource

Instantiate a Resource.

Examples:

Instantiate a new Resource

Person.new('http://swirrl.com/ric.rdf#me')

Instantiate a new Resource in a particular graph

Person.new('http://swirrl.com/ric.rdf#me', :graph_uri => 'http://swirrl.com/graph/people')

Instantiate a new Resource in a particular graph (DEPRECATED)

Person.new('http://swirrl.com/ric.rdf#me', 'http://swirrl.com/graph/people')

Parameters:

  • uri (String, RDF::URI)

    The uri of the resource.

  • opts (Hash, String, RDF::URI) (defaults to: {})

    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.

Returns:

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tripod/resource.rb', line 39

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
    self.rdf_type = self.class.get_rdf_type if respond_to?(:rdf_type=) && self.class.get_rdf_type
  end
end

#to_aObject



99
100
101
# File 'lib/tripod/resource.rb', line 99

def to_a
  [ self ]
end

#to_keyObject

Return the key value for the resource.

Examples:

Return the key.

resource.to_key

Returns:

  • (Object)

    The uri of the resource or nil if new.



95
96
97
# File 'lib/tripod/resource.rb', line 95

def to_key
  (persisted? || destroyed?) ? [ uri.to_s ] : nil
end