Class: Rivulet::Routing::Mapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rivulet/routing/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(routes, prefix: '', scopes: []) ⇒ Mapper

Returns a new instance of Mapper.



10
11
12
13
14
# File 'lib/rivulet/routing/mapper.rb', line 10

def initialize(routes, prefix: '', scopes: [])
  @routes = routes
  @prefix = prefix
  @scopes = scopes
end

Instance Method Details

#build_callable(to) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rivulet/routing/mapper.rb', line 51

def build_callable(to)
  case to
  when String
    handler, action = to.split('#')
  when Hash
    handler, action = to.values_at(:to, :action)
  else
    raise 'Cannot parse route handler'
  end

  ->(input) { ::Handlers[handler].send(action, input) }
end

#draw(&block) ⇒ Object



39
40
41
# File 'lib/rivulet/routing/mapper.rb', line 39

def draw(&block)
  instance_eval(&block)
end

#each(&block) ⇒ Object



6
7
8
# File 'lib/rivulet/routing/mapper.rb', line 6

def each(&block)
  @routes.each(&block)
end

#scope(name, &block) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rivulet/routing/mapper.rb', line 43

def scope(name, &block)
  Mapper.new(
    @routes,
    prefix: join(@prefix, name.to_s),
    scopes: @scopes + [name]
  ).instance_eval(&block)
end