#---
# 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 RulesBuilder4
  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[0].each_pair do |key, value|
      case key
      when :depends_on
        oneOrMany(value) {|i| anItem.add_dependency(@config[i])}
      when :uses
        oneOrMany(value) {|r| anItem.add_usage r}
      when :provides
        oneOrMany(value) {|i| anItem.add_provision i}
      end
    end
  end
  def oneOrMany(obj, &block)
    if obj.kind_of? Array
      obj.each(&block)
    else
      yield obj
    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


if __FILE__ == $0
  RulesBuilder4.new.load(File.readlines('rules4.rb'))
end

