Class: Lita::ConfigurationBuilder
- Inherits:
-
Object
- Object
- Lita::ConfigurationBuilder
- Defined in:
- lib/lita/configuration_builder.rb
Overview
Provides a DSL for building Configuration objects.
Instance Attribute Summary collapse
-
#children ⇒ Array<Lita::ConfigurationBuilder>
readonly
An array of any nested configuration builders.
-
#name ⇒ String, Symbol
The name of the configuration attribute.
-
#required ⇒ Boolean
(also: #required?)
A boolean indicating whether or not the attribute must be set.
-
#types ⇒ Array<Object>
An array of valid types for the attribute.
-
#validator ⇒ Proc
readonly
A block used to validate the attribute.
-
#value ⇒ Object
The value of the configuration attribute.
Class Method Summary collapse
-
.freeze_config(config) ⇒ void
Deeply freezes a configuration object so that it can no longer be modified.
-
.load_user_config(config_path = nil) ⇒ void
Loads configuration from a user configuration file.
Instance Method Summary collapse
-
#build(object = Configuration.new) ⇒ Lita::Confirmation
Builds a Configuration object from the attributes defined on the builder.
-
#children? ⇒ Boolean
Returns a boolean indicating whether or not the attribute has any child attributes.
-
#combine(name, attribute) ⇒ void
Merges two configuration builders by making one an attribute on the other.
-
#config(name, types: nil, type: nil, required: false, default: nil) { ... } ⇒ void
Declares a configuration attribute.
-
#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.
61 62 63 64 |
# File 'lib/lita/configuration_builder.rb', line 61 def initialize @children = [] @name = :root end |
Instance Attribute Details
#children ⇒ Array<Lita::ConfigurationBuilder> (readonly)
An array of any nested configuration builders.
11 12 13 |
# File 'lib/lita/configuration_builder.rb', line 11 def children @children end |
#name ⇒ String, Symbol
The name of the configuration attribute.
23 24 25 |
# File 'lib/lita/configuration_builder.rb', line 23 def name @name end |
#required ⇒ Boolean Also known as: required?
A boolean indicating whether or not the attribute must be set.
31 32 33 |
# File 'lib/lita/configuration_builder.rb', line 31 def required @required end |
#types ⇒ Array<Object>
An array of valid types for the attribute.
15 16 17 |
# File 'lib/lita/configuration_builder.rb', line 15 def types @types end |
#validator ⇒ Proc (readonly)
A block used to validate the attribute.
19 20 21 |
# File 'lib/lita/configuration_builder.rb', line 19 def validator @validator end |
#value ⇒ Object
The value of the configuration attribute.
27 28 29 |
# File 'lib/lita/configuration_builder.rb', line 27 def value @value end |
Class Method Details
.freeze_config(config) ⇒ void
This method returns an undefined value.
Deeply freezes a configuration object so that it can no longer be modified.
38 39 40 |
# File 'lib/lita/configuration_builder.rb', line 38 def freeze_config(config) IceNine.deep_freeze!(config) end |
.load_user_config(config_path = nil) ⇒ void
This method returns an undefined value.
Loads configuration from a user configuration file.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lita/configuration_builder.rb', line 45 def load_user_config(config_path = nil) config_path = "lita_config.rb" unless config_path begin load(config_path) rescue Exception => e Lita.logger.fatal I18n.t( "lita.config.exception", message: e., backtrace: e.backtrace.join("\n") ) abort end if File.exist?(config_path) end |
Instance Method Details
#build(object = Configuration.new) ⇒ Lita::Confirmation
Builds a Lita::Configuration object from the attributes defined on the builder.
70 71 72 73 74 75 76 77 78 |
# File 'lib/lita/configuration_builder.rb', line 70 def build(object = Configuration.new) container = if children.empty? build_leaf(object) else build_nested(object) end container.public_send(name) end |
#children? ⇒ Boolean
Returns a boolean indicating whether or not the attribute has any child attributes.
82 83 84 |
# File 'lib/lita/configuration_builder.rb', line 82 def children? !children.empty? end |
#combine(name, attribute) ⇒ void
This method returns an undefined value.
Merges two configuration builders by making one an attribute on the other.
91 92 93 94 95 |
# File 'lib/lita/configuration_builder.rb', line 91 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.
Declares a configuration attribute.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/lita/configuration_builder.rb', line 109 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 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.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lita/configuration_builder.rb', line 132 def validate(&block) validator = block unless value.nil? error = validator.call(value) raise ValidationError, error if error end @validator = block end |