Sha256: 1f9c0b9da92ad31f661bc111666bc67cd0eda3f6166324c093204036eff761a9

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Kind
  module Validator
    DEFAULT_STRATEGIES = Set.new(%w[instance_of kind_of]).freeze

    class InvalidDefinition < ArgumentError
      OPTIONS = 'Options to define one: :of, :is, :respond_to, :instance_of, :array_of or :array_with'.freeze

      def initialize(attribute)
        super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
      end

      private_constant :OPTIONS
    end

    class InvalidDefaultStrategy < ArgumentError
      OPTIONS =
        DEFAULT_STRATEGIES.map { |option| ":#{option}" }.join(', ')

      def initialize(option)
        super "#{option.inspect} is an invalid option. Please use one of these: #{OPTIONS}"
      end

      private_constant :OPTIONS
    end

    def self.default_strategy
      @default_strategy ||= :kind_of
    end

    def self.default_strategy=(option)
      if DEFAULT_STRATEGIES.member?(String(option))
        @default_strategy = option.to_sym
      else
        raise InvalidDefaultStrategy.new(option)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kind-3.1.0 lib/kind/validator.rb
kind-3.0.1 lib/kind/validator.rb
kind-3.0.0 lib/kind/validator.rb
kind-2.3.0 lib/kind/validator.rb
kind-2.2.0 lib/kind/validator.rb
kind-2.1.0 lib/kind/validator.rb
kind-2.0.0 lib/kind/validator.rb