ForwardsToEnumerable
Posted by kev Tue, 30 Oct 2007 01:16:00 GMT
I haven’t yet decided if this is a good idea or not.
I’ll be at RubyConf this weekend. Say hello, if you get the urge.
require "test/unit"
require 'rubygems'
require 'mocha'
require 'stubba'
module ForwardsToEnumerable
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def forward_to_enum(instance_var, *meths)
meths.each do |meth|
class_eval <<-METH
def #{meth}(*args, &block)
#{instance_var.to_s}.each do |i|
i.send(:#{meth}, *args, &block)
end
end
METH
end
end
end
end
class ForwardsToArray
include ForwardsToEnumerable
forward_to_enum :@array, :foo, :bar, :baz
def initialize(array)
@array = array
end
end
class TestForwardsToArray < Test::Unit::TestCase
def test_forward_to_enum
items = [mock(), mock(), mock()]
items.each {|i| i.expects(:foo); i.expects(:bar); i.expects(:baz) }
f = ForwardsToArray.new(items)
f.foo
f.bar
f.baz
end
end
