mothership 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,7 +54,7 @@ class Mothership
54
54
  # invoke a command with the given inputs
55
55
  def invoke(name, inputs = {})
56
56
  if cmd = @@commands[name]
57
- cmd.invoke(inputs)
57
+ cmd.invoke({}, inputs)
58
58
  else
59
59
  unknown_command(name)
60
60
  end
@@ -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)
@@ -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
 
@@ -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
- @inputs.key?(name)
13
+ @given.key?(name)
14
14
  end
15
15
 
16
16
  def given(name)
17
- @inputs[name]
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 @cache[name] if @cache.key? name
40
+ return @inputs[name] if @inputs.key?(name)
39
41
 
40
42
  meta = @command.inputs[name]
41
43
  return unless meta
42
44
 
43
- if @inputs.key?(name) && @inputs[name] != []
44
- val =
45
- if convert = meta[:from_given]
46
- if @inputs[name].is_a?(Array)
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
- return @cache[name] = val
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
- val =
61
- if meta[:default].respond_to? :to_proc
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
- @cache[name] = val
59
+ @inputs[name] = val
81
60
  end
82
61
 
83
62
  val
84
63
  end
85
64
 
86
65
  def forget(name)
87
- @cache.delete(name)
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
@@ -124,7 +124,7 @@ class Mothership
124
124
  end
125
125
  end
126
126
 
127
- raise ExtraArguments.new(@command.name) unless args.empty?
127
+ raise ExtraArguments.new(@command.name, args) unless args.empty?
128
128
  end
129
129
 
130
130
  private
@@ -1,3 +1,3 @@
1
1
  class Mothership
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
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: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci