mothership 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mothership/base.rb +1 -1
- data/lib/mothership/command.rb +2 -2
- data/lib/mothership/errors.rb +3 -2
- data/lib/mothership/inputs.rb +52 -41
- data/lib/mothership/parser.rb +1 -1
- data/lib/mothership/version.rb +1 -1
- metadata +3 -3
data/lib/mothership/base.rb
CHANGED
data/lib/mothership/command.rb
CHANGED
@@ -52,12 +52,12 @@ class Mothership
|
|
52
52
|
str
|
53
53
|
end
|
54
54
|
|
55
|
-
def invoke(inputs)
|
55
|
+
def invoke(given, inputs = {})
|
56
56
|
@before.each { |f, c| c.new.instance_exec(&f) }
|
57
57
|
|
58
58
|
name = @name
|
59
59
|
ctx = @context.new(self)
|
60
|
-
input = Inputs.new(self, ctx, inputs)
|
60
|
+
input = Inputs.new(self, ctx, given, inputs)
|
61
61
|
|
62
62
|
action = proc do |*given_inputs|
|
63
63
|
ctx.send(name, given_inputs.first || input)
|
data/lib/mothership/errors.rb
CHANGED
@@ -14,12 +14,13 @@ class Mothership
|
|
14
14
|
end
|
15
15
|
|
16
16
|
class ExtraArguments < Error
|
17
|
-
def initialize(cmd)
|
17
|
+
def initialize(cmd, extra)
|
18
18
|
@command = cmd
|
19
|
+
@extra = extra
|
19
20
|
end
|
20
21
|
|
21
22
|
def to_s
|
22
|
-
"#{@command}: too many arguments"
|
23
|
+
"#{@command}: too many arguments; extra: #{@extra.join(" ")}"
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
data/lib/mothership/inputs.rb
CHANGED
@@ -2,32 +2,34 @@ class Mothership
|
|
2
2
|
class Inputs
|
3
3
|
attr_reader :inputs
|
4
4
|
|
5
|
-
def initialize(command, context = nil, inputs = {})
|
5
|
+
def initialize(command, context = nil, given = {}, inputs = {})
|
6
6
|
@command = command
|
7
7
|
@context = context
|
8
|
+
@given = given
|
8
9
|
@inputs = inputs
|
9
|
-
@cache = {}
|
10
10
|
end
|
11
11
|
|
12
12
|
def given?(name)
|
13
|
-
@
|
13
|
+
@given.key?(name)
|
14
14
|
end
|
15
15
|
|
16
16
|
def given(name)
|
17
|
-
@
|
17
|
+
@given[name]
|
18
18
|
end
|
19
19
|
|
20
20
|
def merge(inputs)
|
21
|
-
self.class.new(@command, @context, @inputs.merge(inputs))
|
21
|
+
self.class.new(@command, @context, @given, @inputs.merge(inputs))
|
22
22
|
end
|
23
23
|
|
24
24
|
def without(*names)
|
25
|
+
given = @given.dup
|
25
26
|
inputs = @inputs.dup
|
26
27
|
names.each do |n|
|
28
|
+
given.delete(n)
|
27
29
|
inputs.delete(n)
|
28
30
|
end
|
29
31
|
|
30
|
-
self.class.new(@command, @context, inputs)
|
32
|
+
self.class.new(@command, @context, given, inputs)
|
31
33
|
end
|
32
34
|
|
33
35
|
def [](name, *args)
|
@@ -35,57 +37,66 @@ class Mothership
|
|
35
37
|
end
|
36
38
|
|
37
39
|
def get(name, context, *args)
|
38
|
-
return @
|
40
|
+
return @inputs[name] if @inputs.key?(name)
|
39
41
|
|
40
42
|
meta = @command.inputs[name]
|
41
43
|
return unless meta
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
@inputs[name].collect do |i|
|
48
|
-
@context.instance_exec(i, *args, &convert)
|
49
|
-
end
|
50
|
-
else
|
51
|
-
@context.instance_exec(@inputs[name], *args, &convert)
|
52
|
-
end
|
53
|
-
else
|
54
|
-
@inputs[name]
|
55
|
-
end
|
45
|
+
singular = meta[:singular]
|
46
|
+
return @inputs[name] = [@inputs[singular]] if @inputs.key?(singular)
|
47
|
+
|
48
|
+
given = @given[name] if @given.key?(name)
|
56
49
|
|
57
|
-
|
50
|
+
# value given; convert if needed
|
51
|
+
if given && given != []
|
52
|
+
return @inputs[name] = convert_given(meta, context, given, *args)
|
58
53
|
end
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
unless context
|
63
|
-
raise "no context for input request"
|
64
|
-
end
|
65
|
-
|
66
|
-
context.instance_exec(*args, &meta[:default])
|
67
|
-
elsif meta[:default]
|
68
|
-
meta[:default]
|
69
|
-
elsif meta[:type] == :boolean
|
70
|
-
false
|
71
|
-
elsif meta[:argument] == :splat
|
72
|
-
if meta[:singular] && single = @inputs[meta[:singular]]
|
73
|
-
[single]
|
74
|
-
else
|
75
|
-
[]
|
76
|
-
end
|
77
|
-
end
|
55
|
+
# no value given; set as default
|
56
|
+
val = default_for(meta, context, *args)
|
78
57
|
|
79
58
|
unless meta[:forget]
|
80
|
-
@
|
59
|
+
@inputs[name] = val
|
81
60
|
end
|
82
61
|
|
83
62
|
val
|
84
63
|
end
|
85
64
|
|
86
65
|
def forget(name)
|
87
|
-
@
|
66
|
+
@given.delete(name)
|
88
67
|
@inputs.delete(name)
|
89
68
|
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def convert_given(meta, context, given, *args)
|
73
|
+
if convert = meta[:from_given]
|
74
|
+
if given.is_a?(Array)
|
75
|
+
given.collect do |i|
|
76
|
+
context.instance_exec(i, *args, &convert)
|
77
|
+
end
|
78
|
+
else
|
79
|
+
context.instance_exec(given, *args, &convert)
|
80
|
+
end
|
81
|
+
else
|
82
|
+
given
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def default_for(meta, context, *args)
|
87
|
+
if meta.key?(:default)
|
88
|
+
default = meta[:default]
|
89
|
+
|
90
|
+
if default.respond_to? :to_proc
|
91
|
+
context.instance_exec(*args, &default)
|
92
|
+
else
|
93
|
+
default
|
94
|
+
end
|
95
|
+
elsif meta[:type] == :boolean
|
96
|
+
false
|
97
|
+
elsif meta[:argument] == :splat
|
98
|
+
[]
|
99
|
+
end
|
100
|
+
end
|
90
101
|
end
|
91
102
|
end
|
data/lib/mothership/parser.rb
CHANGED
data/lib/mothership/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mothership
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Suraci
|