Class: Heroics::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/heroics/resource.rb

Overview

A resource with methods mapped to API links.

Instance Method Summary collapse

Constructor Details

#initialize(links) ⇒ Resource

Instantiate a resource.



8
9
10
# File 'lib/heroics/resource.rb', line 8

def initialize(links)
  @links = links
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *parameters) ⇒ String, ... (private)

Find a link and invoke it.

Raises:

  • (NoMethodError)

    Raised if the name doesn’t match a known link.



22
23
24
25
26
27
28
29
# File 'lib/heroics/resource.rb', line 22

def method_missing(name, *parameters)
  link = @links[name.to_s]
  if link.nil?
    address = "<#{self.class.name}:0x00#{(self.object_id << 1).to_s(16)}>"
    raise NoMethodError.new("undefined method `#{name}' for ##{address}")
  end
  link.run(*parameters)
end