Class: Dry::Configurable::Setting

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

Overview

A defined setting.

Constant Summary collapse

OPTIONS =
%i[default reader constructor mutable cloneable settings config_class].freeze
DEFAULT_CONSTRUCTOR =
-> v { v }.freeze
MUTABLE_VALUE_TYPES =
[Array, Hash, Set, Config].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default:, constructor: DEFAULT_CONSTRUCTOR, children: EMPTY_ARRAY, **options) ⇒ Setting

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 Setting.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dry/configurable/setting.rb', line 43

def initialize(
  name,
  default:,
  constructor: DEFAULT_CONSTRUCTOR,
  children: EMPTY_ARRAY,
  **options
)
  @name = name
  @default = default
  @mutable = children.any? || options.fetch(:mutable) {
    # Allow `cloneable` as an option alias for `mutable`
    options.fetch(:cloneable) { Setting.mutable_value?(default) }
  }
  @constructor = constructor
  @children = children
  @options = options
end

Instance Attribute Details

#childrenObject (readonly)



32
33
34
# File 'lib/dry/configurable/setting.rb', line 32

def children
  @children
end

#constructorObject (readonly)



29
30
31
# File 'lib/dry/configurable/setting.rb', line 29

def constructor
  @constructor
end

#defaultObject (readonly)



23
24
25
# File 'lib/dry/configurable/setting.rb', line 23

def default
  @default
end

#mutableObject (readonly)



26
27
28
# File 'lib/dry/configurable/setting.rb', line 26

def mutable
  @mutable
end

#nameObject (readonly)



20
21
22
# File 'lib/dry/configurable/setting.rb', line 20

def name
  @name
end

#optionsObject (readonly)



35
36
37
# File 'lib/dry/configurable/setting.rb', line 35

def options
  @options
end

Class Method Details

.mutable_value?(value) ⇒ Boolean

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:

  • (Boolean)


38
39
40
# File 'lib/dry/configurable/setting.rb', line 38

def self.mutable_value?(value)
  MUTABLE_VALUE_TYPES.any? { |type| value.is_a?(type) }
end

Instance Method Details

#mutable?Boolean Also known as: cloneable?

Returns:

  • (Boolean)


67
68
69
# File 'lib/dry/configurable/setting.rb', line 67

def mutable?
  mutable
end

#reader?Boolean

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:

  • (Boolean)


62
63
64
# File 'lib/dry/configurable/setting.rb', line 62

def reader?
  options[:reader].equal?(true)
end

#to_valueObject

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/dry/configurable/setting.rb', line 73

def to_value
  if children.any?
    (options[:config_class] || Config).new(children)
  else
    value = default
    value = constructor.(value) unless value.eql?(Undefined)

    mutable? ? value.dup : value
  end
end