Sha256: 5a66e72df5d3ce63f0ad792b9031008f4e0cffbfe40a08a832421a0198c90d7f

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Strict
  class InitializationError < Error
    attr_reader :remaining_attributes, :invalid_attributes, :missing_attributes

    def initialize(initializable_class:, remaining_attributes:, invalid_attributes:, missing_attributes:)
      super(message_from(initializable_class:, remaining_attributes:, invalid_attributes:, missing_attributes:))

      @remaining_attributes = remaining_attributes
      @invalid_attributes = invalid_attributes
      @missing_attributes = missing_attributes
    end

    private

    def message_from(initializable_class:, remaining_attributes:, invalid_attributes:, missing_attributes:)
      details = [
        invalid_attributes_message_from(invalid_attributes),
        missing_attributes_message_from(missing_attributes),
        remaining_attributes_message_from(remaining_attributes)
      ].compact.join("\n")

      "Initialization of #{initializable_class} failed because:\n#{details}"
    end

    def invalid_attributes_message_from(invalid_attributes)
      return nil unless invalid_attributes

      details = invalid_attributes.map do |attribute, value|
        "    - #{attribute.name}: got #{value.inspect}, expected #{attribute.validator.inspect}"
      end.join("\n")

      "  Some attributes were invalid:\n#{details}"
    end

    def missing_attributes_message_from(missing_attributes)
      return nil unless missing_attributes

      details = missing_attributes.map do |attribute_name|
        "    - #{attribute_name}"
      end.join("\n")

      "  Some attributes were missing:\n#{details}"
    end

    def remaining_attributes_message_from(remaining_attributes)
      return nil if remaining_attributes.none?

      details = remaining_attributes.map do |attribute_name|
        "    - #{attribute_name}"
      end.join("\n")

      "  Some attributes were provided, but not defined:\n#{details}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strict-1.1.0 lib/strict/initialization_error.rb
strict-1.0.0 lib/strict/initialization_error.rb