Class: Hollaback::Callback

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

Overview

A callback that can be executed later

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, execute = nil, &block) ⇒ Callback

Returns a new instance of Callback.



8
9
10
11
# File 'lib/hollaback/callback.rb', line 8

def initialize(type, execute = nil, &block)
  @type       = type
  @executable = (execute || block)
end

Instance Attribute Details

#executableObject (readonly)

Returns the value of attribute executable.



6
7
8
# File 'lib/hollaback/callback.rb', line 6

def executable
  @executable
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/hollaback/callback.rb', line 6

def type
  @type
end

Instance Method Details

#buildObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/hollaback/callback.rb', line 13

def build
  case executable
  when Symbol
    ->(target, &block) { target.send(executable, &block) }
  when Proc
    ->(target) { target.instance_eval(&executable) }
  else
    raise ArgumentError, "Invalid callback argument: #{executable.inspect}"
  end
end