Class: Hollerback::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/hollerback/callbacks.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Callbacks

Returns a new instance of Callbacks.



21
22
23
# File 'lib/hollerback/callbacks.rb', line 21

def initialize(block)
  self.tap { |proxy| block.call(proxy) if block }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



36
37
38
39
# File 'lib/hollerback/callbacks.rb', line 36

def method_missing(m, *args, &block)
  block ? callbacks[m] = block : super
  self
end

Instance Method Details

#respond_with(callback, *args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/hollerback/callbacks.rb', line 25

def respond_with(callback, *args)
  if callbacks.has_key?(callback)
    callbacks[callback].call(*args)
  else
    raise NoMethodError.new("No callback '#{callback.to_s}'' is defined.")
  end
end

#try_respond_with(callback, *args) ⇒ Object



32
33
34
# File 'lib/hollerback/callbacks.rb', line 32

def try_respond_with(callback, *args)
  callbacks[callback].call(*args) if callbacks.has_key?(callback)
end