Follow the Brass

lib/caliber_batch.rb

34 lines · ruby

Project files
# frozen_string_literal: true

require_relative "placement"

class CaliberBatch
  attr_reader :caliber, :capacity

  def initialize(caliber:, capacity: 50)
    @caliber = caliber
    @capacity = capacity
    @contents = []
  end

  def accept(item)
    reason = refusal_for(item)
    @contents << item if reason.nil?

    Placement.new(item:, reason:)
  end

  def contents
    @contents.dup.freeze
  end

  private

  def refusal_for(item)
    return :not_a_case unless item.respond_to?(:caliber)
    return :wrong_caliber unless item.caliber == @caliber
    return :batch_full if @contents.length >= @capacity

    nil
  end
end

Read along in the manual: The Reloading Lab · Download the project (zip)