Class: DNote::NotesCollection
- Inherits:
-
Object
- Object
- DNote::NotesCollection
- Includes:
- Enumerable
- Defined in:
- lib/dnote/notes_collection.rb
Overview
Notes Collection
This class contains a collection of Note objects and can group them in several ways.
Instance Attribute Summary collapse
-
#notes ⇒ Object
readonly
Array of notes.
Instance Method Summary collapse
-
#by_file ⇒ Object
Organize notes into a hash with filename for keys.
-
#by_file_label ⇒ Object
Organize notes into a hash with filenames for keys, followed by a hash with labels for keys.
-
#by_label ⇒ Object
Organize notes into a hash with labels for keys.
-
#by_label_file ⇒ Object
Organize notes into a hash with labels for keys, followed by a hash with filename for keys.
-
#counts ⇒ Object
Notes counts by label.
-
#each(&block) ⇒ Object
Iterate through notes.
-
#empty? ⇒ Boolean
No notes?.
-
#initialize(notes) ⇒ NotesCollection
constructor
A new instance of NotesCollection.
-
#to_a ⇒ Object
Convert to an array of hashes.
-
#to_h ⇒ Object
Same as #by_label.
Constructor Details
#initialize(notes) ⇒ NotesCollection
Returns a new instance of NotesCollection.
12 13 14 |
# File 'lib/dnote/notes_collection.rb', line 12 def initialize(notes) @notes = notes end |
Instance Attribute Details
#notes ⇒ Object (readonly)
Array of notes.
17 18 19 |
# File 'lib/dnote/notes_collection.rb', line 17 def notes @notes end |
Instance Method Details
#by_file ⇒ Object
Organize notes into a hash with filename for keys.
47 48 49 |
# File 'lib/dnote/notes_collection.rb', line 47 def by_file @by_file ||= notes.group_by(&:file) end |
#by_file_label ⇒ Object
Organize notes into a hash with filenames for keys, followed by a hash with labels for keys.
59 60 61 |
# File 'lib/dnote/notes_collection.rb', line 59 def by_file_label @by_file_label ||= by_file.transform_values { |notes| notes.group_by(&:label) } end |
#by_label ⇒ Object
Organize notes into a hash with labels for keys.
42 43 44 |
# File 'lib/dnote/notes_collection.rb', line 42 def by_label @by_label ||= notes.group_by(&:label) end |
#by_label_file ⇒ Object
Organize notes into a hash with labels for keys, followed by a hash with filename for keys.
53 54 55 |
# File 'lib/dnote/notes_collection.rb', line 53 def by_label_file @by_label_file ||= by_label.transform_values { |notes| notes.group_by(&:file) } end |
#counts ⇒ Object
Notes counts by label.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dnote/notes_collection.rb', line 20 def counts @counts ||= begin h = {} by_label.each do |label, notes| h[label] = notes.size end h end end |
#each(&block) ⇒ Object
Iterate through notes.
32 33 34 |
# File 'lib/dnote/notes_collection.rb', line 32 def each(&block) notes.each(&block) end |
#empty? ⇒ Boolean
No notes?
37 38 39 |
# File 'lib/dnote/notes_collection.rb', line 37 def empty? notes.empty? end |
#to_a ⇒ Object
Convert to an array of hashes.
64 65 66 |
# File 'lib/dnote/notes_collection.rb', line 64 def to_a notes.map(&:to_h) end |
#to_h ⇒ Object
Same as #by_label.
69 70 71 |
# File 'lib/dnote/notes_collection.rb', line 69 def to_h by_label end |