Class: Callback

Inherits:
Object show all
Defined in:
lib/callback.rb

Constant Summary collapse

ACCEPTED =
[Symbol, Proc, String, Method]
ACCEPTANCE_ERROR =
"Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method. %s is none of those."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, input, &block) ⇒ Callback



7
8
9
10
11
12
13
14
# File 'lib/callback.rb', line 7

def initialize(method, input, &block)
  self.input = input
  self.input = block if block_given?
  
  if not ACCEPTED.include? self.input.class
    raise ACCEPTANCE_ERROR % self.input.class
  end
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



2
3
4
# File 'lib/callback.rb', line 2

def input
  @input
end

#methodObject

Returns the value of attribute method.



2
3
4
# File 'lib/callback.rb', line 2

def method
  @method
end

#procObject



16
17
18
# File 'lib/callback.rb', line 16

def proc
  @proc ||= self.generate_proc
end

Instance Method Details

#blockObject



20
21
22
# File 'lib/callback.rb', line 20

def block
  @block ||= self.generate_block
end

#generate_blockObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/callback.rb', line 33

def generate_block
  case self.input
  when Symbol
    instance_method = self.input.to_sym
    Proc.new { self.send(instance_method) }
  when Proc, Method
    self.input
  else
    nil
  end
end

#generate_procObject



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

def generate_proc
  case self.input
  when String
    Proc.new { eval(self.input) }
  else
    nil
  end
end