lib/mothership/inputs.rb in mothership-0.1.5 vs lib/mothership/inputs.rb in mothership-0.2.0

- old
+ new

@@ -1,9 +1,12 @@ class Mothership class Inputs attr_reader :inputs, :given, :global + # the input being processed; set during #get + attr_reader :current_input + def initialize( command, context = nil, inputs = {}, given = {}, global = {}) @command = command @context = context @@ -45,20 +48,35 @@ def [](name, *args) get(name, @context, *args) end + def interact(name, *args) + meta = + if @command + @command.inputs[name] + else + Mothership.global_option(name) + end + + if interact = meta[:interact] + @context.instance_exec(*args, &interact) + end + end + # search: # 1. cache # 2. cache, singular # 3. given # 4. given, singular # 5. global # 6. global, singular def get(name, context, *args) return @inputs[name] if @inputs.key?(name) + @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) @@ -67,11 +85,17 @@ found, val = find_in(@global, name, meta, context, *args) end return val if not found - @inputs[name] = convert_given(meta, context, val, *args) + if val == :interact && interact = meta[:interact] + @inputs[name] = context.instance_exec(*args, &interact) + else + @inputs[name] = convert_given(meta, context, val, *args) + end + ensure + @current_input = nil end def forget(name) @given.delete(name) @inputs.delete(name) @@ -118,9 +142,11 @@ if default.respond_to? :to_proc context.instance_exec(*args, &default) else default end + elsif interact = meta[:interact] + context.instance_exec(*args, &interact) elsif meta[:type] == :boolean false elsif meta[:argument] == :splat [] end