Class: Callback
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
-
#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
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
#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
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_proc ⇒ Object
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 |