Sha256: 4dc03494ce3000ac5fa071b08ce78463c8a1895b86675ad43643e2a06f023752

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 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

9 entries across 9 versions & 1 rubygems

Version Path
soapy_cake-2.2.7 lib/soapy_cake/modification_type.rb
soapy_cake-2.2.6 lib/soapy_cake/modification_type.rb
soapy_cake-2.2.5 lib/soapy_cake/modification_type.rb
soapy_cake-2.2.4 lib/soapy_cake/modification_type.rb
soapy_cake-2.2.3 lib/soapy_cake/modification_type.rb
soapy_cake-2.2.1 lib/soapy_cake/modification_type.rb
soapy_cake-2.1.6 lib/soapy_cake/modification_type.rb
soapy_cake-2.1.5 lib/soapy_cake/modification_type.rb
soapy_cake-2.1.4 lib/soapy_cake/modification_type.rb