Sha256: 221396b6152454bc20884401345474e3d45e51b0433de5af114bf025ed0a5613

Contents?: true

Size: 1.31 KB

Versions: 13

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "core"
require "refinements/array"

module Milestoner
  module Commits
    # Retrieves and categorizes Git repository commit tagged or untagged history.
    class Categorizer
      include Import[:settings]

      using Refinements::Array

      def initialize(collector: Collector.new, **)
        @collector = collector
        super(**)

        @labels = settings.commit_categories.pluck :label
        @pattern = labels.empty? ? // : Regexp.union(labels)
      end

      def call min: Collector::MIN, max: Collector::MAX
        collect(min, max).each_value { |commits| commits.sort_by!(&:subject) }
                         .values
                         .flatten
      end

      private

      attr_reader :collector, :labels, :pattern

      def collect min, max
        collector.call(min:, max:)
                 .value_or(Core::EMPTY_ARRAY)
                 .each
                 .with_object categories do |commit, collection|
                   category = commit.subject[pattern]
                   key = collection.key?(category) ? category : "Unknown"
                   collection[key] << commit
                 end
      end

      def categories
        labels.reduce({}) { |group, prefix| group.merge prefix => [] }
              .merge! "Unknown" => []
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
milestoner-18.11.0 lib/milestoner/commits/categorizer.rb
milestoner-18.10.0 lib/milestoner/commits/categorizer.rb
milestoner-18.9.0 lib/milestoner/commits/categorizer.rb
milestoner-18.8.0 lib/milestoner/commits/categorizer.rb
milestoner-18.7.0 lib/milestoner/commits/categorizer.rb
milestoner-18.6.0 lib/milestoner/commits/categorizer.rb
milestoner-18.5.0 lib/milestoner/commits/categorizer.rb
milestoner-18.4.0 lib/milestoner/commits/categorizer.rb
milestoner-18.3.0 lib/milestoner/commits/categorizer.rb
milestoner-18.2.0 lib/milestoner/commits/categorizer.rb
milestoner-18.1.0 lib/milestoner/commits/categorizer.rb
milestoner-18.0.1 lib/milestoner/commits/categorizer.rb
milestoner-18.0.0 lib/milestoner/commits/categorizer.rb