Class: Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/habittracker/archive.rb

Instance Method Summary collapse

Constructor Details

#initializeArchive

Returns a new instance of Archive.



3
4
5
6
# File 'lib/habittracker/archive.rb', line 3

def initialize
  @db_file = File.join(ENV['HOME'], '/.ht')
  load_archive
end

Instance Method Details

#add_habit(habit) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/habittracker/archive.rb', line 8

def add_habit(habit)
  from = Date.new(Time.now.year, 1, 1)
  to = Date.new(Time.now.year, 12, 31)

  @archive[habit] = from.upto(to).each_with_object({}) do |day, mem|
    mem[date_to_key(day)] = false
    mem
  end
  store
  load_archive
end

#habitsObject



26
27
28
# File 'lib/habittracker/archive.rb', line 26

def habits
  @archive.keys
end


30
31
32
# File 'lib/habittracker/archive.rb', line 30

def print_habits
  habits.each { |h| puts h }
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/habittracker/archive.rb', line 34

def print_status(from, to)
  keys_to_print = from.upto(to).map { |day| date_to_key(day) }

  rows = keys_to_print.map do |day|
    row = [short_key(day)]

    habits.each do |habit|
      hits = @archive[habit]
      row << row_item(hits[day])
    end
    row
  end

  header = habits.unshift('Day')
  puts Terminal::Table.new title: Time.now.strftime('%B'), headings: header, rows: rows
end

#rm_habit(habit) ⇒ Object



20
21
22
23
24
# File 'lib/habittracker/archive.rb', line 20

def rm_habit(habit)
  @archive.delete(habit)
  store
  load_archive
end

#save(habit, date) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/habittracker/archive.rb', line 51

def save(habit, date)
  calendar = @archive[habit]
  return if calendar.nil?

  key = date_to_key(date)
  calendar[key] = true
  store
  load_archive
end