Sha256: 1c5ca0be2cd0b4cd9199f13c995b998b34d7ebec096926628091ce6b40f5e08d

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'dry/configurable/constants'
require 'dry/configurable/setting'
require 'dry/configurable/settings'
require 'dry/configurable/compiler'
require 'dry/configurable/dsl/args'

module Dry
  module Configurable
    # Setting DSL used by the class API
    #
    # @api private
    class DSL
      VALID_NAME = /\A[a-z_]\w*\z/i.freeze

      # @api private
      attr_reader :compiler

      # @api private
      attr_reader :ast

      # @api private
      def initialize(&block)
        @compiler = Compiler.new
        @ast = []
        instance_exec(&block) if block
      end

      # Register a new setting node and compile it into a setting object
      #
      # @see ClassMethods.setting
      # @api public
      # @return Setting
      def setting(name, *args, &block)
        unless VALID_NAME.match?(name.to_s)
          raise ArgumentError, "#{name} is not a valid setting name"
        end

        args = Args.new(args)

        args.ensure_valid_options

        default, opts = args

        node = [:setting, [name.to_sym, default, opts == default ? EMPTY_HASH : opts]]

        if block
          if block.arity.zero?
            ast << [:nested, [node, DSL.new(&block).ast]]
          else
            ast << [:constructor, [node, block]]
          end
        else
          ast << node
        end

        compiler.visit(ast.last)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

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