Class: Rtml::WidgetCore::WidgetProxy

Inherits:
Module
  • Object
show all
Defined in:
lib/rtml/widget_core/widget_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*a, &b) ⇒ WidgetProxy

Returns a new instance of WidgetProxy.



31
32
33
34
# File 'lib/rtml/widget_core/widget_proxy.rb', line 31

def initialize(*a, &b)
  @proxied_widgets = []
  super(*a, &b)
end

Instance Attribute Details

#proxied_widgetsObject (readonly)

Returns the value of attribute proxied_widgets.



2
3
4
# File 'lib/rtml/widget_core/widget_proxy.rb', line 2

def proxied_widgets
  @proxied_widgets
end

Instance Method Details

#imbue(proxy) ⇒ Object

An explanation: What we have set up looks a little like this:

Class includes Module1

Module1 includes Module2

Class is now expected to include methods from Module2 -- BUT it doesn't because its references haven't been
updated.

Class must explicitly reinclude Module1 in order to include methods from Module2. Unfortunately, by this time, we have already forgotten what class included Module1 to begin with. So, include_proxy will hunt down that class for us, and reinclude “self” (this WidgetProxy instance).

The name of the method “imbue” is a nod to where I learned that “ObjectSpace” exists (and a working code example, of course): stackoverflow.com/questions/1655623/ruby-class-c-includes-module-m-including-module-n-in-m-does-not-affect-c-what

If anybody’s curious, the reason I didn’t include his code directly is because I want to keep RTML dependencies to a minimum.



24
25
26
27
28
29
# File 'lib/rtml/widget_core/widget_proxy.rb', line 24

def imbue(proxy)
  include proxy
  ObjectSpace.each_object(Class) do |k|
    k.send :include, self if k.include? self
  end
end

#included(base) ⇒ Object



36
37
38
39
# File 'lib/rtml/widget_core/widget_proxy.rb', line 36

def included(base)
  base.class_widgets ||= []
  base.class_widgets.concat self.proxied_widgets
end