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.
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, ** ) @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)
30 31 32 |
# File 'lib/dry/configurable/setting.rb', line 30 def children @children end |
#constructor ⇒ Object (readonly)
27 28 29 |
# File 'lib/dry/configurable/setting.rb', line 27 def constructor @constructor end |
#default ⇒ Object (readonly)
21 22 23 |
# File 'lib/dry/configurable/setting.rb', line 21 def default @default end |
#mutable ⇒ Object (readonly)
24 25 26 |
# File 'lib/dry/configurable/setting.rb', line 24 def mutable @mutable end |
#name ⇒ Object (readonly)
18 19 20 |
# File 'lib/dry/configurable/setting.rb', line 18 def name @name end |
#options ⇒ Object (readonly)
33 34 35 |
# File 'lib/dry/configurable/setting.rb', line 33 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.
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?
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.
60 61 62 |
# File 'lib/dry/configurable/setting.rb', line 60 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.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dry/configurable/setting.rb', line 71 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 |