Class: Generator

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

Overview

This is the main class that interfaces with Thor’s methods and does all the heavy lifting for Nikki. It’s a bit of a “God” object. Sorries.

Author:

  • Brandon Pittman

Constant Summary collapse

NIKKI_FILE =
"#{ENV['HOME']}/.config/nikki/nikki.yaml".freeze

Data entry collapse

Instance Method Summary collapse

Instance Method Details

#export(export_year) ⇒ Object

Parameters:

  • year (String)

    of journal entries you wish to export



65
66
67
68
69
70
71
72
73
# File 'lib/nikki.rb', line 65

def export(export_year)
  YAML::Store.new(NIKKI_FILE).transaction do |store|
    store['entries'].each do |entry|
      if entry.keys[0].year.to_s == export_year
        puts "#{entry.keys[0]}: #{entry.values[0]}"
      end
    end
  end
end

#lsString

Display Nikki’s latest entires

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (String)


52
53
54
55
56
57
58
59
60
61
# File 'lib/nikki.rb', line 52

def ls
  YAML::Store.new(NIKKI_FILE).transaction do |store|
    entries = store['entries'].last(5)
    entries.each do |entry|
      entry.each do |date, text|
        puts "#{date}: #{text}"
      end
    end
  end
end

#missed(entry) ⇒ Object

Creates a new entry for yesterday

Parameters:

  • entry (String)

Since:

  • 0.5.3



37
38
39
# File 'lib/nikki.rb', line 37

def missed(entry)
  new(entry, (Date.today - 1))
end

#new(entry, date = Date.today) ⇒ Hash

Add entry to journal Reads the settings in from the config YAML file and changes the date updated. It does the same with the journal file, reading in the YAML and merging the hash of entries, and then saves the YAML back again.

Examples:

"nikki new 'This is a thing I learned today!'"

Parameters:

  • entry (String)

    entry to add to the journal

  • update (String)

    International date for update

Returns:

  • (Hash)

    Returns a Hash which is then saved in a YAML file.



25
26
27
28
29
30
31
# File 'lib/nikki.rb', line 25

def new(entry, date = Date.today)
  YAML::Store.new("#{ENV['HOME']}/.nikki/nikki.yaml").transaction do |store|
    store['entries'] << { date => entry.strip }
  end

  ls
end

#openObject

Open Nikki journal in configured text editor



44
45
46
# File 'lib/nikki.rb', line 44

def open
  system(ENV['EDITOR'], NIKKI_FILE)
end