#---
# Excerpted from "The ThoughtWorks Anthology",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/twa for more book information.
#---
require 'model'

class ConfigurationBuilder
  def initialize
    @@source = Configuration.new
  end
  def self.start
    newObj = self.new
    yield newObj
    return newObj
  end
  def item(name) 
    i = ItemBuilder.new(name, source)
    source.add_item i.source
    return i
  end
  def source
    @@source
  end
  def self.source
    @@source
  end
end

class ItemBuilder
  attr_reader :source, :config
  def initialize name, config
    @source = Item.new(name)
    @config = config
  end
  def uses arg
    yield arg if block_given?
    @source.add_usage arg
  end
  def provides arg
    yield arg if block_given?
    @source.add_provision arg
  end
  def depends_on arg
    @source.add_dependency @config[arg]
  end
end


class Resources
  class << self
    def electricity power
      return Electricity.new(power)
    end

    def acid
      result = Acid.new
      return result
    end
  end
end

class RulesBuilder16
  def load aStream
    result = eval aStream.join("\n")
    return ConfigurationBuilder.source
  end
end

if __FILE__ == $0
  c = RulesBuilder16.new.load(File.readlines('rules16.rb'))
  require 'pp'
  pp c
end
