lib/ing.rb in ing-0.1.5 vs lib/ing.rb in ing-0.2.1
- old
+ new
@@ -1,81 +1,103 @@
['ing/version',
+ 'ing/util',
'ing/lib_trollop',
'ing/trollop/parser',
- 'ing/util',
- 'ing/dispatcher',
+ 'ing/option_parsers/trollop',
'ing/shell',
'ing/common_options',
- 'ing/boot',
'ing/files',
'ing/task',
'ing/generator',
+ 'ing/command',
+ 'ing/commands/boot',
'ing/commands/implicit',
+ 'ing/commands/generate',
'ing/commands/list',
- 'ing/commands/help',
- 'ing/commands/generate'
+ 'ing/commands/help'
].each do |f|
require_relative f
end
module Ing
extend self
-
+
Error = Class.new(StandardError)
FileNotFoundError = Class.new(Error)
attr_writer :shell_class
def shell_class
@shell_class ||= Shell::Basic
end
- def implicit_booter
- ["Implicit"]
+
+ class << (Callstack = Object.new)
+
+ def index(klass, meth)
+ stack.index {|e| e == [klass,meth]}
+ end
+
+ def push(klass, meth)
+ stack << [klass, meth]
+ end
+
+ def clear
+ stack.clear
+ end
+
+ def to_a
+ stack.dup
+ end
+
+ private
+ def stack
+ @stack ||= []
+ end
+
end
- # Dispatch command line to boot class (if specified, or Implicit otherwise),
- # which in turn dispatches the command after parsing args.
- #
- # Note boot dispatch happens within +Ing::Commands+ namespace.
- #
- def run(argv=ARGV)
- booter = extract_boot_class!(argv) || implicit_booter
- run_boot booter, "call", *argv
+ def run(args=ARGV)
+ booter = extract_boot_class!(args) || implicit_booter
+ execute booter, *args
end
- # Dispatch to the command via +Ing::Boot#call_invoke+
- # Use this when you want to invoke a command from another command, but only
- # if it hasn't been run yet. For example,
- #
- # invoke Some::Task, :some_instance_method, some_argument, :some_option => true
- #
- # You can skip the method and it will assume +#call+ :
- #
- # invoke Some::Task, :some_option => true
- def invoke(klass, *args)
- run_boot implicit_booter, "call_invoke", klass, *args
+ def execute(klass, meth=:call, *args, &config)
+ cmd = command.new(klass, meth, *args)
+ _callstack.push(cmd.command_class, cmd.command_meth)
+ cmd.execute(&config)
end
- # Dispatch to the command via +Ing::Boot#call_execute+
- # Use this when you want to execute a command from another command, and you
- # don't care if it has been run yet or not. See equivalent examples for
- # +invoke+.
- #
- def execute(klass, *args)
- run_boot implicit_booter, "call_execute", klass, *args
+ def invoke(klass, meth=:call, *args, &config)
+ execute(klass, meth, *args, &config) unless executed?(klass, meth)
end
+ def executed?(klass, meth)
+ !!_callstack.index(klass, meth)
+ end
+
+ def callstack
+ _callstack.to_a
+ end
+
private
- def run_boot(booter, *args)
- Dispatcher.new(["Ing","Commands"], booter, *args).dispatch
+ def command
+ Command
end
+ def _callstack
+ Callstack
+ end
+
+ def implicit_booter
+ Commands::Implicit
+ end
+
def extract_boot_class!(args)
- c = Util.to_class_names(args.first)
- if (Commands.const_defined?(c.first, false) rescue nil)
- args.shift; c
- end
+ c = Util.decode_class(args.first, Ing::Commands)
+ args.shift; c
+ rescue NameError
+ nil
end
-
-end
+
+end
\ No newline at end of file