hbtrack 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'hbtrack/util'
4
-
5
- module Hbtrack
6
- # This class contains the methods that
7
- # are used to format the progress of a Habit
8
- # into string
9
- class HabitPrinter
10
- attr_accessor :formatter
11
-
12
- def initialize(formatter = CompleteSF.new)
13
- @formatter = formatter
14
- end
15
-
16
- def calculate_space_needed_for(habit, key)
17
- habit.longest_month.length - Util.get_month_from(key).length
18
- end
19
-
20
- def print_latest_progress(habit, no_of_space = 0)
21
- habit.name.to_s + ' ' * no_of_space + ' : ' +
22
- print_progress(
23
- habit.latest_progress,
24
- habit.latest_stat
25
- )
26
- end
27
-
28
- def print_all_progress(habit)
29
- habit.progress.map do |key, value|
30
- space = calculate_space_needed_for(habit, key)
31
- Util.convert_key_to_date(key, space) +
32
- print_progress(
33
- value,
34
- habit.stat_for_progress(key)
35
- )
36
- end.join("\n")
37
- end
38
-
39
- def print_progress(progress, stat)
40
- output = progress.split('').map do |x|
41
- if x == '0'
42
- Util.red('*')
43
- elsif x == '1'
44
- Util.green('*')
45
- else
46
- ' '
47
- end
48
- end.join('')
49
- output + ' ' * (32 - progress.size) +
50
- @formatter.format(stat)
51
- end
52
- end
53
- end
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'hbtrack/util'
4
-
5
- module Hbtrack
6
- # This class contains the methods to
7
- # handle the operation of mutliple habits
8
- class HabitTracker
9
- attr_accessor :habits
10
- attr_reader :hp, :output_file_name
11
-
12
- def initialize(file = FILE_NAME,
13
- output = FILE_NAME)
14
- @habits = []
15
- @file_name = file
16
- @output_file_name = output
17
- initialize_habits_from_file
18
- end
19
-
20
- def initialize_habits_from_file
21
- return unless File.exist?(@file_name)
22
- input = File.read(@file_name).split(/\n\n/)
23
- input.each { |string| @habits << Habit.initialize_from_string(string) }
24
- end
25
-
26
- # This methods find a habit based on the name given.
27
- # Blocks are executed when habit is not found.
28
- def find(habit_name)
29
- habit = @habits.find do |h|
30
- h.name == habit_name
31
- end
32
- yield if habit.nil? && block_given?
33
- habit
34
- end
35
-
36
- def find_or_create(habit_name)
37
- habit = find(habit_name) do
38
- habit = create(habit_name)
39
- return habit
40
- end
41
- end
42
-
43
- def longest_name
44
- @habits.max_by(&:name_length).name
45
- end
46
-
47
- def total_habits_stat
48
- @habits.each_with_object(done: 0, undone: 0) do |habit, stat|
49
- stat[:done] += habit.latest_stat[:done]
50
- stat[:undone] += habit.latest_stat[:undone]
51
- end
52
- end
53
-
54
- def done_count_for(date:)
55
- count_for(date, '1')
56
- end
57
-
58
- def undone_count_for(date:)
59
- count_for(date, '0')
60
- end
61
-
62
- def overall_stat_description(formatter)
63
- Util.title('Total') + formatter.format(total_habits_stat)
64
- end
65
-
66
- def create(habit_name)
67
- unless invalid_habit_name?(habit_name)
68
- habit = Habit.new(habit_name)
69
- @habits << habit
70
- return habit
71
- end
72
-
73
- return ErrorHandler.raise_habit_name_too_long if habit_name && habit_name.length > 11
74
-
75
- ErrorHandler.raise_invalid_arguments
76
- end
77
-
78
- def invalid_habit_name?(habit_name)
79
- habit_name.nil? || habit_name =~ /\s+/ ||
80
- habit_name.length > 11
81
- end
82
-
83
- private def count_for(d, value)
84
- habits.reduce(0) do |a, habit|
85
- val = habit.done_for(date: d) == value ? 1 : 0
86
- a + val
87
- end
88
- end
89
-
90
- end
91
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Hbtrack
4
- class Store
5
- def initialize(habits, file_name)
6
- @habits = habits
7
- @file_name = file_name
8
- end
9
-
10
- def save
11
- File.open(@file_name, 'w') do |f|
12
- @habits.each do |habit|
13
- f.puts habit
14
- end
15
- end
16
- end
17
- end
18
- end