class Belajar::Terminal::Setup

Public Instance Methods

init() click to toggle source
# File lib/belajar/terminal/setup.rb, line 10
def init
  empty_line
  say 'Please type the base path in which you want to save your belajar'
  say 'files. The "courses" folder and "solutions" folder will be created'
  say 'automatically.'

  loop do
    path = get 'path:'

    begin
      @belajar_path = File.expand_path(path.to_s, Dir.pwd)
    rescue
      say_warning "#{path} is no valid path name. Try another!"
      next
    end

    say_warning 'Do you want to use the following path as your belajar base path?'
    say "\"#{@belajar_path}\""

    confirmation = get '(yes|no)'
    break if confirmation.casecmp('yes').zero?

    empty_line
    say 'No Problem. Just type another one!'
  end

  prepare_directories(@belajar_path)
end
list() click to toggle source
# File lib/belajar/terminal/setup.rb, line 40
def list
  say_info "Your current belajar setup is:\n\n#{Belajar.config.summary}"
end
set() click to toggle source
# File lib/belajar/terminal/setup.rb, line 57
def set
  courses_path = options[:paths] || options[:courses_path]
  solutions_path = options[:paths] || options[:solutions_path]

  if courses_path.nil? && solutions_path.nil?
    say_warning 'Please specify options when using this command!'
    say `belajar setup help set`
    return
  end

  update_config(:courses_path, courses_path) if courses_path
  update_config(:solutions_path, solutions_path) if solutions_path

  Belajar.config.save
  list
end

Private Instance Methods

prepare_directories(path) click to toggle source
# File lib/belajar/terminal/setup.rb, line 76
def prepare_directories(path)
  courses_dir  = Belajar::Configuration::COURSES_DIR
  courses_path = File.join(path, courses_dir)

  Belajar.config.courses_path = courses_path

  solutions_dir  = Belajar::Configuration::SOLUTIONS_DIR
  solutions_path = File.join(path, solutions_dir)

  if Dir.exist? solutions_path
    Belajar.config.solutions_path = solutions_path
  end

  generator = Belajar::Generator.new
  generator.prepare

  text = [
    "Your Belajar directory is now set up.\n",
    'Belajar created/updated following two paths for you:',
    courses_path,
    solutions_path
  ]

  say_info text.join("\n")
end
update_config(attribute, value) click to toggle source
# File lib/belajar/terminal/setup.rb, line 102
def update_config(attribute, value)
  path = File.expand_path(value, Dir.pwd)
  Belajar.config.send("#{attribute}=", path)
rescue StandardError => e
  say_warning e.message
end