Sha256: b44c02a41f43298c39d5db3368165f0c0026f504d15f96b92e5fe6040d65381e

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module SoapyCake
  class ModificationType
    CHANGE = 'change'
    REMOVE = 'remove'
    DO_NOT_CHANGE = 'do_not_change'

    def initialize(key, modification_type_key, default)
      @key = key
      @modification_type_key = modification_type_key
      @default = default
    end

    def options(input_opts)
      validate_input(input_opts)

      input_opts.merge(
        key => value(input_opts),
        modification_type_key => modification_type(input_opts)
      )
    end

    private

    attr_reader :key, :modification_type_key, :default

    def value(input_opts)
      input_opts.fetch(key, default)
    end

    def modification_type(input_opts)
      input_opts.fetch(modification_type_key) do
        input_opts[key] ? CHANGE : REMOVE
      end
    end

    def validate_input(input_opts)
      return unless input_opts[key].nil? && input_opts[modification_type_key] == CHANGE

      raise(
        InvalidInput,
        "`#{modification_type_key}` was '#{CHANGE}', but no `#{key}` was provided to change it to"
      )
    end

    InvalidInput = Class.new(StandardError)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
soapy_cake-2.3.1 lib/soapy_cake/modification_type.rb
soapy_cake-2.3.0 lib/soapy_cake/modification_type.rb