Class: Archive
- Inherits:
-
Object
- Object
- Archive
- Defined in:
- lib/habittracker/archive.rb
Instance Method Summary collapse
- #add_habit(habit, days = []) ⇒ Object
- #habits ⇒ Object
- #habits_per_day(day) ⇒ Object
-
#initialize ⇒ Archive
constructor
A new instance of Archive.
- #print_habits ⇒ Object
- #print_status(from, to) ⇒ Object
- #rm_habit(habit) ⇒ Object
- #save(habit, date) ⇒ Object
Constructor Details
#initialize ⇒ Archive
Returns a new instance of Archive.
2 3 4 5 |
# File 'lib/habittracker/archive.rb', line 2 def initialize @db_file = File.join(ENV['HOME'], '/.ht') load_archive end |
Instance Method Details
#add_habit(habit, days = []) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/habittracker/archive.rb', line 7 def add_habit(habit, days = []) 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 @archive[habit][:schedule] = days store load_archive end |
#habits ⇒ Object
27 28 29 |
# File 'lib/habittracker/archive.rb', line 27 def habits @archive.keys end |
#habits_per_day(day) ⇒ Object
62 63 64 65 66 |
# File 'lib/habittracker/archive.rb', line 62 def habits_per_day(day) @archive.keys.select do |habit| @archive[habit][:schedule].include?(day) end end |
#print_habits ⇒ Object
31 32 33 |
# File 'lib/habittracker/archive.rb', line 31 def print_habits habits.each { |h| puts h } end |
#print_status(from, to) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/habittracker/archive.rb', line 35 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
21 22 23 24 25 |
# File 'lib/habittracker/archive.rb', line 21 def rm_habit(habit) @archive.delete(habit) store load_archive end |
#save(habit, date) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/habittracker/archive.rb', line 52 def save(habit, date) calendar = @archive[habit] return if calendar.nil? key = date_to_key(date) calendar[key] = true store load_archive end |