Class: HaveAPI::Client::ResourceInstance
- 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
-
#api_response ⇒ Object
Return Response object which created this instance.
-
#attributes ⇒ Object
Return a hash of all object attributes retrieved from the API.
-
#initialize(client, api, resource, action: nil, response: nil, resolved: false, meta: nil, persistent: true) ⇒ ResourceInstance
constructor
A new instance of ResourceInstance.
- #new ⇒ Object
-
#resolve ⇒ Object
Resolve the object (fetch it from the API) if it is not resolved yet.
-
#save ⇒ Object
Invoke
create
action if the object is not persistent,update
action if it is. -
#save! ⇒ Object
Call #save and raise ActionFailed if it fails.
- #to_s ⇒ Object
Methods inherited from Resource
#_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.
10 11 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 |
# File 'lib/haveapi/client/resource_instance.rb', line 10 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 = @persistent = persistent @resource_instances = {} if response if response.is_a?(Hash) @params = response else @response = response @params = response.response 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_response ⇒ Object
Return Response object which created this instance.
90 91 92 |
# File 'lib/haveapi/client/resource_instance.rb', line 90 def api_response @response end |
#attributes ⇒ Object
Return a hash of all object attributes retrieved from the API.
95 96 97 |
# File 'lib/haveapi/client/resource_instance.rb', line 95 def attributes @params end |
#new ⇒ Object
41 42 43 |
# File 'lib/haveapi/client/resource_instance.rb', line 41 def new raise NoMethodError.new end |
#resolve ⇒ Object
Resolve the object (fetch it from the API) if it is not resolved yet.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/haveapi/client/resource_instance.rb', line 75 def resolve return self if @resolved @action.provide_args(*@meta[:url_params]) @response = Response.new(@action, @action.execute({})) @params = @response.response setup_from_clone(@resource) define_attributes @resolved = true self end |
#save ⇒ Object
Invoke create
action if the object is not persistent, update
action if it is.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/haveapi/client/resource_instance.rb', line 47 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.
69 70 71 72 |
# File 'lib/haveapi/client/resource_instance.rb', line 69 def save! raise ActionFailed.new(@response) if save.nil? self end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/haveapi/client/resource_instance.rb', line 99 def to_s "<#{self.class.to_s}:#{object_id}:#{@resource._name}>" end |