Sha256: 1e08d9dff4484e2a0b1021bbcea4fc9bb972562d1d51323f8c5065fedbc3c66d

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module DNote
  # = Notes Collection
  #
  # This class contains a collection of Note objects and can group them in
  # several ways.
  #
  class NotesCollection
    include Enumerable

    def initialize(notes)
      @notes = notes
    end

    # Array of notes.
    attr_reader :notes

    # Notes counts by label.
    def counts
      @counts ||=
        begin
          h = {}
          by_label.each do |label, notes|
            h[label] = notes.size
          end
          h
        end
    end

    # Iterate through notes.
    def each(&block)
      notes.each(&block)
    end

    # No notes?
    def empty?
      notes.empty?
    end

    # Organize notes into a hash with labels for keys.
    def by_label
      @by_label ||= notes.group_by(&:label)
    end

    # Organize notes into a hash with filename for keys.
    def by_file
      @by_file ||= notes.group_by(&:file)
    end

    # Organize notes into a hash with labels for keys, followed
    # by a hash with filename for keys.
    def by_label_file
      @by_label_file ||= by_label.transform_values { |notes| notes.group_by(&:file) }
    end

    # Organize notes into a hash with filenames for keys, followed
    # by a hash with labels for keys.
    def by_file_label
      @by_file_label ||= by_file.transform_values { |notes| notes.group_by(&:label) }
    end

    # Convert to an array of hashes.
    def to_a
      notes.map(&:to_h)
    end

    # Same as #by_label.
    def to_h
      by_label
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mvz-dnote-1.10.0 lib/dnote/notes_collection.rb
mvz-dnote-1.9.0 lib/dnote/notes_collection.rb