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

class RulesBuilder11
  def load aStream
    Item.set_load_methods
    result = eval(aStream.join("\n"))
    puts "loaded", Configuration.current.items.size
    Item.reset_methods
    return Configuration.current
  end
end

class Configuration
  def self.load 
    @@current = Configuration.new
  end
  load

  def self.item arg
    new_item = Item.new(arg)
    @@current.add_item new_item
    return new_item
  end

  def self.current
    @@current
  end
    
end

class Item 

# this is a hack to avoid clashing with the uses method that's defined
# in the domain model and used in the tests
  def uses_loading arg
    add_usage arg
    return self
  end
  def self.reset_methods
    alias_method :uses,  :old_uses 
  end
  def self.set_load_methods
    alias_method :old_uses, :uses
    alias_method :uses, :uses_loading
  end



  def provides arg
    add_provision arg
    return self
  end

  def depends_on arg
    add_dependency(Configuration.current[arg])
    self
  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 Acid
  def set_type arg
    @type = arg
    self
  end
  def set_grade arg
    @grade = arg
    self
  end
end
