Module: Watchmaker::Constructor::ClassMethods

Defined in:
lib/watchmaker/constructor.rb

Instance Method Summary collapse

Instance Method Details

#construct(profile) ⇒ Object

Contruct a profile.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/watchmaker/constructor.rb', line 23

def construct(profile)

  # Store created objects.
  #
  objects = []

  # If a profile exists, call the proc we've stored; if not, raise.
  #
  if selected_profile = Configuration.learned(profile)

    if dependencies = selected_profile[:dependencies]

      # For any abstract dependencies, infer how to create them.
      #
      if abstracts = dependencies[:abstract] 
        abstracts.each do |abstract|
          if Configuration.learned?(abstract)
            objects << construct_from_watchmaker(abstract)
          else
            objects << construct_from_factory(abstract)
          end
        end
      end

      # For any supplied factories, create them.
      #
      if factories = dependencies[:factories] 
        factories.each do |factory|
          objects << construct_from_factory(factory)
        end
      end

      # For any supplied watchmakers, create them.
      #
      if watchmakers = dependencies[:watchmakers] 
        watchmakers.each do |watchmaker|
          objects << construct_from_watchmaker(watchmaker)
        end
      end

    end

    # Run the supplied block.
    #
    if block = selected_profile[:block]
      objects << block.call(objects)
    end

    # Return objects.
    #
    objects

  else
    raise "#{profile} is not a valid profile"
  end

end

#construct_from_factory(factory) ⇒ Object

Construct from a factory.



11
12
13
# File 'lib/watchmaker/constructor.rb', line 11

def construct_from_factory(factory)
  Factory.create(factory.to_sym)
end

#construct_from_watchmaker(watchmaker) ⇒ Object

Construct from another watchmaker.



17
18
19
# File 'lib/watchmaker/constructor.rb', line 17

def construct_from_watchmaker(watchmaker)
  construct(watchmaker.to_sym)
end