Browse files

Break things out into the configuration, the learner and the construc…

…tor.
  • Loading branch information...
1 parent f367b0f commit f9702b6ab519f1c5d40a5d80095dfa41c0da4848 @cmeiklejohn committed Aug 20, 2011
Showing with 110 additions and 70 deletions.
  1. +5 −70 lib/watchmaker.rb
  2. +22 −0 lib/watchmaker/configuration.rb
  3. +59 −0 lib/watchmaker/constructor.rb
  4. +24 −0 lib/watchmaker/learner.rb
View
75 lib/watchmaker.rb
@@ -1,76 +1,11 @@
# encoding: UTF-8
require "watchmaker/version"
+require "watchmaker/configuration"
+require "watchmaker/learner"
+require "watchmaker/constructor"
module Watchmaker
-
- # Attribute accessors to hold profile mappings.
- #
- mattr_accessor :profiles
-
- # Learn a new profile.
- #
- def self.learn(profile, options = {}, &block)
-
- # Initialize the profiles unless they exist.
- #
- @@profiles = {} unless @@profiles
-
- # Add the block to the list of known profiles.
- #
- @@profiles[profile] = {
- :options => options,
- :block => block
- }
-
- end
-
- # Contruct a profile.
- #
- def self.construct(profile)
-
- # Store created objects.
- #
- objects = []
-
- # If a profile exists, call the proc we've stored; if not, raise.
- #
- if selected_profile = @@profiles[profile]
-
- if options = selected_profile[:options]
-
- # For any supplied factories, create them.
- #
- if factories = options[:factories]
- factories.each do |factory|
- objects << Factory.create(factory.to_sym)
- end
- end
-
- # For any supplied watchmakers, create them.
- #
- if watchmakers = options[:watchmakers]
- watchmakers.each do |watchmaker|
- objects << Watchmaker.construct(watchmaker.to_sym)
- end
- end
-
- end
-
- # Run the supplied block.
- #
- if block = selected_profile.delete(:block)
- objects << block.call(objects)
- end
-
- # Return objects.
- #
- objects
-
- else
- raise "#{profile} is not a valid profile"
- end
-
- end
-
+ include Learner
+ include Constructor
end
View
22 lib/watchmaker/configuration.rb
@@ -0,0 +1,22 @@
+# encoding: UTF-8
+
+require 'singleton'
+
+module Watchmaker
+
+ # Singleton configuration class to hold all configured information.
+ #
+ class Configuration
+ include Singleton
+
+ attr_accessor :profiles
+ end
+
+ # As soon as we define the singleton configuration class, instantitate
+ # it and default some values.
+ #
+ Configuration.instance.tap do |o|
+ o.profiles = {}
+ end
+
+end
View
59 lib/watchmaker/constructor.rb
@@ -0,0 +1,59 @@
+# encoding: UTF-8
+
+module Watchmaker
+ module Constructor
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Contruct a profile.
+ #
+ def construct(profile)
+
+ # Store created objects.
+ #
+ objects = []
+
+ # If a profile exists, call the proc we've stored; if not, raise.
+ #
+ if selected_profile = Configuration.instance.profiles[profile]
+
+ if options = selected_profile[:options]
+
+ # For any supplied factories, create them.
+ #
+ if factories = options[:factories]
+ factories.each do |factory|
+ objects << Factory.create(factory.to_sym)
+ end
+ end
+
+ # For any supplied watchmakers, create them.
+ #
+ if watchmakers = options[:watchmakers]
+ watchmakers.each do |watchmaker|
+ objects << construct(watchmaker.to_sym)
+ end
+ end
+
+ end
+
+ # Run the supplied block.
+ #
+ if block = selected_profile.delete(:block)
+ objects << block.call(objects)
+ end
+
+ # Return objects.
+ #
+ objects
+
+ else
+ raise "#{profile} is not a valid profile"
+ end
+
+ end
+
+ end
+ end
+end
View
24 lib/watchmaker/learner.rb
@@ -0,0 +1,24 @@
+# encoding: UTF-8
+
+module Watchmaker
+ module Learner
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Learn a new profile.
+ #
+ def learn(profile, options = {}, &block)
+
+ # Add the block to the list of known profiles.
+ #
+ Configuration.instance.profiles[profile] = {
+ :options => options,
+ :block => block
+ }
+
+ end
+
+ end
+ end
+end

0 comments on commit f9702b6

Please sign in to comment.