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 Graphs

#graphs

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

#build_multifield_query, #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 Dirty

#attribute_change, #attribute_changed?, #attribute_will_change!, #changed, #changed_attributes, #changes, #post_persist

Methods included from Embeds

#embedded_are_valid, #get_embeds

Methods included from Fields

#fields, included

Methods included from Persistence

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

Methods included from RdfType

#set_rdf_type

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.



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

def graph_uri
  @graph_uri
end

#new_recordObject (readonly)

Returns the value of attribute new_record.



20
21
22
# File 'lib/tripod/resource.rb', line 20

def new_record
  @new_record
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#<=>(other) ⇒ Object

 default comparison is via the uri



58
59
60
# File 'lib/tripod/resource.rb', line 58

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

#==(other) ⇒ Object

performs equality checking on the uris



63
64
65
66
# File 'lib/tripod/resource.rb', line 63

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

#===(other) ⇒ Object

performs equality checking on the class



69
70
71
# File 'lib/tripod/resource.rb', line 69

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

#eql?(other) ⇒ Boolean

delegates to ==

Returns:

  • (Boolean)


74
75
76
# File 'lib/tripod/resource.rb', line 74

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

#hashObject



78
79
80
# File 'lib/tripod/resource.rb', line 78

def hash
  identity.hash
end

#identityObject

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



83
84
85
# File 'lib/tripod/resource.rb', line 83

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:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#to_aObject



97
98
99
# File 'lib/tripod/resource.rb', line 97

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.



93
94
95
# File 'lib/tripod/resource.rb', line 93

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