Class: MPV::Callback

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

Overview

Encapsulates an object-method pair that will be invoked whenever an Client receives an event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, method) ⇒ Callback

Returns a new instance of Callback.

Parameters:

  • object (Object)

    the object that the callback will be issued to

  • method (Symbol)

    the method that the callback will invoke



13
14
15
16
# File 'lib/mpv/callback.rb', line 13

def initialize(object, method)
  @object = object
  @method = method
end

Instance Attribute Details

#methodSymbol

Returns the method that the callback will invoke.

Returns:

  • (Symbol)

    the method that the callback will invoke



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

def method
  @method
end

#objectObject

Returns the object that the callback will be issued to.

Returns:

  • (Object)

    the object that the callback will be issued to



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

def object
  @object
end

Instance Method Details

#dispatch!(event) ⇒ void

This method returns an undefined value.

Dispatches the callback. Does nothing unless #valid? is true.

Parameters:

  • event (string)

    the event name



29
30
31
32
33
# File 'lib/mpv/callback.rb', line 29

def dispatch!(event)
  return unless valid?

  object.send method, event
end

#valid?Boolean

Determines the validity of the instantiated callback. A callback is said to be valid if the object responds to the given method and the method has an arity of 1 (for the event data).

Returns:

  • (Boolean)

    whether or not the callback is valid



22
23
24
# File 'lib/mpv/callback.rb', line 22

def valid?
  object.respond_to?(method) && object.method(method).arity == 1
end