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.
Direct Known Subclasses
AttachmentsBase, BatchAPIBase, CustomFieldSettingsBase, CustomFieldsBase, Event, EventsBase, JobsBase, OrganizationExportsBase, PortfolioMembershipsBase, PortfoliosBase, ProjectMembershipsBase, ProjectStatusesBase, ProjectsBase, SectionsBase, StoriesBase, TagsBase, TasksBase, TeamMembershipsBase, TeamsBase, TypeaheadBase, UserTaskListsBase, UsersBase, WebhooksBase, WorkspaceMembershipsBase, WorkspacesBase
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.
12 13 14 15 16 17 18 |
# File 'lib/asana/resource_includes/resource.rb', line 12 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.
33 34 35 36 |
# File 'lib/asana/resource_includes/resource.rb', line 33 def method_missing(m, *args) super unless respond_to_missing?(m, *args) cache(m, wrapped(to_h[m.to_s])) end |
Instance Method Details
#refresh ⇒ Object
If it has findById, it implements #refresh
21 22 23 24 25 |
# File 'lib/asana/resource_includes/resource.rb', line 21 def refresh raise "#{self.class.name} does not respond to #find_by_id" unless \ self.class.respond_to?(:find_by_id) self.class.find_by_id(client, gid) 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.
42 43 44 |
# File 'lib/asana/resource_includes/resource.rb', line 42 def respond_to_missing?(m, *) to_h.key?(m.to_s) end |
#to_h ⇒ Object
Returns the raw Hash representation of the data.
48 49 50 |
# File 'lib/asana/resource_includes/resource.rb', line 48 def to_h @_data end |
#to_s ⇒ Object Also known as: inspect
52 53 54 55 |
# File 'lib/asana/resource_includes/resource.rb', line 52 def to_s attrs = to_h.map { |k, _| "#{k}: #{public_send(k).inspect}" }.join(', ') "#<Asana::#{self.class.name.split('::').last} #{attrs}>" end |