Module: GLib::Eventable

Included in:
Instantiatable
Defined in:
lib/glib/eventable.rb

Overview

This allows you to create subclasses of ‘Gtk` objects in a more OOP style.

Instance Method Summary collapse

Instance Method Details

#event(event_hash = nil) ⇒ Hash Also known as: events

Returns a Hash of all registered events where the key is the signal and the value is the method name.

Parameters:

  • event_hash ({ Symbol => Symbol }) (defaults to: nil)

    a Hash where the key is the signal and the value is the method name.

Returns:

  • (Hash)

    a Hash of all registered events where the key is the signal and the value is the method name.

Raises:

  • (TypeError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/glib/eventable.rb', line 15

def event(event_hash=nil)
  @events ||= ancestors[1].respond_to?(:events) ? ancestors[1].events.dup : {}
  return @events if event_hash.nil?
  
  raise TypeError, 'event_hash must respond to :to_hash or :to_h' unless event_hash.respond_to?(:to_hash) || event_hash.respond_to?(:to_h)
  event_hash = event_hash.to_hash rescue event_hash.to_h
  
  @events = @events.merge(event_hash).each_with_object({}) do |(event_name, method_name_or_proc), memo|
    raise TypeError, 'event_hash keys must respond to :to_s' unless event_hash.respond_to?(:to_s)
    raise TypeError, 'event_hash values must respond to :call or :to_s' unless method_name_or_proc.respond_to?(:call) || method_name_or_proc.respond_to?(:to_s)
    
    event_name = event_name.to_s.downcase.strip
    method_name_or_proc = method_name_or_proc.to_s.downcase.strip.to_sym unless method_name_or_proc.respond_to?(:call)
    
    memo[event_name] = method_name_or_proc
  end
end

#new(*args) ⇒ Object

Connect signals to events before initialization.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/glib/eventable.rb', line 35

def new(*args)
  super.instance_eval do
    self.class.events.each do |event_name, method_name_or_proc|
      signal_connect(event_name) do |*arguments|
        proc = method_name_or_proc.respond_to?(:call) ? method_name_or_proc : method(method_name_or_proc).to_proc
        arguments = arguments[0...proc.arity] if proc.arity >= 0
        
        instance_exec(*arguments, &proc)
      end
    end
    
    self
  end
end