Module: Modular

Included in:
M
Defined in:
lib/redshift/util/modular.rb

Overview

A way to use a class-method-based DSL, like redshift, from within a module. Extend a module M with Modular. Then, use the DSL freely in M, even though M doesn’t have the DSL methods. When you include M in some class, it “replays” the class methods. See examples/modular-component-def.rb.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



6
7
8
# File 'lib/redshift/util/modular.rb', line 6

def method_missing(meth, *args, &block)
  (@_modular_saved ||= []) << [meth, args, block, caller]
end

Instance Method Details

#included(m) ⇒ Object

this would allow unquoted constants, such as state names, but it’s a bit too much magic:

def const_missing(c)
  c
end


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redshift/util/modular.rb', line 16

def included(m)
  @_modular_saved && @_modular_saved.each do |meth, args, block, where|
    begin
      m.send(meth, *args, &block)
    rescue => ex
      ex.set_backtrace where
      ex.message << " while applying methods from #{self}"
      raise ex
    end
  end
end