Class: VER::WidgetEvent

Inherits:
BasicObject
Defined in:
lib/ver/widget_event.rb

Overview

This class acts as a proxy for the widget. it forwards all methods, except for [event], which points to the event currently invoked.

This is done to allow methods operating on the widget to obtain information about the event, so they can act intelligently.

The usage of [method_missing] might make it a bit slower right after startup, but should be next to no difference to a direct reference of widget as we dynamically define the methods handled.

Due to the dynamic nature of ruby, what widget is underneath will not make any difference, and WidgetEvent will be more or less invisible outside of backtraces.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widget, event) ⇒ WidgetEvent

Returns a new instance of WidgetEvent.



19
20
21
# File 'lib/ver/widget_event.rb', line 19

def initialize(widget, event)
  @widget, @event = widget, event
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ver/widget_event.rb', line 27

def method_missing(method, *args, &block)
  if method =~ /=/
    ::VER::WidgetEvent.class_eval(<<-RUBY, __FILE__, __LINE__)
      def #{method}(arg)
        @widget.#{method} arg
      end
    RUBY
  else
    ::VER::WidgetEvent.class_eval(<<-RUBY, __FILE__, __LINE__)
      def #{method}(*args, &block)
        @widget.#{method}(*args, &block)
      end
    RUBY
  end

  @widget.send(method, *args, &block)
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



17
18
19
# File 'lib/ver/widget_event.rb', line 17

def event
  @event
end

#widgetObject (readonly)

Returns the value of attribute widget.



17
18
19
# File 'lib/ver/widget_event.rb', line 17

def widget
  @widget
end

Instance Method Details

#inspectObject



49
50
51
# File 'lib/ver/widget_event.rb', line 49

def inspect
  "#<VER::WidgetEvent widget=%p event=%p>" % [@widget, @event]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ver/widget_event.rb', line 23

def respond_to?(method)
  method.to_sym == :event || @widget.respond_to?(method)
end

#send(method, *args, &block) ⇒ Object



45
46
47
# File 'lib/ver/widget_event.rb', line 45

def send(method, *args, &block)
  @widget.send(method, *args, &block)
end