class Hbtrack::AddCommand

AddCommand class is responsible for handling `hbtrack add` command in CLI

Public Class Methods

new(store_path, options) click to toggle source
Calls superclass method Hbtrack::Command::new
# File lib/hbtrack/command/add_command.rb, line 10
def initialize(store_path, options)
  super(store_path, options)
end

Public Instance Methods

add_to_db(names, store) click to toggle source
# File lib/hbtrack/command/add_command.rb, line 43
def add_to_db(names, store)
  order = store.get_habits_count
  names.each do |name|
    order = order + 1
    habit = Hbtrack::Database::Habit.new(name, order)
    store.add_habit(habit)
  end
  feedback(names, names)
end
create_option_parser() click to toggle source
# File lib/hbtrack/command/add_command.rb, line 21
def create_option_parser
  OptionParser.new do |opts|
    opts.banner = 'Usage: hbtrack add [<habit_name>]'
  end
end
execute() click to toggle source
Calls superclass method Hbtrack::Command#execute
# File lib/hbtrack/command/add_command.rb, line 14
def execute
  unless @names.empty?
    return add_to_db(@names, local_store)
  end
  super
end
feedback(names, added) click to toggle source
# File lib/hbtrack/command/add_command.rb, line 27
def feedback(names, added)
  output = []

  unless added.empty?
    output << Hbtrack::Util.green("Add #{added.join(',')}!")
    output << "\n"
    names -= added
  end

  unless names.empty?
    output << Hbtrack::Util.blue("#{names.join(',')} already existed!")
  end

  output.join
end