Class: Tasker::Configuration::ConfigurationProxy
- Inherits:
-
Object
- Object
- Tasker::Configuration::ConfigurationProxy
- Defined in:
- lib/tasker/configuration.rb
Overview
Simple proxy object for configuration blocks that provides dot-notation access to hash values without the overhead of OpenStruct.
This enables clean configuration syntax like: config.auth do |auth| auth.authentication_enabled = true auth.authenticator_class = 'MyAuth' end
While maintaining the benefits of dry-struct validation and immutability.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(hash = {}) ⇒ ConfigurationProxy
constructor
Initialize a new configuration proxy with hash data.
-
#method_missing(method_name, *args) ⇒ Object
Handle dynamic method calls for configuration access.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if the proxy responds to a given method.
-
#to_h ⇒ Hash
Convert the proxy back to a hash for dry-struct creation.
Constructor Details
#initialize(hash = {}) ⇒ ConfigurationProxy
Initialize a new configuration proxy with hash data
26 27 28 |
# File 'lib/tasker/configuration.rb', line 26 def initialize(hash = {}) @hash = hash.transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Handle dynamic method calls for configuration access
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tasker/configuration.rb', line 34 def method_missing(method_name, *args) if method_name.to_s.end_with?('=') # Setter: config.authentication_enabled = true key = method_name.to_s.chomp('=').to_sym @hash[key] = args.first elsif @hash.key?(method_name.to_sym) # Getter: config.authentication_enabled @hash[method_name.to_sym] else super end end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if the proxy responds to a given method
52 53 54 |
# File 'lib/tasker/configuration.rb', line 52 def respond_to_missing?(method_name, include_private = false) method_name.to_s.end_with?('=') || @hash.key?(method_name.to_sym) || super end |
#to_h ⇒ Hash
Convert the proxy back to a hash for dry-struct creation
59 60 61 |
# File 'lib/tasker/configuration.rb', line 59 def to_h @hash end |