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
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to