Sha256: fca02b487a0c6db726ed682d5bfe5cc08086518d6165a79f1ee070aab2b212e8

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require "forwardable"

module Strict
  module Attributes
    class Configuration
      include Enumerable
      extend Forwardable

      class UnknownAttributeError < Error
        attr_reader :attribute_name

        def initialize(attribute_name:)
          super(message_from(attribute_name:))

          @attribute_name = attribute_name
        end

        private

        def message_from(attribute_name:)
          "Strict tried to find an attribute named #{attribute_name} but was unable. " \
            "It's likely this in an internal bug, feel free to open an issue at #{Strict::ISSUE_TRACKER} for help."
        end
      end

      def_delegator :attributes, :each

      attr_reader :attributes

      def initialize(attributes:)
        @attributes = attributes
        @attributes_index = attributes.to_h { |a| [a.name, a] }
      end

      def named!(name)
        attributes_index.fetch(name) { raise UnknownAttributeError.new(attribute_name: name) }
      end

      private

      attr_reader :attributes_index
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strict-1.1.0 lib/strict/attributes/configuration.rb
strict-1.0.0 lib/strict/attributes/configuration.rb