Class: Walle::Robot::Router::Builder
- Inherits:
-
Object
- Object
- Walle::Robot::Router::Builder
- Defined in:
- lib/walle/robot/router/builder.rb
Constant Summary collapse
- ROUTE_OPTIONS =
i{controller prefix direct delimiter}
Instance Attribute Summary collapse
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Instance Method Summary collapse
- #build(block) ⇒ Object
- #command(*commands, &block) ⇒ Object
- #default(options = {}, &block) ⇒ Object
- #direct(&block) ⇒ Object
-
#initialize(router) ⇒ Builder
constructor
A new instance of Builder.
- #match(regexp, options = {}, &block) ⇒ Object
- #pattern(*patterns) ⇒ Object
- #prefix(value, &block) ⇒ Object
- #use(middleware, *args) ⇒ Object
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
#router ⇒ Object (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. = arguments.extract!(*ROUTE_OPTIONS) regexp = { command: /#{commands.join(?|)}/ }.merge(arguments).map do |name, regexp| regexp.is_a?(Regexp) ? "(?<#{name}>#{regexp.source})" : "#{regexp}" end.join([:delimiter].try(:source) || '\s+') match(Regexp.new(regexp), , &block) end |
#default(options = {}, &block) ⇒ Object
48 49 50 51 |
# File 'lib/walle/robot/router/builder.rb', line 48 def default( = {}, &block) controller = extract_controller(, &block) router.default[!![:direct]] = Route.new(.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) (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, = {}, &block) controller = extract_controller(, &block) router.routes << Route.new(.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) (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 |