Class: Asana::Resources::Resource
- Inherits:
-
Object
- Object
- Asana::Resources::Resource
- Extended by:
- ResponseHelper
- Includes:
- ResponseHelper
- Defined in:
- lib/asana/resource_includes/resource.rb
Overview
The base resource class which provides some sugar over common resource functionality.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data, client: required('client')) ⇒ Resource
constructor
A new instance of Resource.
-
#method_missing(m, *args) ⇒ Object
private
Proxies method calls to the data, wrapping it accordingly and caching the result by defining a real reader method.
-
#refresh ⇒ Object
If it has findById, it implements #refresh.
-
#respond_to_missing?(m) ⇒ Boolean
private
Guard for the method_missing proxy.
-
#to_h ⇒ Object
The raw Hash representation of the data.
- #to_s ⇒ Object (also: #inspect)
Methods included from ResponseHelper
Constructor Details
#initialize(data, client: required('client')) ⇒ Resource
Returns a new instance of Resource.
16 17 18 19 20 21 22 |
# File 'lib/asana/resource_includes/resource.rb', line 16 def initialize(data, client: required('client')) @_client = client @_data = data data.each do |k, v| instance_variable_set(:"@#{k}", v) if respond_to?(k) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ 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.
Proxies method calls to the data, wrapping it accordingly and caching the result by defining a real reader method.
39 40 41 42 |
# File 'lib/asana/resource_includes/resource.rb', line 39 def method_missing(m, *args) super unless respond_to_missing?(m, *args) cache(m, wrapped(to_h[m.to_s])) end |
Class Method Details
Instance Method Details
#refresh ⇒ Object
If it has findById, it implements #refresh
25 26 27 28 29 30 31 |
# File 'lib/asana/resource_includes/resource.rb', line 25 def refresh if self.class.respond_to?(:find_by_id) self.class.find_by_id(client, id) else fail "#{self.class.name} does not respond to #find_by_id" end end |
#respond_to_missing?(m) ⇒ Boolean
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.
Guard for the method_missing proxy. Checks if the resource actually has a specific piece of data at all.
48 49 50 |
# File 'lib/asana/resource_includes/resource.rb', line 48 def respond_to_missing?(m, *) to_h.key?(m.to_s) end |
#to_h ⇒ Object
Returns the raw Hash representation of the data.
54 55 56 |
# File 'lib/asana/resource_includes/resource.rb', line 54 def to_h @_data end |
#to_s ⇒ Object Also known as: inspect
58 59 60 61 |
# File 'lib/asana/resource_includes/resource.rb', line 58 def to_s attrs = to_h.map { |k, _| "#{k}: #{public_send(k).inspect}" }.join(', ') "#<Asana::#{self.class.name.split('::').last} #{attrs}>" end |