Class: ActionDispatch::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/mapper.rb

Instance Method Summary collapse

Instance Method Details

#context(name, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mapper.rb', line 13

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

  constraints = lambda do |req|
    (req.env['contexts'] ||= {})[ctx.key] = ctx
    ctx.matches_constraints?(req)
  end

  scope(path, constraints: constraints, &blk)
end

#contexts(*names, &blk) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/mapper.rb', line 3

def contexts(*names, &blk)
  names.reverse.each do |name|
    blk = lambda do |blk|
      lambda { context(name, &blk) }
    end.call(blk)
  end

  blk.call
end