Class: Quirk::Calendar

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(habits) ⇒ Calendar

Returns a new instance of Calendar.



157
158
159
160
# File 'lib/quirk.rb', line 157

def initialize(habits)
  @habits = habits.reduce({}) {|hash,habit| hash[habit.id] = habit; hash}
  @ids = habits.map(&:id)
end

Instance Attribute Details

#habitsObject (readonly)

Returns the value of attribute habits.



155
156
157
# File 'lib/quirk.rb', line 155

def habits
  @habits
end

#yearObject



162
163
164
# File 'lib/quirk.rb', line 162

def year
  @year || Quirk.today.year
end

Class Method Details

.parse(text) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/quirk.rb', line 238

def self.parse(text)
  marks, habits = [], []
  text.strip.each_line do |line|
    line = line.split(';')[0].to_s.strip # remove comments
    if line =~ /^\d\d\d\d\/\d\d?\/\d\d?\s+/
      marks << line
    elsif line !~ /^\s*$/
      habits << Habit.parse(line)
    end
  end
  cal = Calendar.new(habits)
  marks.each { |mark| cal.mark!(mark) }
  cal
end

Instance Method Details

#has_habit?(habit_id) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/quirk.rb', line 166

def has_habit?(habit_id)
  raise "No habit found: #{habit_id}" if !@habits.has_key?(habit_id)
  true
end

#mark!(line) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/quirk.rb', line 210

def mark!(line)
  date = Date.parse(line.strip.split(/\s+/)[0])
  originals = line.strip.split(/\s+/, 2)[1].split(',').map(&:strip)
  originals.each do |original|
    if original =~ /^\^\s*/
      original = original.sub(/^\^\s*/, '')
      habit = @habits[original] if has_habit?(original)
      habit.mark_first!(date)
    elsif original =~ /^\$\s*/
      original = original.sub(/^\$\s*/, '')
      habit = @habits[original] if has_habit?(original)
      habit.mark_last!(date)
    elsif has_habit?(original)
      @habits[original].mark!(date)
    end
  end
end

#output(habit_id) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/quirk.rb', line 171

def output(habit_id)
  return if !has_habit?(habit_id)
  habit = @habits[habit_id]
  months = (1..12).map do |month|
    first = Date.new(year, month, 1)
    out = "#{first.strftime('         %b        ')}\n"
    out += "Su Mo Tu We Th Fr Sa\n"
    out += ("   " * first.wday)
    while first.month == month
      out += (first..(first + (6 - first.wday))).map do |date|
        if date.year != year || date.month != month
          '  '
        else
          len = date.day.to_s.length
          "#{' ' * (2 - len)}#{date.day.to_s.colorize(habit.color_on(date))}"
        end
      end.join(" ")
      out += "\n"
      first += 7 - first.wday
    end
    out.split("\n")
  end
  out = "#{(' ' * 32)}#{year}\n"
  index = 0
  while index < 12
    line = 0
    max_line = [months[index], months[index+1], months[index+2]].
      map(&:length).max
    while line <= max_line
      out += "#{months[index][line] || (' ' * 20)}   "
      out += "#{months[index+1][line] || ( ' ' * 20)}   "
      out += "#{months[index+2][line]}\n"
      line += 1
    end
    index += 3
  end
  out
end

#streaksObject



228
229
230
231
232
233
234
235
236
# File 'lib/quirk.rb', line 228

def streaks
  pairs = @ids.map do |id|
    [@habits[id].streak, id]
  end
  len = pairs.map {|p| p.first.to_s.length}.max
  pairs.map do |count, id|
    "#{' ' * (len - count.to_s.length)}#{count} #{id}"
  end.join("\n")
end