Class: RubyAPI::Router
- Inherits:
-
Object
- Object
- RubyAPI::Router
- Defined in:
- lib/fastrb/router.rb
Instance Attribute Summary collapse
-
#current_prefix ⇒ Object
readonly
Returns the value of attribute current_prefix.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #add_route(method, path, **opts, &block) ⇒ Object
- #find(method, path) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #pop_prefix(prefix) ⇒ Object
- #push_prefix(prefix) ⇒ Object
Constructor Details
Instance Attribute Details
#current_prefix ⇒ Object (readonly)
Returns the value of attribute current_prefix.
3 4 5 |
# File 'lib/fastrb/router.rb', line 3 def current_prefix @current_prefix end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/fastrb/router.rb', line 3 def routes @routes end |
Instance Method Details
#add_route(method, path, **opts, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fastrb/router.rb', line 11 def add_route(method, path, **opts, &block) full_path = @current_prefix + path pattern = compile_pattern(full_path) param_names = extract_param_names(full_path) route = { method: method, path: full_path, pattern: pattern, param_names: param_names, block: block, params: opts[:params] || {}, body: opts[:body], injected_deps: opts[:inject] || [], dependency_checks: opts[:depends] || [] } @routes << route @trie.insert(full_path, route) route end |
#find(method, path) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/fastrb/router.rb', line 41 def find(method, path) route = @trie.search(path) return nil unless route return nil unless route[:method].to_s == method.to_s route end |
#pop_prefix(prefix) ⇒ Object
37 38 39 |
# File 'lib/fastrb/router.rb', line 37 def pop_prefix(prefix) @current_prefix = @current_prefix[0...-prefix.length] end |
#push_prefix(prefix) ⇒ Object
33 34 35 |
# File 'lib/fastrb/router.rb', line 33 def push_prefix(prefix) @current_prefix = @current_prefix + prefix end |