Skip to content
Browse files

Factor out common build code into a manufacturer, builders and creato…

…rs call the manufacturing code. Provide a rails-y compatible syntax with build and create.
  • Loading branch information...
1 parent 528704e commit c98a6f1648f1a579b2c98ce8ffd7958c793c03b0 @cmeiklejohn committed Sep 2, 2011
Showing with 104 additions and 58 deletions.
  1. +8 −1 lib/watchmaker.rb
  2. +19 −0 lib/watchmaker/assembler.rb
  3. +5 −57 lib/watchmaker/constructor.rb
  4. +72 −0 lib/watchmaker/manufacturer.rb
View
9 lib/watchmaker.rb
@@ -2,12 +2,19 @@
require "watchmaker/version"
require "watchmaker/configuration"
+
require "watchmaker/integrations"
+
require "watchmaker/learner"
+
+require "watchmaker/manufacturer"
require "watchmaker/constructor"
+require "watchmaker/assembler"
module Watchmaker # :nodoc:
+ include Integrations
include Learner
+ include Manufacturer
include Constructor
- include Integrations
+ include Assembler
end
View
19 lib/watchmaker/assembler.rb
@@ -0,0 +1,19 @@
+# encoding: UTF-8
+
+module Watchmaker
+ module Assembler
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Assemble a profile, or build in memory only.
+ #
+ def build(profile)
+ manufacture(profile, :build)
+ end
+
+ alias :assemble :build
+
+ end
+ end
+end
View
62 lib/watchmaker/constructor.rb
@@ -6,66 +6,14 @@ module Constructor
module ClassMethods
- # Contruct a profile.
+ # Contruct a profile, or persist objects.
#
- 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 << from_watchmaker(abstract)
- else
- objects << from_factory(abstract)
- end
- end
- end
-
- # For any supplied factories, create them.
- #
- if factories = dependencies[:factories]
- factories.each do |factory|
- objects << from_factory(factory)
- end
- end
-
- # For any supplied watchmakers, create them.
- #
- if watchmakers = dependencies[:watchmakers]
- watchmakers.each do |watchmaker|
- objects << 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
-
+ def create(profile)
+ manufacture(profile, :create)
end
+ alias :construct :create
+
end
end
end
View
72 lib/watchmaker/manufacturer.rb
@@ -0,0 +1,72 @@
+# encoding: UTF-8
+
+module Watchmaker
+ module Manufacturer
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Create objects, by a specific means, either in memory or in the
+ # database.
+ #
+ 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
+
+ end
+ end
+end

0 comments on commit c98a6f1

Please sign in to comment.
Something went wrong with that request. Please try again.