Class: CallbacksAttachable::Callback

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

Instance Method Summary collapse

Constructor Details

#initialize(registry, events, opts, instance_scope, callback) ⇒ Callback

Returns a new instance of Callback.



3
4
5
6
7
8
9
10
# File 'lib/all/callback.rb', line 3

def initialize(registry, events, opts, instance_scope, callback)
  @registry = registry
  @events = events
  @once = opts.fetch(:once?, false)
  @instance_scope = instance_scope
  @callback = callback
  @cancelled = false
end

Instance Method Details

#call(instance, args) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/all/callback.rb', line 12

def call(instance, args)
  if @instance_scope
    instance.instance_exec(*args, &@callback)
  else
    @callback.call(*args)
  end
  cancel if @once
end

#cancelObject



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

def cancel
  if @cancelled
    raise Error, 'already cancelled'
  else
    @events.each{ |event| @registry.deregister event, self }
    @on_cancel.call if @on_cancel
    @cancelled = true
  end
end

#cancelled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/all/callback.rb', line 35

def cancelled?
  @cancelled
end

#on_cancel(&on_cancel) ⇒ Object



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

def on_cancel(&on_cancel)
  @on_cancel = on_cancel
end