class Factory

A Factory is a wrapper around a Proc that exposes it through its {Factory#new new} method.

Wrapping a Proc in a Factory is useful to have a uniform API across classes and custom object-creating lambdas. For instance, if a method create_object takes a class as argument, like:

def create_object(klass)
  obj = klass.new('foo')
  # do something with obj
  obj
end

you can pass modified class constructors:

create_object(Factory.new {|arg| Array.new(4) { arg } })

and have the method behave as if the passed argument were a normal class.

Attributes

component[R]

A Factory can specify a component, which is the class used to instantiate the objects created by this Factory.

When non-nil, it should satisfy component == new(*args).class.

@return the component of this Factory

Public Class Methods

new(component = nil, &blk) click to toggle source

Create a factory object.

@param component the factory component @param &blk the wrapped Proc

# File lib/rui/factory.rb, line 61
def initialize(component = nil, &blk)
  @blk = blk
  @component = component
end

Public Instance Methods

__bind__(object) click to toggle source

Rebind this Factory.

Binding a Factory to an object causes the wrapped Proc to be executed in the given object’s scope.

@param object the object to bind this Factory to

# File lib/rui/factory.rb, line 81
def __bind__(object)
  Factory.new(@component, &@blk.bind(object))
end
new(*args) click to toggle source

Call the wrapped Proc

# File lib/rui/factory.rb, line 69
def new(*args)
  @blk[*args]
end