Class: Generator
- Inherits:
-
Thor
- Object
- Thor
- Generator
- 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.
Constant Summary collapse
- NIKKI_FILE =
"#{ENV['HOME']}/.config/nikki/nikki.yaml".freeze
Data entry collapse
-
#missed(entry) ⇒ Object
Creates a new entry for yesterday.
-
#new(entry, date = Date.today) ⇒ Hash
Add entry to journal Reads the settings in from the config YAML file and changes the date updated.
Instance Method Summary collapse
- #export(export_year) ⇒ Object
-
#ls ⇒ String
Display Nikki’s latest entires.
-
#open ⇒ Object
Open Nikki journal in configured text editor.
Instance Method Details
#export(export_year) ⇒ Object
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 |
#ls ⇒ String
Display Nikki’s latest entires
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
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.
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 |
#open ⇒ Object
Open Nikki journal in configured text editor
44 45 46 |
# File 'lib/nikki.rb', line 44 def open system(ENV['EDITOR'], NIKKI_FILE) end |