Class: Mixit

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

Constant Summary collapse

VERSION =
'0.5.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) {|_self| ... } ⇒ Mixit

Returns a new instance of Mixit.

Parameters:

  • opts (Hash)

    A key -> value pair of options.

Options Hash (opts):

  • :scope (Object)

    The scope to extend.

  • :modules (Module)

    A list of modules to extend the scope with.

Yield Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mixit.rb', line 29

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

  if block_given?
    yield(self)
  end
end

Instance Attribute Details

#modulesArray<Module> (readonly)

Returns A list of modules to extend a scope with.

Returns:

  • (Array<Module>)

    A list of modules to extend a scope with.



9
10
11
# File 'lib/mixit.rb', line 9

def modules
  @modules
end

#scopeObject (readonly)

Returns The scope to extend.

Returns:

  • (Object)

    The scope to extend.



15
16
17
# File 'lib/mixit.rb', line 15

def scope
  @scope
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'.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mixit.rb', line 59

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