Project Mixit
Homepage https://github.com/robgleeson/Mixit
Documentation http://rubydoc.info/gems/mixit/frames
Author Rob Gleeson, Renato Riccieri Santos Zannon

DESCRIPTION

Mixit can be used to temporarily extend the calling scope of a block or object with instance methods from a module.
It is written to help Domain Specific Language(DSL) writers provide a DSL with their own methods without losing the calling scope of the caller.

WHY?

Some Ruby DSL approaches already exist(maybe one suits you):

  • instance_eval
    The calling scope is lost, and internal state is exposed to the user of the DSL.

  • instance_eval via "EmptyContext"
    The calling scope is lost, but the object is void of any state.

  • Pass self onto the calling block.
    The cleanest approach but some of the beauty of the DSL can be lost.

All these approaches(but the last) lose the calling scope, and that's why I created 'Mixit'.
I wanted the user of the DSL to have access to the calling scope, but have access to my DSL as implicit receivers of 'self' as well.

This approach is by no means 'perfect', or even 'good'.
If you use it wisely, though, it can be a cool way to build nice DSLs.

EXAMPLES

1.

Temporarily mix 'SomeModule' onto 'self' (non-thread safe).

Mixit.temporarily :modules => [SomeModule], :scope => self do 
  p self.class  # => Object
  p respond_to?(:some_mixed_method) # => true  
end

p self.class # => Object
p respond_to?(:some_mixed_method) # => false

2.

Temporarily mix 'SomeModule' onto 'self' (thread-safe - per thread)

A clone of the extended scope is passed as a argument to the receiving block.
Any state created by your mixed-in module(s) is lost to the original scope.
Any existing state mutated by your mixed-in module(s) will propagate back to the original scope.

You should be careful, as always.

Mixit.temporarily :modules => [SomeModule], :scope => self do |extended| 
  p extended.class # => Object
  p extended.respond_to?(:some_mixed_method) # => true
end

INSTALL

gem install mixit

LICENSE

See LICENSE.txt