Class: ShinseiConfig::ConfigBuilder
- Inherits:
-
Object
- Object
- ShinseiConfig::ConfigBuilder
- Defined in:
- lib/shinsei_config/config_builder.rb
Overview
Recursively builds Data.define objects from hash with deep freezing
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(hash) ⇒ ConfigBuilder
constructor
A new instance of ConfigBuilder.
Constructor Details
#initialize(hash) ⇒ ConfigBuilder
Returns a new instance of ConfigBuilder.
17 18 19 |
# File 'lib/shinsei_config/config_builder.rb', line 17 def initialize(hash) @hash = hash end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
14 15 16 |
# File 'lib/shinsei_config/config_builder.rb', line 14 def hash @hash end |
Class Method Details
.build(hash) ⇒ Object
7 8 9 |
# File 'lib/shinsei_config/config_builder.rb', line 7 def self.build(hash) new(hash).build end |
Instance Method Details
#build ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/shinsei_config/config_builder.rb', line 22 def build raise Error, "Expected Hash, got #{hash.class}" unless hash.is_a?(Hash) # Create a Data.define class with all keys as symbols config_class = Data.define(*hash.keys.map(&:to_sym)) # Build values, recursively converting nested hashes # Convert keys to symbols for Data.define values = hash.transform_keys(&:to_sym).transform_values { |v| build_value(v) } # Create instance and freeze config_class.new(**values).freeze end |