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] unless @meta

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

    setup_from_clone(resource)
    define_attributes
  end

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

Instance Method Details

#api_responseObject

Return Response object which created this instance.



97
98
99
# File 'lib/haveapi/client/resource_instance.rb', line 97

def api_response
  @response
end

#attributesObject

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



102
103
104
# File 'lib/haveapi/client/resource_instance.rb', line 102

def attributes
  @params
end

#newObject

Raises:

  • (NoMethodError)


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

def new
  raise NoMethodError.new
end

#resolveObject

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



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

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
70
71
72
# 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)))

    if @response.ok?
      @params = @response.response
      define_attributes

    else
      return nil
    end

    @persistent = true
    self
  end
end

#save!Object

Call #save and raise ActionFailed if it fails.

Raises:



75
76
77
78
# File 'lib/haveapi/client/resource_instance.rb', line 75

def save!
  raise ActionFailed.new(@response) if save.nil?
  self
end

#to_sObject



106
107
108
# File 'lib/haveapi/client/resource_instance.rb', line 106

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