Sha256: 06e56d8b8d5df5bde16e685014a88694096297de3792bee827847e0ac979972a

Contents?: true

Size: 734 Bytes

Versions: 1

Compression:

Stored size: 734 Bytes

Contents

module ActionDispatch::Routing
  class Mapper
    def contexts(*names, &blk)
      names.reverse.each do |name|
        blk = lambda do |blk|
          lambda { context(name, &blk) }
        end.call(blk)
      end

      blk.call
    end

    def context(name, &blk)
      name = "#{name}_context".classify.constantize unless name.is_a?(Class)
      ctx = name.new
      path = ":#{ctx.key}"
      path = "(#{path})" unless ctx.respond_to?(:required?) and ctx.required?

      constraints ->(req) { (req.env['contexts'] ||= {})[ctx.key] = ctx } do
        if ctx.respond_to?(:constraints)
          options = { constraints: { ctx.key => ctx.constraints } }
        end

        scope(path, options, &blk)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contexts-1.0.0 lib/mapper.rb