#---
# 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 :subject
  def initialize
    @subject = Configuration.new
  end
  def self.start
    newObj = self.new
    yield newObj
    return newObj
  end
  def item(name, &block) 
    i = ItemBuilder.new(name, subject)
    i.instance_eval(&block) if block_given?
    subject.add_item i.subject
  end
end

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

  def acid args
    result = Acid.new
    result.grade = args[:grade]
    result.type = args[:type]
    return result
  end

  def electricity power
    return Electricity.new(power)
  end
end


class RulesBuilder20
  def load aStream
    builder = ConfigurationBuilder.new
    builder.instance_eval aStream.join("\n")
    return builder.subject
  end

end

if __FILE__ == $0
  c = RulesBuilder20.new.load(File.readlines('rules20.rb'))
  require 'pp'
#  puts c
end
