lib/ignoramos.rb in ignoramos-1.0.1 vs lib/ignoramos.rb in ignoramos-1.1.0

- old
+ new

@@ -1,29 +1,30 @@ -require 'commands/new_command' -require 'commands/build_command' -require 'commands/tweet_command' - class Ignoramos def initialize(args = []) command(args).execute end - def command(args) - cmd = args[0] unless args.empty? - - if cmd == 'new' - NewCommand.new(args[1]) - elsif cmd == 'build' - BuildCommand.new - elsif cmd == 'tweet' - TweetCommand.new(args[1]) - else - NilCommand.new - end - end - NilCommand = Struct.new(:args) do def execute puts 'command not supported' end + end + + private + def command(args) + cmd = args.slice!(0) unless args.empty? + Object.const_get(classify(cmd)).new(*args) + rescue NameError + NilCommand.new + end + + + def classify(command) + snake_class = "#{command}_command" + begin + require "commands/#{snake_class}" + rescue LoadError => e + puts e.to_s + end + snake_class.split('_').collect(&:capitalize).join end end