Class: Dry::Configurable::Setting
- Inherits:
-
Object
- Object
- Dry::Configurable::Setting
- 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
- #children ⇒ Object readonly
- #constructor ⇒ Object readonly
- #default ⇒ Object readonly
- #mutable ⇒ Object readonly
- #name ⇒ Object readonly
- #options ⇒ Object readonly
Class Method Summary collapse
- .mutable_value?(value) ⇒ Boolean private
Instance Method Summary collapse
-
#initialize(name, default:, constructor: DEFAULT_CONSTRUCTOR, children: EMPTY_ARRAY, **options) ⇒ Setting
constructor
private
A new instance of Setting.
- #mutable? ⇒ Boolean (also: #cloneable?)
- #reader? ⇒ Boolean private
- #to_value ⇒ Object private
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, ** ) @name = name @default = default @mutable = children.any? || .fetch(:mutable) { # Allow `cloneable` as an option alias for `mutable` .fetch(:cloneable) { Setting.mutable_value?(default) } } @constructor = constructor @children = children @options = end |
Instance Attribute Details
#children ⇒ Object (readonly)
32 33 34 |
# File 'lib/dry/configurable/setting.rb', line 32 def children @children end |
#constructor ⇒ Object (readonly)
29 30 31 |
# File 'lib/dry/configurable/setting.rb', line 29 def constructor @constructor end |
#default ⇒ Object (readonly)
23 24 25 |
# File 'lib/dry/configurable/setting.rb', line 23 def default @default end |
#mutable ⇒ Object (readonly)
26 27 28 |
# File 'lib/dry/configurable/setting.rb', line 26 def mutable @mutable end |
#name ⇒ Object (readonly)
20 21 22 |
# File 'lib/dry/configurable/setting.rb', line 20 def name @name end |
#options ⇒ Object (readonly)
35 36 37 |
# File 'lib/dry/configurable/setting.rb', line 35 def @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.
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?
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.
62 63 64 |
# File 'lib/dry/configurable/setting.rb', line 62 def reader? [:reader].equal?(true) end |
#to_value ⇒ 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.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dry/configurable/setting.rb', line 73 def to_value if children.any? ([:config_class] || Config).new(children) else value = default value = constructor.(value) unless value.eql?(Undefined) mutable? ? value.dup : value end end |