mvcli 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mvcli/router.rb +21 -3
- data/lib/mvcli/version.rb +1 -1
- data/spec/mvcli/router_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8df3bbd6ac8b26f4a48b1ed2d9c67a8ef4a8616
|
4
|
+
data.tar.gz: caff046276e71a1e374b6f58941e5f6a25422c78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72f2bfbd5958be1c910e1fe92d3259a43112e64f83563b837e76b6df153ee27f665406e8b98d336c3e737e8860dd08275d87717dd3112488774b3e04bd54289a
|
7
|
+
data.tar.gz: 70fd38e776ff6c3e8c3ad4b6b49efb2f96143366eadadd4416577a45ceb51d07c0cc009f4ba82141eb822b1966fc27c7d747d246080ef5ad7bd80b7bb34de4e0
|
data/lib/mvcli/router.rb
CHANGED
@@ -10,6 +10,11 @@ module MVCLI
|
|
10
10
|
def initialize(actions = nil)
|
11
11
|
@actions = actions || Map.new
|
12
12
|
@routes = []
|
13
|
+
@macros = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def macro(options)
|
17
|
+
@macros.push Macro.new options
|
13
18
|
end
|
14
19
|
|
15
20
|
def match(options)
|
@@ -19,22 +24,35 @@ module MVCLI
|
|
19
24
|
end
|
20
25
|
|
21
26
|
def call(command)
|
27
|
+
argv = @macros.reduce(command.argv) do |args, macro|
|
28
|
+
macro.expand args
|
29
|
+
end
|
22
30
|
@routes.each do |route|
|
23
|
-
if match = route.match(
|
31
|
+
if match = route.match(argv)
|
24
32
|
return match.call command
|
25
33
|
end
|
26
34
|
end
|
27
35
|
fail RoutingError, "no route matches '#{command.argv.join ' '}'"
|
28
36
|
end
|
29
37
|
|
38
|
+
class Macro
|
39
|
+
def initialize(options)
|
40
|
+
@pattern, @expansion = options.first
|
41
|
+
end
|
42
|
+
|
43
|
+
def expand(argv)
|
44
|
+
argv.join(" ").gsub(@pattern, @expansion).split /\s+/
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
30
48
|
class Route
|
31
49
|
def initialize(pattern, actions, action, options = {})
|
32
50
|
@pattern = Pattern.new pattern.to_s
|
33
51
|
@actions, @action, @options = actions, action, options
|
34
52
|
end
|
35
53
|
|
36
|
-
def match(
|
37
|
-
argv = MVCLI::Argv.new
|
54
|
+
def match(argv)
|
55
|
+
argv = MVCLI::Argv.new argv
|
38
56
|
match = @pattern.match(argv.arguments)
|
39
57
|
if match.matches?
|
40
58
|
proc do |command|
|
data/lib/mvcli/version.rb
CHANGED
data/spec/mvcli/router_spec.rb
CHANGED
@@ -45,6 +45,13 @@ describe "MVCLI::Router" do
|
|
45
45
|
And {@bindings[:id].should eql '6'}
|
46
46
|
end
|
47
47
|
|
48
|
+
context "with macros" do
|
49
|
+
Given { router.macro /(-h|--help) (.*)/ => "help \\2" }
|
50
|
+
Given { router.match "help me" => "help#me"}
|
51
|
+
When { invoke "--help me" }
|
52
|
+
Then { @action.should eql 'help#me' }
|
53
|
+
end
|
54
|
+
|
48
55
|
def invoke(route = '')
|
49
56
|
router.call mock(:Command, :argv => route.split(/\s+/))
|
50
57
|
end
|