Class: Quirk::App

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

Instance Method Summary collapse

Constructor Details

#initialize(quirkfile = QUIRKFILE) ⇒ App

Returns a new instance of App.



18
19
20
# File 'lib/quirk.rb', line 18

def initialize(quirkfile = QUIRKFILE)
  @quirkfile = quirkfile
end

Instance Method Details

#calendar(habit_id) ⇒ Object



22
23
24
# File 'lib/quirk.rb', line 22

def calendar(habit_id)
  puts cal.output(habit_id)
end

#editObject



26
27
28
# File 'lib/quirk.rb', line 26

def edit
  `#{EDITOR} #{QUIRKFILE} < \`tty\` > \`tty\``
end

#mark(habit_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quirk.rb', line 30

def mark(habit_id)
  contents = File.read(@quirkfile)
  return if !cal.has_habit?(habit_id)

  date = Quirk.today.strftime('%Y/%m/%d')
  match = contents.match(/^(#{date} .*)$/)
  if match # already has date, add habit to line
    old_line = match.captures[0]
    ids = old_line.strip.split(/\s+/, 2)[1].split(',').map(&:strip)
    ids << habit_id
    ids = ids.sort.uniq
    new_line = "#{date} #{ids.join(', ')}"
    contents.sub!(old_line, new_line)
    File.open(@quirkfile, 'w') { |f| f.print(contents) }
  else
    File.open(@quirkfile, 'a') { |f| f.puts("#{date} #{habit_id}") }
  end
end

#streaksObject



49
50
51
# File 'lib/quirk.rb', line 49

def streaks
  puts cal.streaks
end

#year=(year) ⇒ Object



53
54
55
56
# File 'lib/quirk.rb', line 53

def year=(year)
  raise "Invalid year: #{year}" if year !~ /\d\d\d\d/
  cal.year = year.to_i
end