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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResponseHelper

parse

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.

Returns:

  • the value for the requested property.

Raises:

  • a NoMethodError if the property doesn’t exist.



37
38
39
40
# File 'lib/asana/resource_includes/resource.rb', line 37

def method_missing(m, *args)
  super unless respond_to_missing?(m, *args)
  cache(m, wrapped(to_h[m.to_s]))
end

Class Method Details

.inherited(base) ⇒ Object



12
13
14
# File 'lib/asana/resource_includes/resource.rb', line 12

def self.inherited(base)
  Registry.register(base)
end

Instance Method Details

#refreshObject

If it has findById, it implements #refresh



25
26
27
28
29
# File 'lib/asana/resource_includes/resource.rb', line 25

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.



46
47
48
# File 'lib/asana/resource_includes/resource.rb', line 46

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.



52
53
54
# File 'lib/asana/resource_includes/resource.rb', line 52

def to_h
  @_data
end

#to_sObject Also known as: inspect



56
57
58
59
# File 'lib/asana/resource_includes/resource.rb', line 56

def to_s
  attrs = to_h.map { |k, _| "#{k}: #{public_send(k).inspect}" }.join(', ')
  "#<Asana::#{self.class.name.split('::').last} #{attrs}>"
end