#---
# 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 'breakpoint'

class RulesBuilder21
  def load aStream
    @config = Configuration.new
    instance_eval aStream.join("\n")
    return @config
  end


  def item name, *args
    newItem = Item.new name
    process_item_args(newItem, args) unless args.empty?
    @config.add_item newItem
    return self
  end
  def process_item_args anItem, args
    args.each do |e|
      case e.head
      when :depends_on
        e.tail.each {|i| anItem.add_dependency(@config[i])}
      when :uses
        e.tail.each {|r| anItem.add_usage r}
      when :provides
        e.tail.each {|i| anItem.add_provision i}
      end
    end
  end



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

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


class Array
  def tail
    self[1..-1]
  end
  alias head first
end




if __FILE__ == $0
  RulesBuilder21.new.load(File.readlines('rules21.rb'))
end

