# spec_qualifiers.rb
# $Id$
# 
# :brief
#   An implementation of `each_of' qualifier. 
#     (see http://d.hatena.ne.jp/moro/20061029/1162116778) 
# :description
#   Provides `each_of', `all_of' qualifier to RSpec. This version of spec_qualifiers.rb is for RSpec 1.1.3.
# :synoposis
#   like bellow:
#    describe Foo do
#      before do
#        @array = Foo.some_method
#      end
#      it "all should be nil" do
#        # This is equivalent to @array.each{|item| item.should be_nil}.
#        all_of(@array).should be_nil
#      end
#    end
# :author
#   $Author$
# :licence
#   MIT license

module Spec
  module Example
    if RUBY_VERSION >= '1.9'
      BasicObject = Kernel::BasicObject
    else
      class BasicObject
        instance_methods.each do |name|
          unless %w[ object_id __id__ __send__ respond_to? ].include?(name)
            undef_method(name)
          end
        end
        self
      end
    end

    class EachOfProxy < BasicObject
      def initialize(targets)
        @targets = targets
      end
      def method_missing(msg, *args)
        new_targets = @targets.map { |target|
          target.__send__(msg, *args)
        }
        EachOfProxy.new(new_targets)
      end
    end

    module ExampleMethods
      def each_of(targets)
        EachOfProxy.new(targets)
      end
      alias all_of each_of
    end
  end
end
