class Hbtrack::UpdateCommand
Public Class Methods
new(store_path, options, is_done)
click to toggle source
Calls superclass method
Hbtrack::Command::new
# 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
Public Instance Methods
add_or_update_entry(store, id, day, is_done)
click to toggle source
# 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_parser()
click to toggle source
# 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)
click to toggle source
Check if the entry timestamp are within the same day
# 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
execute()
click to toggle source
Calls superclass method
Hbtrack::Command#execute
# 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)
click to toggle source
Extract out the year, month and day of a Date or Time object.
# 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)
click to toggle source
# 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)
click to toggle source
# 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