class CubaGenie::MyCLI

Public Instance Methods

new(project_name) click to toggle source
# File lib/cuba_genie.rb, line 21
def new(project_name)
  @cmds = CommandList.new
  puts "your project name is '#{project_name}'"
  minitest = want_minitest?
  reporter = (minitest ? choose_reporter : 'n')
  capybara = (minitest ? want_capybara? : false)
  @cmds.add_command CubaSetup.new(project_name: project_name, minitest: minitest, capybara: capybara)
  @cmds.add_command ViewsSetup.new(project_name: project_name, bootstrap_version: choose_bootstrap_version)
  @cmds.add_command MinitestSetup.new(project_name: project_name, reporter: reporter, capybara_setup: capybara) if minitest
  
  # allow users to add their own commands
  run_extensions

  puts closing_message(project_name) if @cmds.execute
end

Private Instance Methods

ask_for_numeric_reponse(valid_range) click to toggle source

Prompts the user for a numeric input, within a valid range

@param valid_range [String] captured STDIN

@return [Fixnum] an integer within the valid range

@note character 0 cannot be used as a range value. The reason is that to_i will convert any non-numberic character to 0, which the method will then wrongly interpret as a valid character

# File lib/cuba_genie.rb, line 102
def ask_for_numeric_reponse(valid_range)
  response = STDIN.gets.chomp.to_i
  while !valid_range.cover? response
    puts "Enter a number between #{valid_range.first}-#{valid_range.last}"
    response = STDIN.gets.chomp.to_i
  end
  puts ""
  response
end
ask_for_yes_no_response() click to toggle source

Prompts the user for a boolean input, within a range of potential semantically true or false values (POSITIVE_RESPONSES, NEGATIVE_RESPONSES)

@param N/A

@return [Boolean] a true or false value

# File lib/cuba_genie.rb, line 81
def ask_for_yes_no_response
  response = STDIN.gets.chomp
  while !(POSITIVE_RESPONSES + NEGATIVE_RESPONSES).include? response
    puts "Enter y or n"
    response = STDIN.gets.chomp
  end
  puts ""
  POSITIVE_RESPONSES.include? response
end
choose_bootstrap_version() click to toggle source
# File lib/cuba_genie.rb, line 59
def choose_bootstrap_version
  puts "Choose which version of Twitter Bootstrap you want to use:"
  (["Don't install Bootstrap"] + ALLOWED_BOOTSTRAP_VERSIONS).each_with_index do |opt, idx|
    puts "#{idx+1}: #{opt}"
  end
  response = ask_for_numeric_reponse (1..5)
  puts ""

  (response.to_i == 1) ? 'n' : ALLOWED_BOOTSTRAP_VERSIONS[response.to_i - 2]
end
choose_reporter() click to toggle source
# File lib/cuba_genie.rb, line 48
def choose_reporter
  puts "Choose a Minitest output format (reporter):"
  MINITEST_REPORTERS.each_with_index do |reporter, idx|
    puts "#{idx+1}: #{reporter.keys.first.to_s.extend(ContentHelpers).titlecase} (#{reporter.values.first})"
  end
  response = ask_for_numeric_reponse (1..8)
  puts ""

  MINITEST_REPORTERS[response.to_i - 1].keys.first.to_s.extend(ContentHelpers).titlecase
end
closing_message(project_name) click to toggle source
# File lib/cuba_genie.rb, line 132
def closing_message(project_name)
  "\nYour #{project_name} Cuba app is now set up and ready to go! To start the app, cd "\
    "into the #{project_name} directory, run 'bundle install' and then 'rackup'. You can then "\
    "see your app running on 'http://localhost:9292'."
end
get_extensions() click to toggle source

Gets all Command-derived classes defined in the CubaGenie module and filters out the classes already defined by the core CubaGenie gem. What is left are the classes added as user-extensions to the gem, and which will be run after the built-in commands have ran.

@param N/A

@return [Array<Symbol>] all user-added Command-derived classes

# File lib/cuba_genie.rb, line 124
def get_extensions
  existing_klasses = [:CubaSetup, :MinitestSetup, :ViewsSetup]
  all_command_klasses = CubaGenie.constants.select do |c|
    Class === CubaGenie.const_get(c) && CubaGenie.const_get(c).superclass == CubaGenie::Command
  end
  all_command_klasses - existing_klasses
end
run_extensions() click to toggle source

Gets all user-defined Command-derived classes within the CubaGenie module, order them according to their PRECEDENCE constant and adds them to the command-list so that they can be executed after the built-in CubaGenie classes

@param N/A

@return [Array<Symbol>] all user-added Command-derived classes

# File lib/cuba_genie.rb, line 149
def run_extensions
  # first check if someone added extension classes in our module
  command_classes = get_extensions
  unless command_classes.empty?
    #order classes in order of precedence
    command_classes.sort_by! do |item|
      CubaGenie.const_get(item).const_get(:PRECEDENCE)
    end

    command_classes.each do |klass|
      if CubaGenie.const_get(klass).instance_methods.include? :execute
        @cmds.add_command CubaGenie.const_get(klass).new
      else
        # #execute method is not implemented
        ## TODO: raise exception?
      end
    end
  end
end
want_capybara?() click to toggle source
# File lib/cuba_genie.rb, line 43
def want_capybara?
  puts "Would you like to create acceptance (Capybara) tests for your project?"
  ask_for_yes_no_response
end
want_minitest?() click to toggle source
# File lib/cuba_genie.rb, line 38
def want_minitest?
  puts "Would you like to setup Minitest for your project?"
  ask_for_yes_no_response
end