Class: Walle::Robot::Router::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/walle/robot/router/builder.rb

Constant Summary collapse

ROUTE_OPTIONS =
i{controller prefix direct delimiter}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Builder

Returns a new instance of Builder.



13
14
15
# File 'lib/walle/robot/router/builder.rb', line 13

def initialize(router)
  @router = router
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



11
12
13
# File 'lib/walle/robot/router/builder.rb', line 11

def router
  @router
end

Instance Method Details

#build(block) ⇒ Object



53
54
55
# File 'lib/walle/robot/router/builder.rb', line 53

def build(block)
  instance_eval(&block)
end

#command(*commands, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/walle/robot/router/builder.rb', line 24

def command(*commands, &block)
  arguments = commands.extract_options!
  options   = arguments.extract!(*ROUTE_OPTIONS)

  regexp = { command: /#{commands.join(?|)}/ }.merge(arguments).map do |name, regexp|
    regexp.is_a?(Regexp) ? "(?<#{name}>#{regexp.source})" : "#{regexp}"
  end.join(options[:delimiter].try(:source) || '\s+')

  match(Regexp.new(regexp), options, &block)
end

#default(options = {}, &block) ⇒ Object



48
49
50
51
# File 'lib/walle/robot/router/builder.rb', line 48

def default(options = {}, &block)
  controller = extract_controller(options, &block)
  router.default[!!options[:direct]] = Route.new(options.slice(*ROUTE_OPTIONS).merge(controller: controller))
end

#direct(&block) ⇒ Object



35
36
37
# File 'lib/walle/robot/router/builder.rb', line 35

def direct(&block)
  with_options(direct: true, &block)
end

#match(regexp, options = {}, &block) ⇒ Object



43
44
45
46
# File 'lib/walle/robot/router/builder.rb', line 43

def match(regexp, options = {}, &block)
  controller = extract_controller(options, &block)
  router.routes << Route.new(options.slice(*ROUTE_OPTIONS).merge(controller: controller, regexp: regexp))
end

#pattern(*patterns) ⇒ Object



17
18
# File 'lib/walle/robot/router/builder.rb', line 17

def pattern(*patterns)
end

#prefix(value, &block) ⇒ Object



39
40
41
# File 'lib/walle/robot/router/builder.rb', line 39

def prefix(value, &block)
  with_options(prefix: value, &block)
end

#use(middleware, *args) ⇒ Object



20
21
22
# File 'lib/walle/robot/router/builder.rb', line 20

def use(middleware, *args)
  router.use(middleware, *args)
end