Class: ShinseiConfig::ConfigBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/shinsei_config/config_builder.rb

Overview

Recursively builds Data.define objects from hash with deep freezing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hashObject (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

#buildObject

Raises:



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