Class: EventMachine::Ssh::Callbacks::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/em-ssh/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, event, &blk) ⇒ Callback

Returns a new instance of Callback.

Raises:

  • (ArgumentError)


76
77
78
79
80
81
# File 'lib/em-ssh/callbacks.rb', line 76

def initialize(obj, event, &blk)
  raise ArgumentError.new("a block is required") unless block_given?
  @obj   = obj
  @event = event
  @block = blk
end

Instance Attribute Details

#blockObject (readonly)

The block to call when the event is fired



74
75
76
# File 'lib/em-ssh/callbacks.rb', line 74

def block
  @block
end

#eventObject (readonly)

Sybmol

the name of the event



72
73
74
# File 'lib/em-ssh/callbacks.rb', line 72

def event
  @event
end

#objObject (readonly)

The object that keeps this callback



70
71
72
# File 'lib/em-ssh/callbacks.rb', line 70

def obj
  @obj
end

Instance Method Details

#call(*args) ⇒ Object

Call the callback with optional arguments



84
85
86
# File 'lib/em-ssh/callbacks.rb', line 84

def call(*args)
  block.call(*args)
end

#cancelObject

register



94
95
96
97
98
99
# File 'lib/em-ssh/callbacks.rb', line 94

def cancel
  raise "#{@obj} does not have any callbacks for #{@event.inspect}" unless @obj.respond_to?(:callbacks) && @obj.callbacks.respond_to?(:[]) && @obj.callbacks[@event].respond_to?(:delete)
  @obj.callbacks[@event].delete(self)
  @obj = nil
  self
end

#registerObject

Registers the callback with the object. This is useful if you cancel the callback at one point and want to re-enable it later on.



90
91
92
# File 'lib/em-ssh/callbacks.rb', line 90

def register
  @obj.on(self)
end