Sha256: d8bbdfbbe3dcddcb8c1504ff9d6af2e34e30e374f42b2ba3d872e905c9e57e13

Contents?: true

Size: 1.17 KB

Versions: 10

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require 'dry/configurable/constants'
require 'dry/configurable/setting'

module Dry
  module Configurable
    class DSL
      # @api private
      class Args
        # @api private
        attr_reader :args

        # @api private
        attr_reader :size

        # @api private
        attr_reader :opts

        # @api private
        def initialize(args)
          @args = args
          @size = args.size
          @opts = Setting::OPTIONS
        end

        # @api private
        def ensure_valid_options
          return unless options

          keys = options.keys - opts
          raise ArgumentError, "Invalid options: #{keys.inspect}" unless keys.empty?
        end

        # @api private
        def to_ary
          [default, options || EMPTY_HASH]
        end

        # @api private
        def default
          if size.equal?(1) && options.nil?
            args[0]
          elsif size > 1 && options
            args[0]
          else
            Undefined
          end
        end

        # @api private
        def options
          args.detect { |arg| arg.is_a?(Hash) && (opts & arg.keys).any? }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
dry-configurable-0.12.1 lib/dry/configurable/dsl/args.rb
dry-configurable-0.12.0 lib/dry/configurable/dsl/args.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-configurable-0.11.6/lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.6 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.5 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.4 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.3 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.2 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.1 lib/dry/configurable/dsl/args.rb
dry-configurable-0.11.0 lib/dry/configurable/dsl/args.rb