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, AuditLogAPIBase, BatchAPIBase, CustomFieldSettingsBase, CustomFieldsBase, Event, EventsBase, GoalRelationshipsBase, GoalsBase, JobsBase, MembershipsBase, OrganizationExportsBase, PortfolioMembershipsBase, PortfoliosBase, ProjectBriefsBase, ProjectMembershipsBase, ProjectStatusesBase, ProjectTemplatesBase, ProjectsBase, SectionsBase, StatusUpdatesBase, StoriesBase, TagsBase, TasksBase, TeamMembershipsBase, TeamsBase, TimePeriodsBase, TypeaheadBase, UserTaskListsBase, UsersBase, WebhooksBase, WorkspaceMembershipsBase, WorkspacesBase
Instance Method Summary collapse
-
#initialize(data, client: required('client')) ⇒ Resource
constructor
A new instance of Resource.
-
#method_missing(method_name, *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?(method_name) ⇒ 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.
14 15 16 17 18 19 20 |
# File 'lib/asana/resource_includes/resource.rb', line 14 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(method_name, *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.
36 37 38 39 |
# File 'lib/asana/resource_includes/resource.rb', line 36 def method_missing(method_name, *args) super unless respond_to_missing?(method_name, *args) cache(method_name, wrapped(to_h[method_name.to_s])) end |
Instance Method Details
#refresh ⇒ Object
If it has findById, it implements #refresh
23 24 25 26 27 28 |
# File 'lib/asana/resource_includes/resource.rb', line 23 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?(method_name) ⇒ 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.
45 46 47 |
# File 'lib/asana/resource_includes/resource.rb', line 45 def respond_to_missing?(method_name, *) to_h.key?(method_name.to_s) end |
#to_h ⇒ Object
Returns the raw Hash representation of the data.
51 52 53 |
# File 'lib/asana/resource_includes/resource.rb', line 51 def to_h @_data end |
#to_s ⇒ Object Also known as: inspect
55 56 57 58 |
# File 'lib/asana/resource_includes/resource.rb', line 55 def to_s attrs = to_h.map { |k, _| "#{k}: #{public_send(k).inspect}" }.join(', ') "#<Asana::#{self.class.name.split('::').last} #{attrs}>" end |