lib/mothership/inputs.rb in mothership-0.2.3 vs lib/mothership/inputs.rb in mothership-0.2.4
- old
+ new
@@ -20,12 +20,12 @@
end
def given(name)
if @inputs.key?(name)
@inputs[name]
- else
- @given[name]
+ elsif (given = @given[name]) != :interact
+ given
end
end
def merge(inputs)
self.class.new(@command, @context, @inputs.merge(inputs), @given, @global)
@@ -41,15 +41,30 @@
names.each do |n|
given.delete(n)
inputs.delete(n)
end
- self.class.new(@command, @context, given, inputs, @global)
+ self.class.new(@command, @context, inputs, given, @global)
end
def [](name, *args)
- get(name, @context, *args)
+ return @inputs[name] if @inputs.key?(name)
+
+ if @command && meta = @command.inputs[name]
+ # special case so #invoke can be called with singular-named inputs
+ singular = meta[:singular]
+
+ if @inputs.key?(singular)
+ return @inputs[name] = [@inputs[singular]]
+ end
+ end
+
+ val = get(name, @context, *args)
+
+ @inputs[name] = val unless meta && meta[:forget]
+
+ val
end
def interact(name, *args)
meta =
if @command
@@ -69,38 +84,31 @@
# 3. given
# 4. given, singular
# 5. global
# 6. global, singular
def get(name, context, *args)
- return @inputs[name] if @inputs.key?(name)
-
before_input = @current_input
@current_input = [name, args]
if @command && meta = @command.inputs[name]
- # special case so #invoke can be called with singular-named inputs
- singular = meta[:singular]
- return @inputs[name] = [@inputs[singular]] if @inputs.key?(singular)
-
found, val = find_in(@given, name, meta, context, *args)
elsif meta = Mothership.global_option(name)
found, val = find_in(@global, name, meta, context, *args)
end
return val if not found
if val == :interact && interact = meta[:interact]
- @inputs[name] = context.instance_exec(*args, &interact)
+ context.instance_exec(*args, &interact)
else
- @inputs[name] = convert_given(meta, context, val, *args)
+ convert_given(meta, context, val, *args)
end
ensure
@current_input = before_input
end
def forget(name)
- @given.delete(name)
@inputs.delete(name)
end
def interactive?(name)
@given[name] == :interact
@@ -115,15 +123,10 @@
[true, where[name]]
elsif where.key?(singular)
[true, [where[singular]]]
else
# no value given; set as default
- val = default_for(meta, context, *args)
-
- # cache default value
- @inputs[name] = val unless meta[:forget]
-
- [false, val]
+ [false, default_for(meta, context, *args)]
end
end
def convert_given(meta, context, given, *args)
if convert = meta[:from_given]