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: NoValueProvided
Class Method Summary collapse
-
.attribute(name, options = {}) { ... } ⇒ Object
Defines reader and writer methods for the specified attribute.
- .attribute_initializers ⇒ Object
-
.component(name, options = {}, &block) ⇒ Object
Defines a sub-component.
-
.component_dict(name, options = {}, &block) ⇒ Object
Defines an associative array of sub-components.
- .declared_component_dicts ⇒ Object
- .declared_components ⇒ Object
- .for_all(attribute, &action) ⇒ Object
- .required_attributes ⇒ 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.
114 115 116 117 118 |
# File 'lib/config_mapper/config_struct.rb', line 114 def initialize self.class.for_all(:attribute_initializers) do |name, initializer| instance_variable_set("@#{name}", initializer.call) end end |
Class Method Details
.attribute(name, options = {}) { ... } ⇒ 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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/config_mapper/config_struct.rb', line 24 def attribute(name, = {}) name = name.to_sym required = true default_value = nil if .key?(:default) default_value = .fetch(:default).freeze required = false if default_value.nil? end attribute_initializers[name] = proc { default_value } required_attributes << name if required attr_reader(name) define_method("#{name}=") do |value| if value.nil? raise NoValueProvided if required else value = yield(value) if block_given? end instance_variable_set("@#{name}", value) end end |
.attribute_initializers ⇒ Object
93 94 95 |
# File 'lib/config_mapper/config_struct.rb', line 93 def attribute_initializers @attribute_initializers ||= {} end |
.component(name, options = {}, &block) ⇒ Object
Defines a sub-component.
If a block is be provided, it will be ‘class_eval`ed to define the sub-components class.
54 55 56 57 58 59 60 61 62 |
# File 'lib/config_mapper/config_struct.rb', line 54 def component(name, = {}, &block) name = name.to_sym declared_components << name type = .fetch(:type, ConfigStruct) type = Class.new(type, &block) if block type = type.method(:new) if type.respond_to?(:new) attribute_initializers[name] = type attr_reader name end |
.component_dict(name, options = {}, &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.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/config_mapper/config_struct.rb', line 75 def component_dict(name, = {}, &block) name = name.to_sym declared_component_dicts << name type = .fetch(:type, ConfigStruct) type = Class.new(type, &block) if block type = type.method(:new) if type.respond_to?(:new) key_type = [:key_type] key_type = key_type.method(:new) if key_type.respond_to?(:new) attribute_initializers[name] = lambda do ConfigDict.new(type, key_type) end attr_reader name end |
.declared_component_dicts ⇒ Object
101 102 103 |
# File 'lib/config_mapper/config_struct.rb', line 101 def declared_component_dicts @declared_component_dicts ||= [] end |
.declared_components ⇒ Object
97 98 99 |
# File 'lib/config_mapper/config_struct.rb', line 97 def declared_components @declared_components ||= [] end |
.for_all(attribute, &action) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/config_mapper/config_struct.rb', line 105 def for_all(attribute, &action) ancestors.each do |klass| next unless klass.respond_to?(attribute) klass.public_send(attribute).each(&action) end end |
.required_attributes ⇒ Object
89 90 91 |
# File 'lib/config_mapper/config_struct.rb', line 89 def required_attributes @required_attributes ||= [] end |
Instance Method Details
#config_errors ⇒ Object
124 125 126 |
# File 'lib/config_mapper/config_struct.rb', line 124 def config_errors immediate_config_errors.merge(component_config_errors) end |
#configure_with(attribute_values) ⇒ Hash
Configure with data.
133 134 135 136 |
# File 'lib/config_mapper/config_struct.rb', line 133 def configure_with(attribute_values) errors = ConfigMapper.configure_with(attribute_values, self) config_errors.merge(errors) end |
#immediate_config_errors ⇒ Object
120 121 122 |
# File 'lib/config_mapper/config_struct.rb', line 120 def immediate_config_errors missing_required_attribute_errors end |
#to_h ⇒ Hash
Return the configuration as a Hash.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/config_mapper/config_struct.rb', line 142 def to_h {}.tap do |result| self.class.for_all(:attribute_initializers) do |attr_name, _| value = send(attr_name) if value && value.respond_to?(:to_h) && !value.is_a?(Array) value = value.to_h end result[attr_name.to_s] = value end end end |