Class: Ronin::Store
- Inherits:
-
Object
- Object
- Ronin::Store
- Defined in:
- lib/ronin/store.rb
Constant Summary collapse
- DB_DIR =
File.join(Dir.home, '.config', 'ronin')
- DB_PATH =
File.join(DB_DIR, 'sessions.db')
Instance Method Summary collapse
- #history(days = 7) ⇒ Object
-
#initialize ⇒ Store
constructor
A new instance of Store.
- #record(duration_min, completed) ⇒ Object
- #streak ⇒ Object
- #today ⇒ Object
- #total_completed ⇒ Object
- #total_minutes ⇒ Object
Constructor Details
Instance Method Details
#history(days = 7) ⇒ Object
58 59 60 61 |
# File 'lib/ronin/store.rb', line 58 def history(days = 7) cutoff = (Time.now - (days * 86_400)).strftime('%Y-%m-%d') all_sessions.select { |s| s[:date] >= cutoff } end |
#record(duration_min, completed) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ronin/store.rb', line 17 def record(duration_min, completed) @pstore.transaction do sessions = @pstore['sessions'] || [] sessions << { date: Time.now.strftime('%Y-%m-%d'), started_at: Time.now.iso8601, duration: duration_min, completed: completed } @pstore['sessions'] = sessions end end |
#streak ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ronin/store.rb', line 35 def streak dates = all_sessions .select { |s| s[:completed] } .map { |s| s[:date] } .uniq .sort .reverse return 0 if dates.empty? today_str = Time.now.strftime('%Y-%m-%d') # Streak must include today or yesterday return 0 unless dates.first == today_str || dates.first == prev_date(today_str) count = 1 (0...dates.length - 1).each do |i| break unless prev_date(dates[i]) == dates[i + 1] count += 1 end count end |
#today ⇒ Object
30 31 32 33 |
# File 'lib/ronin/store.rb', line 30 def today date = Time.now.strftime('%Y-%m-%d') all_sessions.select { |s| s[:date] == date } end |
#total_completed ⇒ Object
63 64 65 |
# File 'lib/ronin/store.rb', line 63 def total_completed all_sessions.count { |s| s[:completed] } end |
#total_minutes ⇒ Object
67 68 69 |
# File 'lib/ronin/store.rb', line 67 def total_minutes all_sessions.select { |s| s[:completed] }.sum { |s| s[:duration] } end |