Class: DNote::NotesCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#notesObject (readonly)

Array of notes.



17
18
19
# File 'lib/dnote/notes_collection.rb', line 17

def notes
  @notes
end

Instance Method Details

#by_fileObject

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_labelObject

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_labelObject

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_fileObject

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

#countsObject

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?

Returns:

  • (Boolean)


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

def empty?
  notes.empty?
end

#to_aObject

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_hObject

Same as #by_label.



69
70
71
# File 'lib/dnote/notes_collection.rb', line 69

def to_h
  by_label
end