Module: Erector::Inline

Included in:
InlineWidget, FieldTable
Defined in:
lib/erector/inline.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

This is part of the sub-widget/parent feature (see #widget method).



23
24
25
26
27
28
29
30
# File 'lib/erector/inline.rb', line 23

def method_missing(name, *args, &block)
  if parent && parent.respond_to?(name)
    block ||= lambda {} # captures self HERE
    parent.send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#call_blockObject

Executes the widget’s block (the one that was passed in the constructor). Since “self” is pointing to the new widget, the block does not naturally have access to parent method methods, so an Erector::Inline widget uses some method_missing black magic to propagate messages to the parent object. Since it executes inside the called widget’s context, when the block refers to instance variables, it’s talking about those of this widget, not the caller. It does, of course, have access to bound local variables of the caller, so you can use those to smuggle in instance variables.



16
17
18
19
# File 'lib/erector/inline.rb', line 16

def call_block
  # note that instance_eval seems to pass in self as a parameter to the block
  instance_eval(&block) if block
end