Permalink
Browse files

Clean up the configuration interface so the underlying data structure…

… is no longer exposed.
  • Loading branch information...
1 parent 8befd09 commit 8c2a99a3cac6ab479dbaca73574a19cd3e159b12 @cmeiklejohn committed Aug 20, 2011
Showing with 36 additions and 23 deletions.
  1. +28 −8 lib/watchmaker/configuration.rb
  2. +5 −5 lib/watchmaker/constructor.rb
  3. +3 −10 lib/watchmaker/learner.rb
@@ -1,22 +1,42 @@
# encoding: UTF-8
-require 'singleton'
-
module Watchmaker
# Singleton configuration class to hold all configured information.
#
class Configuration
+
include Singleton
+ # Store list of all profiles.
+ #
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 = {}
+ # Initialize to an empty hash.
+ #
+ def initialize
+ @profiles = {}
+ end
+
+ def self.learn(name, dependencies, &block)
+ instance.learn(name, dependencies, &block)
+ end
+
+ def self.learned(name)
+ instance.learned(name)
+ end
+
+ def learn(name, dependencies, &block)
+ @profiles[name] = {
+ :dependencies => dependencies,
+ :block => block
+ }
+ end
+
+ def learned(name)
+ @profiles[name]
+ end
+
end
end
@@ -16,21 +16,21 @@ def construct(profile)
# If a profile exists, call the proc we've stored; if not, raise.
#
- if selected_profile = Configuration.instance.profiles[profile]
+ if selected_profile = Configuration.learned(profile)
- if options = selected_profile[:options]
+ if dependencies = selected_profile[:dependencies]
# For any supplied factories, create them.
#
- if factories = options[:factories]
+ if factories = dependencies[:factories]
factories.each do |factory|
objects << Factory.create(factory.to_sym)
end
end
# For any supplied watchmakers, create them.
#
- if watchmakers = options[:watchmakers]
+ if watchmakers = dependencies[:watchmakers]
watchmakers.each do |watchmaker|
objects << construct(watchmaker.to_sym)
end
@@ -40,7 +40,7 @@ def construct(profile)
# Run the supplied block.
#
- if block = selected_profile.delete(:block)
+ if block = selected_profile[:block]
objects << block.call(objects)
end
View
@@ -6,17 +6,10 @@ module Learner
module ClassMethods
- # Learn a new profile.
+ # Learn a profile by taking explicit dependencies.
#
- def learn(profile, options = {}, &block)
-
- # Add the block to the list of known profiles.
- #
- Configuration.instance.profiles[profile] = {
- :options => options,
- :block => block
- }
-
+ def learn(name, dependencies = {}, &block)
+ Configuration.learn(name, dependencies, &block)
end
end

0 comments on commit 8c2a99a

Please sign in to comment.