Class: Hbtrack::AddCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/hbtrack/command/add_command.rb

Overview

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

Instance Method Summary collapse

Methods inherited from Command

#help, #local_store

Constructor Details

#initialize(store_path, options) ⇒ AddCommand

Returns a new instance of AddCommand.



10
11
12
# File 'lib/hbtrack/command/add_command.rb', line 10

def initialize(store_path, options)
  super(store_path, options)
end

Instance Method Details

#add_to_db(names, store) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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_parserObject



21
22
23
24
25
# 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

#executeObject



14
15
16
17
18
19
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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