require 'rails/generators' class EotbGenerator < Rails::Generators::Base desc "Creates eotb initializer in confing/initializers/eotb.rb, setting your app to work with Beholder." source_root File.expand_path('../../../../generators/eotb/templates', __FILE__) class_option :api_key, :aliases => "-k", :type => :string, :desc => "Your Beholder API key" def generator ensure_parameters_was_set generate_initializer unless api_key_configured? puts "Eotb is configured!" puts "File 'config/initializers/eotb.rb' was created!" puts "Start using eotb now!" end private def ensure_parameters_was_set if api_key_configured? puts "Configuration file already exists: config/initializers/eotb.rb" exit end end def api_key_exp "#{options[:api_key]}" end def generate_initializer template 'initializer.rb', 'config/initializers/eotb.rb' end def api_key_configured? if !options[:api_key] puts "Must pass --api-key='your_api_key' or -k 'your_api_key'" exit end File.exists?('config/initializers/eotb.rb') && options[:api_key].empty? end end