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.



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.

Returns:

  • the value for the requested property.

Raises:

  • a NoMethodError if the property doesn’t exist.



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

#refreshObject

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.

Returns:

  • (Boolean)

    true if the resource has the property, false otherwise.



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_hObject

Returns the raw Hash representation of the data.

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