mothership 0.3.5 → 0.4.0

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.
@@ -31,6 +31,11 @@ class Mothership
31
31
  @command.add_input(name, options, &interact)
32
32
  end
33
33
 
34
+ # specify a module that defines interactions for each input
35
+ def interactions(mod)
36
+ @command.interactions = mod
37
+ end
38
+
34
39
  # register a command
35
40
  def method_added(name)
36
41
  return unless @command
@@ -41,8 +46,8 @@ class Mothership
41
46
  @command = nil
42
47
  end
43
48
 
44
- def alias_command(orig, new)
45
- @@commands[new] = @@commands[orig]
49
+ def alias_command(new, orig = nil)
50
+ @@commands[new] = orig ? @@commands[orig] : @command
46
51
  end
47
52
  end
48
53
 
@@ -89,6 +94,6 @@ class Mothership
89
94
 
90
95
  raise "no active input" unless input
91
96
 
92
- @input.interact(input, *args)
97
+ @input.interact(input, self, *args)
93
98
  end
94
99
  end
@@ -2,7 +2,7 @@ require "mothership/inputs"
2
2
 
3
3
  class Mothership
4
4
  class Command
5
- attr_accessor :name, :description
5
+ attr_accessor :name, :description, :interactions
6
6
 
7
7
  attr_reader :context, :inputs, :arguments, :flags
8
8
 
@@ -60,9 +60,13 @@ class Mothership
60
60
  @before.each { |f, c| c.new.instance_exec(&f) }
61
61
 
62
62
  name = @name
63
+
63
64
  ctx = @context.new(self)
65
+ ctx.extend @interactions if @interactions
66
+
64
67
  ctx.input = Inputs.new(self, ctx, inputs, given, global)
65
68
 
69
+
66
70
  action = proc do |*given_inputs|
67
71
  ctx.input = given_inputs.first || ctx.input
68
72
  ctx.run(name)
@@ -90,6 +94,16 @@ class Mothership
90
94
  options[:interact] = interact if interact
91
95
  options[:description] = options.delete(:desc) if options.key?(:desc)
92
96
 
97
+ options[:type] ||=
98
+ case options[:default]
99
+ when true, false
100
+ :boolean
101
+ when Integer
102
+ :integer
103
+ when Float
104
+ :floating
105
+ end
106
+
93
107
  unless options[:hidden]
94
108
  @flags["--#{name.to_s.gsub("_", "-")}"] = name
95
109
 
@@ -73,7 +73,7 @@ class Mothership
73
73
  val
74
74
  end
75
75
 
76
- def interact(name, *args)
76
+ def interact(name, context, *args)
77
77
  meta =
78
78
  if @command
79
79
  @command.inputs[name]
@@ -81,8 +81,13 @@ class Mothership
81
81
  Mothership.global_option(name)
82
82
  end
83
83
 
84
- if interact = meta[:interact]
85
- @context.instance_exec(*args, &interact)
84
+ interact = meta[:interact] || :"ask_#{name}"
85
+
86
+ case interact
87
+ when Symbol, String
88
+ context.send(interact, *args)
89
+ else
90
+ context.instance_exec(*args, &interact)
86
91
  end
87
92
  end
88
93
 
@@ -107,8 +112,8 @@ class Mothership
107
112
 
108
113
  return [false, val] if not found
109
114
 
110
- if val == :interact && interact = meta[:interact]
111
- [true, context.instance_exec(*args, &interact)]
115
+ if val == :interact
116
+ [true, interact(name, context, *args)]
112
117
  else
113
118
  [true, convert_given(meta, context, val, *args)]
114
119
  end
@@ -136,7 +141,7 @@ class Mothership
136
141
  [true, [where[singular]]]
137
142
  else
138
143
  # no value given; set as default
139
- [false, default_for(meta, context, *args)]
144
+ [false, default_for(name, meta, context, *args)]
140
145
  end
141
146
  end
142
147
 
@@ -153,9 +158,9 @@ class Mothership
153
158
  case meta[:type]
154
159
  when :integer, :number, :numeric
155
160
  given.to_i
156
- when :float
161
+ when :float, :floating
157
162
  given.to_f
158
- when :boolean
163
+ when :bool, :boolean
159
164
  given == "true"
160
165
  else
161
166
  given
@@ -163,7 +168,7 @@ class Mothership
163
168
  end
164
169
  end
165
170
 
166
- def default_for(meta, context, *args)
171
+ def default_for(name, meta, context, *args)
167
172
  if meta.key?(:default)
168
173
  default = meta[:default]
169
174
 
@@ -172,8 +177,8 @@ class Mothership
172
177
  else
173
178
  default
174
179
  end
175
- elsif interact = meta[:interact]
176
- context.instance_exec(*args, &interact)
180
+ elsif meta[:interact] || context.respond_to?(:"ask_#{name}", true)
181
+ interact(name, context, *args)
177
182
  elsif meta[:type] == :boolean
178
183
  false
179
184
  elsif meta[:argument] == :splat
@@ -41,7 +41,7 @@ class Mothership
41
41
 
42
42
  input = @command.inputs[name]
43
43
 
44
- if input[:interact] && argv.first == "_"
44
+ if argv.first == "--ask"
45
45
  @given[name] = :interact
46
46
  argv.shift
47
47
  next
@@ -162,7 +162,7 @@ class Mothership
162
162
  "--#$1"
163
163
 
164
164
  when /^--ask-(.+)/
165
- argv.unshift "_"
165
+ argv.unshift "--ask"
166
166
  "--#$1"
167
167
 
168
168
  # --foo=bar form
@@ -1,3 +1,3 @@
1
1
  class Mothership
2
- VERSION = "0.3.5"
2
+ VERSION = "0.4.0"
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: 25
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 5
10
- version: 0.3.5
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-12-07 00:00:00 -08:00
18
+ date: 2012-12-12 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency