Sha256: 6d353581f79df41ce43b5017a78861b151d6c0ee1a5019b3e0827595106303f4

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module NxtInit
  class Option
    InvalidOptionError = Class.new(ArgumentError)

    def initialize(key, default_value: NotProvidedOption.new)
      @key = key
      @default_value = default_value
    end

    attr_reader :key, :default_value

    def resolve(attrs, target:)
      if default_value_was_given?
        key_missing = !attrs.key?(key)
        given_value = attrs[key]

        if default_value_is_preprocessor?
          key_missing ? raise_key_error : target.instance_exec(given_value, &default_value)
        else
          # only when the given value was nil we will evaluate the fallback --> false is a valid value
          if given_value.nil?
            default_value_is_block? ? target.instance_exec(&default_value) : default_value
          else
            given_value
          end
        end
      elsif requires_value?
        attrs.fetch(key) { |_| raise_key_error }
      else
        raise InvalidOptionError, "Don't know how to deal with #{self}"
      end
    end

    def requires_value?
      !default_value_was_given?
    end

    def default_value_is_block?
      default_value && default_value.respond_to?(:call)
    end

    def default_value_is_preprocessor?
      default_value_is_block? && default_value.arity == 1
    end

    def default_value_was_given?
      !default_value.is_a?(NotProvidedOption)
    end

    private

    def raise_key_error
      raise KeyError, "NxtInit attr_init key :#{key} was missing at initialization!"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nxt_init-0.1.5 lib/nxt_init/option.rb