Sha256: db22b5f4a920a40636c11ab5c46c82eff05930f5f8a79a4e0a58bb7a66b27cf9

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Strict
  class Parameter
    NOT_PROVIDED = ::Object.new.freeze

    class << self
      def make(name, validator = Validators::Anything.instance, coerce: false, **defaults)
        unless valid_defaults?(**defaults)
          raise ArgumentError, "Only one of 'default', 'default_value', or 'default_generator' can be provided"
        end

        new(
          name: name.to_sym,
          validator: validator,
          default_generator: make_default_generator(**defaults),
          coercer: coerce
        )
      end

      private

      def valid_defaults?(default: NOT_PROVIDED, default_value: NOT_PROVIDED, default_generator: NOT_PROVIDED)
        defaults_provided = [default, default_value, default_generator].count do |default_option|
          !default_option.equal?(NOT_PROVIDED)
        end

        defaults_provided <= 1
      end

      def make_default_generator(default: NOT_PROVIDED, default_value: NOT_PROVIDED, default_generator: NOT_PROVIDED)
        if !default.equal?(NOT_PROVIDED)
          default.respond_to?(:call) ? default : -> { default }
        elsif !default_value.equal?(NOT_PROVIDED)
          -> { default_value }
        elsif !default_generator.equal?(NOT_PROVIDED)
          default_generator
        else
          NOT_PROVIDED
        end
      end
    end

    attr_reader :name, :validator, :default_generator, :coercer

    def initialize(name:, validator:, default_generator:, coercer:)
      @name = name.to_sym
      @validator = validator
      @default_generator = default_generator
      @coercer = coercer
      @optional = !default_generator.equal?(NOT_PROVIDED)
    end

    def optional?
      @optional
    end

    def valid?(value)
      validator === value
    end

    def coerce(value)
      return value unless coercer

      coercer.call(value)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
strict-1.4.0 lib/strict/parameter.rb
strict-1.3.1 lib/strict/parameter.rb
strict-1.3.0 lib/strict/parameter.rb
strict-1.2.0 lib/strict/parameter.rb