-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathpath_parser_service.rb
More file actions
37 lines (34 loc) · 1010 Bytes
/
path_parser_service.rb
File metadata and controls
37 lines (34 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class PathParserService
def initialize(template, path)
@template = template
@path = path
end
def call
components = @path.match(path_parse_pattern)&.named_captures&.symbolize_keys
return {} if components.nil?
components.merge({
tags: components[:tags]&.split("/")&.compact_blank,
model_id: nil # discard ID, never gonna use it in parsing
}).compact
end
private
def path_parse_pattern
Regexp.new("^/?.*?" +
@template.gsub(/{.+?}/) { |token|
case token
when "{tags}"
"(?<tags>[[:print:]]*)"
when "{creator}"
"(?<creator>[[:print:]&&[^/]]*?)"
when "{collection}"
"(?<collection>[[:print:]&&[^/]]*?)"
when "{modelName}"
"(?<model_name>[[:print:]&&[^/]]*?)"
when "{modelId}"
"" # Always detected by default, moved below, kept for compatibility
else
"[[:print:]&&[^/]]*"
end
} + "(?<model_id>#[[:digit:]]+)?" + "$")
end
end