Class: HaveAPI::Client::ResourceInstance

Inherits:
Resource
  • Object
show all
Defined in:
lib/haveapi/client/resource_instance.rb

Overview

Instance of an object from the API. An instance of this class may be in three states:

  • resolved/persistent - the instance was created by an action that retrieved it from the API.

  • unresolved - this instance is an attribute of another instance that was resolved and will be resolved when first accessed.

  • not persistent - created by Resource.new, the object was not yet sent to the API.

Instance Attribute Summary

Attributes inherited from Resource

#actions, #prepared_args, #resources

Instance Method Summary collapse

Methods inherited from Resource

#_description, #_name, #inspect, #setup, #setup_from_clone

Constructor Details

#initialize(client, api, resource, action: nil, response: nil, resolved: false, meta: nil, persistent: true) ⇒ ResourceInstance

Returns a new instance of ResourceInstance.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haveapi/client/resource_instance.rb', line 12

def initialize(client, api, resource, action: nil, response: nil,
               resolved: false, meta: nil, persistent: true)
  super(client, api, resource._name)

  @action = action
  @resource = resource
  @resolved = resolved
  @meta = meta
  @persistent = persistent
  @resource_instances = {}

  if response
    if response.is_a?(Hash)
      @params = response
      @prepared_args = response[:_meta][:path_params]
      @meta ||= response[:_meta]

    else
      @response = response
      @params = response.response
      @prepared_args = response.meta[:path_params]
      @meta ||= response.meta
    end

    setup_from_clone(resource)
    define_attributes
  end

  return if @persistent

  setup_from_clone(resource)
  define_implicit_attributes
  define_attributes(@action.input_params)
end

Instance Method Details

#api_responseObject

Return Response object which created this instance.



95
96
97
# File 'lib/haveapi/client/resource_instance.rb', line 95

def api_response
  @response
end

#attributesObject

Return a hash of all object attributes retrieved from the API.



100
101
102
# File 'lib/haveapi/client/resource_instance.rb', line 100

def attributes
  @params
end

#newObject

Raises:

  • (NoMethodError)


47
48
49
# File 'lib/haveapi/client/resource_instance.rb', line 47

def new
  raise NoMethodError
end

#resolveObject

Resolve the object (fetch it from the API) if it is not resolved yet.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/haveapi/client/resource_instance.rb', line 79

def resolve
  return self if @resolved

  @action.provide_args(*@meta[:path_params])
  @response = Response.new(@action, @action.execute({}))
  @params = @response.response

  setup_from_clone(@resource)
  define_attributes

  @prepared_args = @response.meta[:path_params]
  @resolved = true
  self
end

#saveObject

Invoke create action if the object is not persistent, update action if it is.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/haveapi/client/resource_instance.rb', line 53

def save
  if @persistent
    method(:update).call

  else
    @action.provide_args
    @response = Response.new(@action, @action.execute(attributes_for_api(@action)))

    return nil unless @response.ok?

    @params = @response.response
    define_attributes

    @persistent = true
    self
  end
end

#save!Object

Call #save and raise ActionFailed if it fails.

Raises:



72
73
74
75
76
# File 'lib/haveapi/client/resource_instance.rb', line 72

def save!
  raise ActionFailed, @response if save.nil?

  self
end

#to_sObject



104
105
106
# File 'lib/haveapi/client/resource_instance.rb', line 104

def to_s
  "<#{self.class}:#{object_id}:#{@resource._name}>"
end