Class: ConfigMapper::ConfigStruct
- Inherits:
-
Object
- Object
- ConfigMapper::ConfigStruct
- Defined in:
- lib/config_mapper/config_struct.rb
Overview
A set of configurable attributes.
Defined Under Namespace
Classes: Attribute, NoValueProvided
Class Method Summary collapse
-
.attribute(name, type = nil, default: :no_default, description: nil) { ... } ⇒ Object
Defines reader and writer methods for the specified attribute.
- .attributes ⇒ Object
-
.component(name, type: ConfigStruct, description: nil, &block) ⇒ Object
Defines a sub-component.
-
.component_dict(name, type: ConfigStruct, key_type: nil, description: nil, &block) ⇒ Object
Defines an associative array of sub-components.
-
.config_doc ⇒ Hash
Generate documentation, as Ruby data.
- .each_attribute(&action) ⇒ Object
Instance Method Summary collapse
- #config_errors ⇒ Object
-
#configure_with(attribute_values) ⇒ Hash
Configure with data.
- #immediate_config_errors ⇒ Object
-
#initialize ⇒ ConfigStruct
constructor
A new instance of ConfigStruct.
-
#to_h ⇒ Hash
Return the configuration as a Hash.
Constructor Details
#initialize ⇒ ConfigStruct
Returns a new instance of ConfigStruct.
115 116 117 118 119 |
# File 'lib/config_mapper/config_struct.rb', line 115 def initialize self.class.each_attribute do |attribute| instance_variable_set("@#{attribute.name}", attribute.initial_value) end end |
Class Method Details
.attribute(name, type = nil, default: :no_default, description: nil) { ... } ⇒ Object
Defines reader and writer methods for the specified attribute.
A :default value may be specified; otherwise, the attribute is considered mandatory.
If a block is provided, it will invoked in the writer-method to validate the argument.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/config_mapper/config_struct.rb', line 26 def attribute(name, type = nil, default: :no_default, description: nil, &type_block) attribute = attribute!(name) attribute.description = description if default == :no_default attribute.required = true else attribute.default = default.freeze end attribute.validator = Validator.resolve(type || type_block) define_method("#{attribute.name}=") do |value| if value.nil? raise NoValueProvided if attribute.required else value = attribute.validator.call(value) if attribute.validator end instance_variable_set("@#{attribute.name}", value) end end |
.attributes ⇒ Object
90 91 92 |
# File 'lib/config_mapper/config_struct.rb', line 90 def attributes attributes_by_name.values end |
.component(name, type: ConfigStruct, description: nil, &block) ⇒ Object
Defines a sub-component.
If a block is be provided, it will be ‘class_eval`ed to define the sub-components class.
58 59 60 61 62 63 |
# File 'lib/config_mapper/config_struct.rb', line 58 def component(name, type: ConfigStruct, description: nil, &block) type = Class.new(type, &block) if block attribute = attribute!(name) attribute.description = description attribute.factory = type end |
.component_dict(name, type: ConfigStruct, key_type: nil, description: nil, &block) ⇒ Object
Defines an associative array of sub-components.
If a block is be provided, it will be ‘class_eval`ed to define the sub-components class.
74 75 76 77 |
# File 'lib/config_mapper/config_struct.rb', line 74 def component_dict(name, type: ConfigStruct, key_type: nil, description: nil, &block) type = Class.new(type, &block) if block component(name, type: ConfigDict::Factory.new(type, key_type), description: description) end |
.config_doc ⇒ Hash
Generate documentation, as Ruby data.
Returns an entry for each configurable path, detailing description, type, and default.
86 87 88 |
# File 'lib/config_mapper/config_struct.rb', line 86 def config_doc each_attribute.sort_by(&:name).map(&:config_doc).inject({}, :merge) end |
.each_attribute(&action) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/config_mapper/config_struct.rb', line 94 def each_attribute(&action) return enum_for(:each_attribute) unless action ancestors.each do |klass| next unless klass.respond_to?(:attributes) klass.attributes.each(&action) end end |
Instance Method Details
#config_errors ⇒ Object
125 126 127 |
# File 'lib/config_mapper/config_struct.rb', line 125 def config_errors immediate_config_errors.merge(component_config_errors) end |
#configure_with(attribute_values) ⇒ Hash
Configure with data.
134 135 136 137 |
# File 'lib/config_mapper/config_struct.rb', line 134 def configure_with(attribute_values) errors = ConfigMapper.configure_with(attribute_values, self) config_errors.merge(errors) end |
#immediate_config_errors ⇒ Object
121 122 123 |
# File 'lib/config_mapper/config_struct.rb', line 121 def immediate_config_errors missing_required_attribute_errors end |
#to_h ⇒ Hash
Return the configuration as a Hash.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/config_mapper/config_struct.rb', line 143 def to_h {}.tap do |result| self.class.each_attribute do |attribute| value = send(attribute.name) if value && value.respond_to?(:to_h) && !value.is_a?(Array) value = value.to_h end result[attribute.name.to_s] = value end end end |