Class: Topping::ConfigurationBuilder
- Inherits:
-
Object
- Object
- Topping::ConfigurationBuilder
- Defined in:
- lib/topping/configuration_builder.rb
Overview
Provides a DSL for building Configuration objects.
Instance Attribute Summary collapse
-
#children ⇒ Array<ConfigurationBuilder>
readonly
private
An array of any nested configuration builders.
-
#configuration ⇒ Boolean
readonly
private
A object that stores user settings.
-
#name ⇒ String, Symbol
private
The name of the configuration attribute.
-
#required ⇒ Boolean
(also: #required?)
private
A boolean indicating whether or not the attribute must be set.
-
#types ⇒ Array<Object>
private
An array of valid types for the attribute.
-
#validator ⇒ Proc
readonly
private
A block used to validate the attribute.
-
#value ⇒ Object
private
The value of the configuration attribute.
Class Method Summary collapse
-
.freeze_config(config) ⇒ void
private
Deeply freezes a configuration object so that it can no longer be modified.
Instance Method Summary collapse
-
#build(object = Configuration.new) ⇒ Configuration
private
Builds a Configuration object from the attributes defined on the builder.
-
#children? ⇒ Boolean
private
Returns a boolean indicating whether or not the attribute has any child attributes.
-
#combine(name, attribute) ⇒ void
private
Merges two configuration builders by making one an attribute on the other.
-
#config(name, types: nil, type: nil, required: false, default: nil) { ... } ⇒ void
rubocop:disable Metrics1/ParameterLists Declares a configuration attribute.
-
#configure { ... } ⇒ Object
Merges two configuration builders by making one an attribute on the other.
-
#initialize ⇒ ConfigurationBuilder
constructor
A new instance of ConfigurationBuilder.
-
#validate! { ... } ⇒ void
Declares a block to be used to validate the value of an attribute whenever it’s set.
Constructor Details
#initialize ⇒ ConfigurationBuilder
Returns a new instance of ConfigurationBuilder.
57 58 59 60 |
# File 'lib/topping/configuration_builder.rb', line 57 def initialize @children = [] @name = :root end |
Instance Attribute Details
#children ⇒ Array<ConfigurationBuilder> (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.
An array of any nested configuration builders.
13 14 15 |
# File 'lib/topping/configuration_builder.rb', line 13 def children @children end |
#configuration ⇒ Boolean (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.
A object that stores user settings
18 19 20 |
# File 'lib/topping/configuration_builder.rb', line 18 def configuration @configuration end |
#name ⇒ String, Symbol
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.
The name of the configuration attribute.
33 34 35 |
# File 'lib/topping/configuration_builder.rb', line 33 def name @name end |
#required ⇒ Boolean Also known as: required?
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.
A boolean indicating whether or not the attribute must be set.
43 44 45 |
# File 'lib/topping/configuration_builder.rb', line 43 def required @required end |
#types ⇒ Array<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.
An array of valid types for the attribute.
23 24 25 |
# File 'lib/topping/configuration_builder.rb', line 23 def types @types end |
#validator ⇒ Proc (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.
A block used to validate the attribute.
28 29 30 |
# File 'lib/topping/configuration_builder.rb', line 28 def validator @validator end |
#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.
The value of the configuration attribute.
38 39 40 |
# File 'lib/topping/configuration_builder.rb', line 38 def value @value end |
Class Method Details
.freeze_config(config) ⇒ void
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.
This method returns an undefined value.
Deeply freezes a configuration object so that it can no longer be modified.
52 53 54 |
# File 'lib/topping/configuration_builder.rb', line 52 def freeze_config(config) IceNine.deep_freeze!(config) end |
Instance Method Details
#build(object = Configuration.new) ⇒ Configuration
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.
Builds a Topping::Configuration object from the attributes defined on the builder.
73 74 75 76 77 78 79 80 81 |
# File 'lib/topping/configuration_builder.rb', line 73 def build(object = Configuration.new) container = if children.empty? build_leaf(object) else build_nested(object) end @configuration = container.public_send(name) end |
#children? ⇒ 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 a boolean indicating whether or not the attribute has any child attributes.
86 87 88 |
# File 'lib/topping/configuration_builder.rb', line 86 def children? !children.empty? end |
#combine(name, attribute) ⇒ void
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.
This method returns an undefined value.
Merges two configuration builders by making one an attribute on the other.
96 97 98 99 100 |
# File 'lib/topping/configuration_builder.rb', line 96 def combine(name, attribute) attribute.name = name children << attribute end |
#config(name, types: nil, type: nil, required: false, default: nil) { ... } ⇒ void
This method returns an undefined value.
rubocop:disable Metrics1/ParameterLists Declares a configuration attribute.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/topping/configuration_builder.rb', line 114 def config(name, types: nil, type: nil, required: false, default: nil, &block) attribute = self.class.new attribute.name = name attribute.types = types || type attribute.required = required attribute.value = default attribute.instance_exec(&block) if block children << attribute # FIXME: I want remove Topping.build process. Topping.build attribute end |
#configure { ... } ⇒ Object
Merges two configuration builders by making one an attribute on the other.
64 65 66 |
# File 'lib/topping/configuration_builder.rb', line 64 def configure yield configuration end |
#validate! { ... } ⇒ void
This method returns an undefined value.
Declares a block to be used to validate the value of an attribute whenever it’s set. Validation blocks should return any object to indicate an error, or nil/false if validation passed.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/topping/configuration_builder.rb', line 142 def validate!(&block) validator = block unless value.nil? error = validator.call(value) raise ValidationError, error if error end @validator = block end |