Module: Watchmaker::Manufacturer::ClassMethods

Defined in:
lib/watchmaker/manufacturer.rb

Instance Method Summary collapse

Instance Method Details

#manufacture(profile, method) ⇒ Object

Create objects, by a specific means, either in memory or in the database.



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/watchmaker/manufacturer.rb', line 12

def manufacture(profile, method)

  # 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 << from_watchmaker(abstract, method)
          else
            objects << from_factory(abstract, method)
          end
        end
      end

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

      # For any supplied watchmakers, create them.
      #
      if watchmakers = dependencies[:watchmakers] 
        watchmakers.each do |watchmaker|
          objects << from_watchmaker(watchmaker, method)
        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