Class: Restify::Resource

Inherits:
Delegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/restify/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, response: nil, data: nil, relations: {}) ⇒ Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Resource.

API:

  • private



11
12
13
14
15
16
# File 'lib/restify/resource.rb', line 11

def initialize(context, response: nil, data: nil, relations: {})
  @data      = data
  @context   = context
  @response  = response
  @relations = relations
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



67
68
69
# File 'lib/restify/resource.rb', line 67

def response
  @response
end

Instance Method Details

#__getobj__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



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

def __getobj__
  @data
end

#_restify_relationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



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

def _restify_relations
  @relations
end

#_restify_response=(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



88
89
90
# File 'lib/restify/resource.rb', line 88

def _restify_response=(response)
  @response = response
end

#followRelation

Follow a LOCATION or CONTEXT-LOCATION header.

Returns:

  • Relation to follow resource.

Raises:

  • RuntimeError If nothing to follow.



74
75
76
77
78
79
80
# File 'lib/restify/resource.rb', line 74

def follow
  if relation? :_restify_follow
    relation :_restify_follow
  else
    raise RuntimeError.new 'Nothing to follow'
  end
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



93
94
95
96
97
98
99
100
# File 'lib/restify/resource.rb', line 93

def inspect
  text = {
    "@data" => @data,
    "@relations" => @relations
  }.map {|k,v | k + '=' + v.inspect }.join(' ')

  "#<#{self.class} #{text}>"
end

#relation(name) ⇒ Relation Also known as: rel

Return relation with given name.

Parameters:

  • Relation name.

Returns:

  • Relation.



42
43
44
45
46
47
48
# File 'lib/restify/resource.rb', line 42

def relation(name)
  if @relations.key? name
    Relation.new @context, @relations.fetch(name)
  else
    Relation.new @context, @relations.fetch(name.to_s)
  end
end

#relation?(name) ⇒ Boolean Also known as: rel?, has_rel?, has_relation?

Check if resource has a relation with given name.

Parameters:

  • Relation name.

Returns:

  • True if resource has relation, false otherwise.



29
30
31
# File 'lib/restify/resource.rb', line 29

def relation?(name)
  @relations.key?(name) || @relations.key?(name.to_s)
end