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.



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

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)



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

def children
  @children
end

#constructorObject (readonly)



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

def constructor
  @constructor
end

#defaultObject (readonly)



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

def default
  @default
end

#mutableObject (readonly)



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

def mutable
  @mutable
end

#nameObject (readonly)



18
19
20
# File 'lib/dry/configurable/setting.rb', line 18

def name
  @name
end

#optionsObject (readonly)



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

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)


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

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)


65
66
67
# File 'lib/dry/configurable/setting.rb', line 65

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)


60
61
62
# File 'lib/dry/configurable/setting.rb', line 60

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.



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

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