From ed4a32518fe323d6083803237628c1a709b2b3a4 Mon Sep 17 00:00:00 2001 From: Chris Lowder Date: Mon, 16 Jan 2012 11:56:55 +0000 Subject: [PATCH] Removing the hard coding of export formats allowing the user to 'plug-in' their own export format. --- lib/foreman/cli.rb | 16 ++++++++------ lib/foreman/helpers.rb | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 lib/foreman/helpers.rb diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 45f01aa..8241314 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -1,10 +1,14 @@ require "foreman" +require "foreman/helpers" require "foreman/engine" require "foreman/export" require "thor" require "yaml" class Foreman::CLI < Thor + include Foreman::Helpers + + class_option :procfile, :type => :string, :aliases => "-f", :desc => "Default: Procfile" desc "start", "Start the application" @@ -42,16 +46,14 @@ class Foreman::CLI < Thor def export(format, location=nil) check_procfile! - formatter = case format - when "inittab" then Foreman::Export::Inittab - when "upstart" then Foreman::Export::Upstart - when "bluepill" then Foreman::Export::Bluepill - when "runit" then Foreman::Export::Runit - else error "Unknown export format: #{format}." - end + classy_format = classify(format) + formatter = constantize("Foreman::Export::#{ classy_format }") formatter.new(engine).export(location, options) + rescue NameError => ex + error "Unknown export format: #{format}." + raise ex rescue Foreman::Export::Exception => ex error ex.message end diff --git a/lib/foreman/helpers.rb b/lib/foreman/helpers.rb new file mode 100644 index 0000000..4b5625d --- /dev/null +++ b/lib/foreman/helpers.rb @@ -0,0 +1,50 @@ +module Foreman::Helpers + # Copied whole sale from, https://github.com/defunkt/resque/ + + # Given a word with dashes, returns a camel cased version of it. + # + # classify('job-name') # => 'JobName' + def classify(dashed_word) + dashed_word.split('-').each { |part| part[0] = part[0].chr.upcase }.join + end + + # Tries to find a constant with the name specified in the argument string: + # + # constantize("Module") # => Module + # constantize("Test::Unit") # => Test::Unit + # + # The name is assumed to be the one of a top-level constant, no matter + # whether it starts with "::" or not. No lexical context is taken into + # account: + # + # C = 'outside' + # module M + # C = 'inside' + # C # => 'inside' + # constantize("C") # => 'outside', same as ::C + # end + # + # NameError is raised when the constant is unknown. + def constantize(camel_cased_word) + camel_cased_word = camel_cased_word.to_s + + if camel_cased_word.include?('-') + camel_cased_word = classify(camel_cased_word) + end + + names = camel_cased_word.split('::') + names.shift if names.empty? || names.first.empty? + + constant = Object + names.each do |name| + args = Module.method(:const_get).arity != 1 ? [false] : [] + + if constant.const_defined?(name, *args) + constant = constant.const_get(name) + else + constant = constant.const_missing(name) + end + end + constant + end +end \ No newline at end of file