Class: Callback
Constant Summary collapse
- ACCEPTED =
[Symbol, Proc, String, Method]
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
-
#method ⇒ Object
Returns the value of attribute method.
- #proc ⇒ Object
Instance Method Summary collapse
- #block ⇒ Object
- #generate_block ⇒ Object
- #generate_proc ⇒ Object
-
#initialize(method, input, &block) ⇒ Callback
constructor
A new instance of Callback.
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
#input ⇒ Object
Returns the value of attribute input.
2 3 4 |
# File 'lib/callback.rb', line 2 def input @input end |
#method ⇒ Object
Returns the value of attribute method.
2 3 4 |
# File 'lib/callback.rb', line 2 def method @method end |
#proc ⇒ Object
16 17 18 |
# File 'lib/callback.rb', line 16 def proc @proc ||= self.generate_proc end |
Instance Method Details
#block ⇒ Object
20 21 22 |
# File 'lib/callback.rb', line 20 def block @block ||= self.generate_block end |
#generate_block ⇒ Object
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_proc ⇒ Object
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 |