Class: Hbtrack::UpdateCommand

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

Instance Method Summary collapse

Methods inherited from Command

#help, #local_store

Constructor Details

#initialize(store_path, options, is_done) ⇒ UpdateCommand

Returns a new instance of UpdateCommand.



8
9
10
11
12
13
# File 'lib/hbtrack/command/update_command.rb', line 8

def initialize(store_path, options, is_done)
  @day = DateTime.now
  @is_done = is_done
  @db = false
  super(store_path, options)
end

Instance Method Details

#add_or_update_entry(store, id, day, is_done) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/hbtrack/command/update_command.rb', line 59

def add_or_update_entry(store, id, day, is_done)
  entry = store.get_latest_entry_of(id)
  type = is_done ? 'completed' : 'missed'
  unless entry_exist?(entry, day)
    entry = Hbtrack::Database::Entry.new(DateTime.now, type)
    store.add_entry_of(id, entry)
  else
    store.update_entry_of(id, day, type)
  end
end

#create_option_parserObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hbtrack/command/update_command.rb', line 21

def create_option_parser
  action = @is_done ? 'Done' : 'Undone'
  OptionParser.new do |opts|
    opts.banner = "Usage: hbtrack #{action.downcase} [<habit_name>] [options]"
    opts.separator ''
    opts.separator 'Options:'

    opts.on('-a', '--all', "#{action} all habits") do
      @all = true
    end

    opts.on('--day DAY', Integer, "#{action} habit(s) for specific day") do |day|
      @day = Date.new(Date.today.year, Date.today.month, day.to_i)
    end

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit
    end
  end
end

#entry_exist?(entry, day) ⇒ Boolean

Check if the entry timestamp are within the same day

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/hbtrack/command/update_command.rb', line 72

def entry_exist?(entry, day)
  return false unless entry
  year, month , day = extract_date(day)
  time = entry[:timestamp]
  y, m, d = extract_date(time)
  return y == year && m == month && d == day
end

#executeObject



15
16
17
18
19
# File 'lib/hbtrack/command/update_command.rb', line 15

def execute
  return update_all_in_db(local_store, @day, @is_done) if @all
  return update_in_db(local_store, @names, @day, @is_done)
  super
end

#extract_date(day) ⇒ Object

Extract out the year, month and day of a Date or Time object.



82
83
84
# File 'lib/hbtrack/command/update_command.rb', line 82

def extract_date(day)
  [day.year, day.month, day.day]
end

#update_all_in_db(store, day, is_done) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/hbtrack/command/update_command.rb', line 51

def update_all_in_db(store, day, is_done)
  habits = store.get_all_habits
  habits.each do |h|
    add_or_update_entry(store, h[:id], day, is_done)
  end
  Hbtrack::Util.green("Update successfully!")
end

#update_in_db(store, name, day, is_done) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/hbtrack/command/update_command.rb', line 43

def update_in_db(store, name, day, is_done)
  id = store.get_habit_id_for(name)
  return ErrorHandler.raise_habit_not_found(name) unless id

  add_or_update_entry(store, id, day, is_done)
  Hbtrack::Util.green("Update successfully!")
end