Improve command handling

This commit is contained in:
Anton Vakhrushev 2019-10-06 18:30:36 +03:00
parent 927ca47b04
commit 3669e31c66
3 changed files with 20 additions and 3 deletions

View File

@ -12,7 +12,7 @@
- [x] Цикл ввода-вывода - [x] Цикл ввода-вывода
- [ ] Просмотр задач в очереди - [ ] Просмотр задач в очереди
- [ ] Время завершения задачи при создании - [ ] Время завершения задачи при создании
- [ ] Сообщение о выйгрыше - [x] Сообщение о выйгрыше
## Механики ## Механики

View File

@ -40,4 +40,17 @@ describe CLI::CommandRouter do
router.handle "plus 1 3 6" router.handle "plus 1 3 6"
x.should eq 10 x.should eq 10
end end
it "should match commands from begin of string" do
router = CLI::CommandRouter.new
x = 5
router.add "plus" do |p|
x += 10
end
router.add "mult and plus" do |p|
x = x * 2 + 10
end
router.handle "mult and plus"
x.should eq 20
end
end end

View File

@ -25,6 +25,10 @@ class CLI::CommandRouter
end end
private def route_to_regex(route) : Regex private def route_to_regex(route) : Regex
Regex.new('^' + split_route route)
end
private def split_route(route) : String
vals = route.partition(/\{[a-z]+?\}/) vals = route.partition(/\{[a-z]+?\}/)
param = vals[1].lstrip('{').rstrip('}') param = vals[1].lstrip('{').rstrip('}')
result = "" result = ""
@ -35,8 +39,8 @@ class CLI::CommandRouter
result += sprintf "(?P<%s>.+?)", param result += sprintf "(?P<%s>.+?)", param
end end
if vals[2] != "" if vals[2] != ""
result += route_to_regex(vals[2]).source result += split_route(vals[2])
end end
Regex.new(result) result
end end
end end