Class: SolidObjects::Web::Route

Inherits:
Object
  • Object
show all
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/:id never swallows /instances/1/pause and route order cannot hide a page.

Returns:

  • (::Regexp)
%r{/([^/]*):([^.:$/]+)}
SEGMENT_CAPTURE =

Returns:

  • (::String)
'/\1(?<\2>[^$/]+)'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method:, pattern:, policy:, handler:) ⇒ Route

Returns a new instance of Route.

RBS:

  • (request_method: String, pattern: String, policy: Hash[Symbol, String], handler: Proc) -> void

Parameters:

  • request_method: (String)
  • pattern: (String)
  • policy: (Hash[Symbol, String])
  • handler: (Proc)


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

#handlerObject (readonly)

RBS:

  • @matcher: String | Regexp

  • @request_method: String

  • @pattern: String

  • @policy: Hash[Symbol, String]

  • @handler: Proc

Returns:

  • (Object)


17
18
19
# File 'lib/solid_objects/web/route.rb', line 17

def handler
  @handler
end

#patternObject (readonly)

RBS:

  • @matcher: String | Regexp

  • @request_method: String

  • @pattern: String

  • @policy: Hash[Symbol, String]

  • @handler: Proc

Returns:

  • (Object)


17
18
19
# File 'lib/solid_objects/web/route.rb', line 17

def pattern
  @pattern
end

#policyObject (readonly)

RBS:

  • @matcher: String | Regexp

  • @request_method: String

  • @pattern: String

  • @policy: Hash[Symbol, String]

  • @handler: Proc

Returns:

  • (Object)


17
18
19
# File 'lib/solid_objects/web/route.rb', line 17

def policy
  @policy
end

#request_methodObject (readonly)

RBS:

  • @matcher: String | Regexp

  • @request_method: String

  • @pattern: String

  • @policy: Hash[Symbol, String]

  • @handler: Proc

Returns:

  • (Object)


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?]

RBS:

  • (String) -> Hash[Symbol, String?]

Parameters:

  • (String)

Returns:

  • (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

RBS:

  • (String) -> (String | Regexp)

Parameters:

  • (String)

Returns:

  • (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

RBS:

  • (String) -> bool

Parameters:

  • (String)

Returns:

  • (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