Class: Mixit

Inherits:
Object
  • Object
show all
Defined in:
lib/mixit.rb,
lib/mixit/version.rb

Constant Summary collapse

VERSION =
'0.5.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Mixit

Returns a new instance of Mixit.

Parameters:

  • opts (Hash)

    A Hash of options.

Options Hash (opts):

  • :scope (Object)

    The scope to extend.

  • :modules (Module)

    An Array of modules to extend a scope with.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mixit.rb', line 52

def initialize opts
  @clones  = []
  @modules = opts[:modules]

  case opts[:scope]
  when Binding
    @scope = opts[:scope].eval "self"
  when Proc
    @scope = opts[:scope].binding.eval "self"
  else
    @scope = opts[:scope]
  end
end

Instance Attribute Details

#modulesArray<Module> (readonly)

Returns An Array of modules to extend a scope with.

Returns:

  • (Array<Module>)

    An Array of modules to extend a scope with.



34
35
36
# File 'lib/mixit.rb', line 34

def modules
  @modules
end

#scopeObject (readonly)

Returns The scope to extend.

Returns:

  • (Object)

    The scope to extend.



40
41
42
# File 'lib/mixit.rb', line 40

def scope
  @scope
end

Class Method Details

.temporarily(opts, &block) {|object| ... } ⇒ Object

Temporarily extends a scope with instance methods from module(s).

Parameters:

  • opts (Hash)

    A Hash of options.

  • block (Proc)

    A block executed while the object is extended.

Options Hash (opts):

  • :scope (Object)

    The scope to extend.

  • :modules (Module)

    An Array of modules to extend a scope with.

Yield Parameters:

  • object (Object)

    Yields a clone of the extended object (Optional).

Returns:

  • (Object)

    Returns the return value of 'block'.



24
25
26
27
# File 'lib/mixit.rb', line 24

def temporarily opts, &block
  mixit = new(opts)
  mixit.temporarily(&block)
end

Instance Method Details

#temporarily(&block) {|object| ... } ⇒ Object

Temporarily extends scope with methods from module(s).

Parameters:

  • block (Proc)

    A block executed while the object is extended.

Yield Parameters:

  • object (Object)

    Yields a clone of the extended object (Optional).

Returns:

  • (Object)

    Returns the return value of 'block'.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mixit.rb', line 78

def temporarily &block
  if block.arity == 1
    value = block.call extend_scope!(:clone => true) 
  else
    begin
      value = extend_scope!(:clone => false).instance_eval(&block)
    ensure
      remove_methods!
      trigger_callback!
    end
  end

  value
end