Class: BulletJournal::File

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

Class Method Summary collapse

Class Method Details

.add_task(text) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bullit/file.rb', line 60

def self.add_task(text)
  file = YAML.load(today_file.read)

  if file[:tasks] == {}
    file[:tasks] = [ Task.new(text: text).to_h ]
  else
    file[:tasks] = today_tasks << Task.new(text: text).to_h
  end

  today_file.write(YAML.dump(file))
end

.complete_task(task_number) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/bullit/file.rb', line 72

def self.complete_task(task_number)
  file = YAML.load(today_file.read)

  unless file[:tasks].nil?
    unless file[:tasks][task_number].nil?
      file[:tasks][task_number] = Task.new(file[:tasks][task_number].to_h).mark_as_complete
      today_file.write(YAML.dump(file))
    end
  end
end

.generate_today_file(loud) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bullit/file.rb', line 10

def self.generate_today_file(loud)
  today_file.dirname.mkpath unless today_file.dirname.exist?

  unless today_file.exist?
    t = Time.now

    title = "Journal for #{t.strftime('%m/%d/%y')}"

    file_contents = {
      title: title,
      tasks: incomplete_tasks_from_yesterday(loud)
    }

    today_file.write(YAML.dump(file_contents))
    
    puts "#{today_file.to_s} created." if loud
  end
end

.incomplete_tasks_from_yesterday(loud) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bullit/file.rb', line 29

def self.incomplete_tasks_from_yesterday(loud)
  if yesterday_file.exist?
    file = YAML.load(yesterday_file.read)
    
    return {} if file[:tasks].empty?
    
    file[:tasks].each do |t|
      unless t[:complete] == true
        action = TTY::Prompt.new.select("\nWould you like to complete\n\n\t'#{t[:text]}'\n\nor migrate it to today's entry?\n", %w(complete migrate))
        if action == 'complete'
          t[:complete] = true
        end
      end
    end

    file[:tasks] = file[:tasks].reject{ |t| t[:complete] == true }

    yesterday_file.write(YAML.dump(file))

    if file[:tasks] == {}
      return nil
    else
      return file[:tasks]
    end
  end
end

.today_fileObject



83
84
85
86
87
88
89
90
91
# File 'lib/bullit/file.rb', line 83

def self.today_file
  t = Time.now

  year    = t.strftime "%Y"
  week    = t.strftime "%W" 
  weekday = t.strftime "%u"

  Pathname("#{Dir.home}/.bullit/journal/#{year}/#{week}/#{weekday}.yaml")
end

.today_tasksObject



56
57
58
# File 'lib/bullit/file.rb', line 56

def self.today_tasks
  YAML.load(today_file.read)[:tasks]
end

.yesterday_fileObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bullit/file.rb', line 93

def self.yesterday_file
  t = Time.now

  year    = t.strftime "%Y"
  week    = t.strftime "%W" 
  weekday = t.strftime("%u").to_i

  # TODO: the logic here is a bit dodgy... it should look for the latest file in case you don't use your machine for more than one day.
  if weekday > 1
    Pathname("#{Dir.home}/.bullit/journal/#{year}/#{week}/#{(weekday - 1).to_s}.yaml")
  else
    Pathname("#{Dir.home}/.bullit/journal/#{year}/#{week-1}/7.yaml")
  end
end