Class: Rogalik::Configuration::BaseConfig
- Inherits:
-
Object
- Object
- Rogalik::Configuration::BaseConfig
- Defined in:
- lib/rogalik/configuration.rb
Overview
A lightweight dynamic configuration struct backed by a Hash.
Subclasses (or instances used directly) gain arbitrary getter/setter pairs without declaring each attribute explicitly. Useful for ad-hoc config namespaces where the key set is not known ahead of time.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(settings = {}) ⇒ BaseConfig
constructor
A new instance of BaseConfig.
- #inspect ⇒ String
-
#method_missing(name, *args, &block) ⇒ Object
Supports
key(getter) andkey=(setter) for any symbol name. - #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash{Symbol => Object}
Returns the underlying settings as a plain Hash.
-
#to_json(*args) ⇒ String
JSON representation of the settings hash.
- #to_s ⇒ String
Constructor Details
#initialize(settings = {}) ⇒ BaseConfig
Returns a new instance of BaseConfig.
38 39 40 |
# File 'lib/rogalik/configuration.rb', line 38 def initialize(settings = {}) @settings = settings.transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Supports key (getter) and key= (setter) for any symbol name.
43 44 45 46 47 48 49 50 51 |
# File 'lib/rogalik/configuration.rb', line 43 def method_missing(name, *args, &block) key = name.to_s.delete_suffix("=").to_sym if name.to_s.end_with?("=") @settings[key] = args.first else @settings.fetch(key, nil) end end |
Instance Method Details
#inspect ⇒ String
78 79 80 |
# File 'lib/rogalik/configuration.rb', line 78 def inspect "#<#{self.class.name} #{@settings.inspect}>" end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
55 56 57 58 |
# File 'lib/rogalik/configuration.rb', line 55 def respond_to_missing?(name, include_private = false) key = name.to_s.delete_suffix("=").to_sym @settings.key?(key) || name.to_s.end_with?("=") || super end |
#to_h ⇒ Hash{Symbol => Object}
Returns the underlying settings as a plain Hash.
63 64 65 |
# File 'lib/rogalik/configuration.rb', line 63 def to_h @settings.dup end |
#to_json(*args) ⇒ String
Returns JSON representation of the settings hash.
68 69 70 |
# File 'lib/rogalik/configuration.rb', line 68 def to_json(*args) @settings.to_json(*args) end |
#to_s ⇒ String
73 74 75 |
# File 'lib/rogalik/configuration.rb', line 73 def to_s @settings.to_s end |