Sha256: dd2d177f4b9ec318a9b98fa2fc789e15dc4a172cc4bf44a76c43310e99d681e8

Contents?: true

Size: 1.23 KB

Versions: 30

Compression:

Stored size: 1.23 KB

Contents

require_relative "../spec_helper"
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

30 entries across 30 versions & 1 rubygems

Version Path
roda-3.28.0 spec/plugin/class_matchers_spec.rb
roda-3.27.0 spec/plugin/class_matchers_spec.rb
roda-3.26.0 spec/plugin/class_matchers_spec.rb
roda-3.25.0 spec/plugin/class_matchers_spec.rb
roda-3.24.0 spec/plugin/class_matchers_spec.rb
roda-3.23.0 spec/plugin/class_matchers_spec.rb
roda-3.22.0 spec/plugin/class_matchers_spec.rb
roda-3.21.0 spec/plugin/class_matchers_spec.rb
roda-3.20.0 spec/plugin/class_matchers_spec.rb
roda-3.19.0 spec/plugin/class_matchers_spec.rb
roda-3.18.0 spec/plugin/class_matchers_spec.rb
roda-3.17.0 spec/plugin/class_matchers_spec.rb
roda-3.16.0 spec/plugin/class_matchers_spec.rb
roda-3.15.0 spec/plugin/class_matchers_spec.rb
roda-3.14.1 spec/plugin/class_matchers_spec.rb
roda-3.14.0 spec/plugin/class_matchers_spec.rb
roda-3.13.0 spec/plugin/class_matchers_spec.rb
roda-3.12.0 spec/plugin/class_matchers_spec.rb
roda-3.11.0 spec/plugin/class_matchers_spec.rb
roda-3.10.0 spec/plugin/class_matchers_spec.rb