Sha256: aef914ecbcaf541c540c36a102c257a24a0ae403d4811c66455a8928255a3cc4

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require File.expand_path("spec_helper", File.dirname(File.dirname(__FILE__)))
require 'date'

describe "class_matchers plugin" do 
  it "allows class specific regexps with type conversion for class matchers" do
    app(:bare) do
      plugin :class_matchers
      class_matcher(Date, /(\d\d\d\d)-(\d\d)-(\d\d)/){|y,m,d| [Date.new(y.to_i, m.to_i, d.to_i)]}
      class_matcher(Array, /(\w+)\/(\w+)/){|a, b| [[a, 1], [b, 2]]}
      class_matcher(Hash, /(\d+)\/(\d+)/){|a, b| [{a.to_i=>b.to_i}]}

      route do |r|
        r.on Array do |(a,b), (c,d)|
          r.get Date do |date|
            [date.year, date.month, date.day, a, b, c, d].join('-')
          end
          r.get Hash do |h|
            [h.inspect, a, b, c, d].join('-')
          end
          r.get Array do |(a1,b1), (c1,d1)|
            [a1, b1, c1, d1, a, b, c, d].join('-')
          end
          r.is do
            [a, b, c, d].join('-')
          end
          "array"
        end
        ""
      end
    end

    body("/c").must_equal ''
    body("/c/d").must_equal 'c-1-d-2'
    body("/c/d/e").must_equal 'array'
    body("/c/d/2009-10-a").must_equal 'array'
    body("/c/d/2009-10-01").must_equal '2009-10-1-c-1-d-2'
    body("/c/d/1/2").must_equal '{1=>2}-c-1-d-2'
    body("/c/d/e/f").must_equal 'e-1-f-2-c-1-d-2'
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roda-2.29.0 spec/plugin/class_matchers_spec.rb
roda-2.28.0 spec/plugin/class_matchers_spec.rb
roda-2.27.0 spec/plugin/class_matchers_spec.rb