lib/mothership/inputs.rb in mothership-0.0.2 vs lib/mothership/inputs.rb in mothership-0.0.3
- old
+ new
@@ -1,10 +1,10 @@
class Mothership
class Inputs
attr_reader :inputs
- def initialize(command, context, inputs = {})
+ def initialize(command, context = nil, inputs = {})
@command = command
@context = context
@inputs = inputs
@cache = {}
end
@@ -29,19 +29,28 @@
self.class.new(@command, @context, inputs)
end
def [](name, *args)
+ get(name, @context, *args)
+ end
+
+ def get(name, context, *args)
return @inputs[name] if @inputs.key?(name) && @inputs[name] != []
+
return @cache[name] if @cache.key? name
meta = @command.inputs[name]
return unless meta
val =
if meta[:default].respond_to? :to_proc
- @context.instance_exec(*args, &meta[:default])
+ unless context
+ raise "no context for input request"
+ end
+
+ context.instance_exec(*args, &meta[:default])
elsif meta[:default]
meta[:default]
elsif meta[:type] == :boolean
false
elsif meta[:argument] == :splat