Skip to content

Commit

Permalink
Clean up the configuration interface so the underlying data structure…
Browse files Browse the repository at this point in the history
… is no longer exposed.
  • Loading branch information
cmeiklejohn committed Aug 20, 2011
1 parent 8befd09 commit 8c2a99a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
36 changes: 28 additions & 8 deletions lib/watchmaker/configuration.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
10 changes: 5 additions & 5 deletions lib/watchmaker/constructor.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down
13 changes: 3 additions & 10 deletions lib/watchmaker/learner.rb
Expand Up @@ -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
Expand Down

0 comments on commit 8c2a99a

Please sign in to comment.