Class: SolidObjects::Web::Route
- Inherits:
-
Object
- Object
- SolidObjects::Web::Route
- Defined in:
- lib/solid_objects/web/route.rb,
sig/generated/lib/solid_objects/web/route.rbs
Constant Summary collapse
- NAMED_SEGMENT =
A named segment stops at the next slash, so
/instances/:idnever swallows/instances/1/pauseand route order cannot hide a page. %r{/([^/]*):([^.:$/]+)}- SEGMENT_CAPTURE =
'/\1(?<\2>[^$/]+)'
Instance Attribute Summary collapse
- #handler ⇒ Object readonly
- #pattern ⇒ Object readonly
- #policy ⇒ Object readonly
- #request_method ⇒ Object readonly
Instance Method Summary collapse
- #capture(path) ⇒ Hash[Symbol, String?]
- #compile(pattern) ⇒ String, Regexp
-
#initialize(request_method:, pattern:, policy:, handler:) ⇒ Route
constructor
A new instance of Route.
- #match?(path) ⇒ Boolean
Constructor Details
#initialize(request_method:, pattern:, policy:, handler:) ⇒ Route
Returns a new instance of Route.
20 21 22 23 24 25 26 |
# File 'lib/solid_objects/web/route.rb', line 20 def initialize(request_method:, pattern:, policy:, handler:) @request_method = request_method @pattern = pattern @policy = policy @handler = handler @matcher = compile(pattern) end |
Instance Attribute Details
#handler ⇒ Object (readonly)
17 18 19 |
# File 'lib/solid_objects/web/route.rb', line 17 def handler @handler end |
#pattern ⇒ Object (readonly)
17 18 19 |
# File 'lib/solid_objects/web/route.rb', line 17 def pattern @pattern end |
#policy ⇒ Object (readonly)
17 18 19 |
# File 'lib/solid_objects/web/route.rb', line 17 def policy @policy end |
#request_method ⇒ Object (readonly)
17 18 19 |
# File 'lib/solid_objects/web/route.rb', line 17 def request_method @request_method end |
Instance Method Details
#capture(path) ⇒ Hash[Symbol, String?]
36 37 38 39 40 41 42 43 |
# File 'lib/solid_objects/web/route.rb', line 36 def capture(path) return {} if @matcher.is_a?(String) match = @matcher.match(path) return {} unless match match.named_captures.transform_keys(&:to_sym) end |
#compile(pattern) ⇒ String, Regexp
48 49 50 51 52 |
# File 'lib/solid_objects/web/route.rb', line 48 def compile(pattern) return pattern unless pattern.match?(NAMED_SEGMENT) Regexp.new("\\A#{pattern.gsub(NAMED_SEGMENT, SEGMENT_CAPTURE)}\\z") end |
#match?(path) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/solid_objects/web/route.rb', line 29 def match?(path) return @matcher == path if @matcher.is_a?(String) @matcher.match?(path) end |