Class: Dry::Configurable::DSL Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/configurable/dsl.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Setting DSL used by the class API

Constant Summary collapse

VALID_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/\A[a-z_]\w*\z/i
DATA_RESERVED_NAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Method names defined on ‘Data` (and its ancestors) that cannot be used as setting names because `Data.define` would reject them, which would break Config#to_data.

::Data.instance_methods.to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options, &block) ⇒ DSL

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DSL.



21
22
23
24
25
26
# File 'lib/dry/configurable/dsl.rb', line 21

def initialize(**options, &block)
  @compiler = Compiler.new
  @ast = []
  @options = options
  instance_exec(&block) if block
end

Instance Attribute Details

#astObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/dry/configurable/dsl.rb', line 17

def ast
  @ast
end

#compilerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/dry/configurable/dsl.rb', line 15

def compiler
  @compiler
end

#optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/dry/configurable/dsl.rb', line 19

def options
  @options
end

Instance Method Details

#config_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
# File 'lib/dry/configurable/dsl.rb', line 59

def config_class
  options[:config_class]
end

#defaultObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
# File 'lib/dry/configurable/dsl.rb', line 63

def default
  options[:default_undefined] ? Undefined : nil
end

#setting(name, **options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers a new setting node and compile it into a setting object

Returns:

  • Setting

See Also:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dry/configurable/dsl.rb', line 33

def setting(name, **options, &block)
  unless VALID_NAME.match?(name.to_s)
    raise ArgumentError, "#{name} is not a valid setting name"
  end

  if DATA_RESERVED_NAMES.include?(name.to_sym)
    raise ArgumentError,
          "#{name.inspect} is not a valid setting name: it conflicts with a Data " \
          "instance method, which would break Config#to_data"
  end

  ensure_valid_options(options)

  options = {default: default, config_class: config_class, **options}

  node = [:setting, [name.to_sym, options]]

  if block
    ast << [:nested, [node, DSL.new(**@options, &block).ast]]
  else
    ast << node
  end

  compiler.visit(ast.last)
end