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.



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.



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.



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

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.



100
101
102
# File 'lib/restify/resource.rb', line 100

def _restify_response=(response)
  @response = response
end

#followRelation

Follow a LOCATION or CONTEXT-LOCATION header.

Returns:

  • (Relation)

    Relation to follow resource or nil.



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

def follow
  if relation? :_restify_follow
    relation :_restify_follow
  else
    nil
  end
end

#follow!Relation

Follow a LOCATION or CONTEXT-LOCATION header.

Returns:

  • (Relation)

    Relation to follow resource.

Raises:

  • RuntimeError If nothing to follow.



86
87
88
89
90
91
92
# File 'lib/restify/resource.rb', line 86

def follow!
  if (rel = follow)
    rel
  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.



105
106
107
108
109
110
111
112
# File 'lib/restify/resource.rb', line 105

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:

  • name (String, Symbol)

    Relation name.

Returns:



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:

  • name (String, Symbol)

    Relation name.

Returns:

  • (Boolean)

    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