#---
# 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.
#---
# using explicit receivers
# I'm using an expression builder here, but I feel that
# this style is more natural without one. 
# I just don't want t
require 'model'

class ConfigurationBuilder
  attr_reader :source
  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)
    yield i if block_given?
    source.add_item i.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 RulesBuilder3
  def load aStream
    result = eval aStream.join("\n")
    return result.source
  end
end

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