Class: Callback

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

Constant Summary collapse

ACCEPTED =
[Symbol, Proc, String, Method]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Callback.



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

def initialize(method, input, &block)
  #self.method = method
  self.input = input
  self.input = block if block_given?
  
  if not ACCEPTED.include? self.input.class
    raise "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. #{self.input} is none of those."
  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



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

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

#generate_procObject



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

def generate_proc
  case self.input
  when String
    Proc.new { eval(self.input) }
  #else if self.input.respond_to?(self.method)
  #    Proc.new { callback.send(method, self) }
  #  end
  end
end