Class: Asana::Resources::Resource

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Methods included from ResponseHelper

parse

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.

Returns:

  • the value for the requested property.

Raises:

  • a NoMethodError if the property doesn’t exist.



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

#refreshObject

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.

Returns:

  • (Boolean)

    true if the resource has the property, false otherwise.



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_hObject

Returns the raw Hash representation of the data.

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_sObject 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