#
# must.rb
#
#   Copyright (c) 1999 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
#
# usage:
#
#   str.must String
#   str = (arg.must String)
#   arg.must String, Array
#   arg.must_have :each
#   arg.must_be 3
#

require 'amstd/to_s'


class Object

  def must( *args )
    args.each {|c| return self if c === self }
    raise TypeError, "wrong arg type '#{type}' for required #{args.join('/')}"
  end

  def must_have( *args )
    args.each do |m|
      unless self.respond_to? m then
        name = _name2str( m )
        tos = self.to_s; tos = tos[0, 15] + '...' if tos.size > 15
        raise ArgumentError, "arg #{tos}(#{type}) does not have '#{name}'"
      end
    end
    self
  end
  alias needed must_have

  def must_be( eq )
    unless self == eq then
      stos = self.to_s; stos = stos[0,15] + '...' if stos.size > 15
      etos = eq.to_s;   etos = etos[0,15] + '...' if etos.size > 15
      raise ArgumentError, "arg #{stos}(#{type}) is not #{etos}"
    end
    self
  end

end
