Class: Quirk::Habit

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, days) ⇒ Habit



67
68
69
70
# File 'lib/quirk.rb', line 67

def initialize(id, days)
  @id, @days, @marks = id, days, []
  raise "No days found for #{id}" if @days.empty?
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



65
66
67
# File 'lib/quirk.rb', line 65

def days
  @days
end

#idObject (readonly)

Returns the value of attribute id.



65
66
67
# File 'lib/quirk.rb', line 65

def id
  @id
end

#marksObject (readonly)

Returns the value of attribute marks.



65
66
67
# File 'lib/quirk.rb', line 65

def marks
  @marks
end

Class Method Details

.parse(line) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/quirk.rb', line 130

def self.parse(line)
  line.strip!
  line.gsub!(/\s+/, ' ')

  id, days = line.split(':', 2).map(&:strip)
  self.new(id, parse_days(days))
end

.parse_days(text) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/quirk.rb', line 138

def self.parse_days(text)
  originals = text.split(',').map(&:strip)
  days = []
  if originals.include?('everyday')
    (0..6).to_a
  else
    %w(sunday monday tuesday wednesday
       thursday friday saturday).each_with_index do |day, index|
      days << (index) if originals.include?(day)
    end
    days
  end
end

Instance Method Details

#color_on(date) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/quirk.rb', line 94

def color_on(date)
  hit_color = :light_green
  miss_color = :light_red
  last_date = [@last_date, Quirk.today].compact.min
  if first_date.nil? ||
     date < first_date ||
     date > last_date ||
     !days.include?(date.wday)
    :white
  elsif @marks.include?(date)
    hit_color
  else
    date == last_date ? :white : miss_color
  end
end

#first_dateObject



85
86
87
# File 'lib/quirk.rb', line 85

def first_date
  @first_date || @marks.first
end

#mark!(date) ⇒ Object



72
73
74
75
# File 'lib/quirk.rb', line 72

def mark!(date)
  @marks << date
  @marks.sort
end

#mark_first!(date) ⇒ Object



77
78
79
# File 'lib/quirk.rb', line 77

def mark_first!(date)
  @first_date = date
end

#mark_last!(date) ⇒ Object



81
82
83
# File 'lib/quirk.rb', line 81

def mark_last!(date)
  @last_date = date
end

#pending?(date) ⇒ Boolean



89
90
91
92
# File 'lib/quirk.rb', line 89

def pending?(date)
  weekday = date.strftime("%w").to_i
  days.include?(weekday) && color_on(date) != :light_green
end

#streakObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/quirk.rb', line 110

def streak
  return 0 if first_date.nil?

  count = 0
  deltas = {:light_red => -1, :light_green => 1, :white => 0}
  date = Quirk.today
  while date >= first_date && (color = color_on(date)) == :white
    date -= 1
  end

  init_color = color
  while date >= first_date
    color = color_on(date)
    break if color != init_color && days.include?(date.wday)
    date -= 1
    count += deltas[color]
  end
  count
end