Class: Til::NoteList

Inherits:
Object
  • Object
show all
Includes:
Thor::Actions, Thor::Base
Defined in:
lib/til/models/note_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notes = Array.new) ⇒ NoteList

Returns a new instance of NoteList.



7
8
9
# File 'lib/til/models/note_list.rb', line 7

def initialize(notes=Array.new)
  @notes = notes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



41
42
43
# File 'lib/til/models/note_list.rb', line 41

def method_missing(m, *args, &block)
  notes.send(m, *args, &block)
end

Instance Attribute Details

#notesObject

Returns the value of attribute notes.



5
6
7
# File 'lib/til/models/note_list.rb', line 5

def notes
  @notes
end

Instance Method Details

#filterObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/til/models/note_list.rb', line 19

def filter
  if notes.length == 1
    notes.first
  else
    notes.each_with_index do |note, index|
      puts "#{index+1}) #{note.title.bold} (#{note.subject})"
    end
  
    begin
      choice = ask("Choice: ").to_i
    rescue Interrupt
      warn "\nAborted!"
      abort
    end

    notes.fetch(choice-1) do
      puts "Invalid choice!"
      abort
    end
  end
end

#most_recentObject



15
16
17
# File 'lib/til/models/note_list.rb', line 15

def most_recent
  notes.sort { |a, b| b.mtime <=> a.mtime }.fetch(0)
end

#sort_by_modified_timeObject



11
12
13
# File 'lib/til/models/note_list.rb', line 11

def sort_by_modified_time
  notes.sort { |a, b| b.mtime <=> a.mtime }
end