Thank you Justin & David.

I was able to to use Justin solution. I also tried hard to understand and use 
David's solution but I could not. 

Now I am just curious. What is &HaveXXXHelpers.on_column? A reference to a 
class method?

Regards,

Gustavo Delfino

On May 25, 2011, at 2:04 PM, Justin Ko wrote:

> 
> 
> On Wed, May 25, 2011 at 11:20 AM, David Chelimsky <dchelim...@gmail.com> 
> wrote:
> On May 25, 2011, at 10:07 AM, Gustavo Delfino wrote:
> 
> > Hello all. Thanks to subjects, custom matchers and fluent chaining I was 
> > able to greatly simplify my spec. But now I want to DRY my custom matchers.
> >
> > I have two custom matchers: 'have_text' and 'have_number' and both contain 
> > exactly the same chains 'on_column' and 'on_columns'.
> >
> > Is there a way to DRY this up?
> >
> >
> > module CustomColumnsMatchers
> >
> >  RSpec::Matchers.define(:have_text) do |expected_string|
> >    chain(:on_column) do |colnumber|
> >      @index = colnumber - 1
> >      @width = 1
> >    end
> >    chain(:on_columns) do |range|
> >      a = range.to_a
> >      @index = Range.new( a[0]-1, a[-1]-1 )
> >      @width = a[-1] - a[0] + 1
> >    end
> >    match do |line|
> >      line[@index] == expected_string.ljust(@width)
> >    end
> >  end
> >
> >  RSpec::Matchers.define(:have_number) do |expected_number_string|
> >    chain(:on_column) do |colnumber|
> >      @index = colnumber - 1
> >      @width = 1
> >    end
> >    chain(:on_columns) do |range|
> >      a = range.to_a
> >      @index = Range.new( a[0]-1, a[-1]-1 )
> >      @width = a[-1] - a[0] + 1
> >    end
> >    match do |line|
> >      line[@index] == expected_number_string.rjust(@width,'0')
> >    end
> >  end
> >
> > end
> >
> >
> > Regards,
> >
> > Gustavo Delfino
> 
> I'd probably do something like:
> 
>  RSpec::Matchers.define(:have_text) do |expected_string|
>   chain(:on_column, &HaveXXXHelpers.on_column)
>   chain(:on_columns, &HaveXXXHelpers.on_columns)
>   match do |line|
>     line[@index] == expected_string.ljust(@width)
>   end
>  end
> 
> HTH,
> David
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> Ahh David beat me to the lambda approach! Here is another way:
> 
> 
> module CustomColumnsMatchers
> 
>  RSpec::Matchers.define(:have_text) do |expected_string|
>    extend ChainMethods
>    
>    match do |line|
>      line[@index] == expected_string.ljust(@width)
>    end
>  end
> 
>  RSpec::Matchers.define(:have_number) do |expected_number_string|
>    extend ChainMethods
>    
>    match do |line|
>      line[@index] == expected_number_string.rjust(@width,'0')
>    end
>  end
> 
>  module ChainMethods
>    def self.extended(matcher)
>      matcher.instance_eval do
>       chain(:on_column) do |colnumber|
>         @index = colnumber - 1
>         @width = 1
>       end
>       chain(:on_columns) do |range|
>          a = range.to_a
>          @index = Range.new( a[0]-1, a[-1]-1 )
>          @width = a[-1] - a[0] + 1
>       end
>     end
>   end
> end
> 
> end
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to