Class: Conject::ObjectFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/conject/object_factory.rb

Instance Method Summary collapse

Instance Method Details

#construct_new(name, object_context) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/conject/object_factory.rb', line 6

def construct_new(name, object_context)
  object = nil
  config = object_context.get_object_config(name)
  lambda_constructor = config[:construct]
  alias_for = config[:is]
  klass = config[:class]
  raise "#{klass.inspect} is not a Class" if klass and !(Class === klass)
  if alias_for
    # This object is defined as simply being another name for a different object
    begin
      object = object_context.get(alias_for)
    rescue Exception => ex
      raise "(When attempting to fill alias '#{name}' with actual object '#{alias_for}') #{ex.message}"
    end

  elsif lambda_constructor
    case lambda_constructor.arity
    when 0
      object = lambda_constructor[]
    when 1
      object = lambda_constructor[object_context]
    when 2
      object = lambda_constructor[name, object_context]
    else
      raise "Constructor lambda takes 0, 1 or 2 params; this lambda takes #{lambda_constructor.arity}"
    end

  elsif klass
    # This object has a specified class
    object = type_1_constructor(klass, name, object_context, config[:specialize]) # Specify class 
  else
    object = type_1_constructor(nil, name, object_context, config[:specialize]) # Let class be determined by name
  end

  # Stuff an internal back reference to the object context into the new object:
  object.__send__(:instance_variable_set, :@_conject_object_context, object_context)

  return object
end