#---
# 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'
require 'pp'

class RulesBuilder2
  def load aStream
    result = eval(aStream.join("\n"))
    pp result
    return result.configuration
  end
end

class ConfigurationBuilder
  def initialize
    @configuration = Configuration.new
    @capturing = nil
  end
  attr_accessor :configuration

  def self.item arg
    builder = ConfigurationBuilder.new
    builder.item arg
  end

  def item srcSymbol
    @current_item = Item.new srcSymbol
    @configuration.add_item @current_item
    self
  end


  def depends_on arg
    item = @configuration[arg]
    @current_item.add_dependency(item)
    self
  end

  def uses
    @capturing = :uses
    self
  end    
  def electricity powerNumber
    @current_resource = Electricity.new(powerNumber)
    capture @current_resource
    self
  end
  def capture arg
    case @capturing
    when :uses
      @current_item.add_usage arg
    when :provisions
      @current_item.add_provision arg
    end
  end    

  def acid
    @current_resource = Acid.new
    capture @current_resource
    self
  end


  def type acidType
    @current_resource.type = acidType
    self
  end
  def grade aNumber
    @current_resource.grade = aNumber
    self
  end

  
  def provides 
    @capturing = :provisions
    self
  end
  
end

