Class: ComplexConfig::Proxy

Inherits:
BasicObject
Defined in:
lib/complex_config/proxy.rb

Overview

A proxy class that provides dynamic configuration access with lazy evaluation

The Proxy class acts as a wrapper around configuration access, deferring the actual configuration loading until a method is first called. It supports environment-specific configuration lookups and can handle both direct configuration access and existence checks.

lookups

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Proxy

The proxy object’s initialization method sets up the environment for configuration access.

Parameters:

  • env (String, nil) (defaults to: nil)

    The environment name to use for configuration lookups, defaults to nil which will use the default environment



18
19
20
# File 'lib/complex_config/proxy.rb', line 18

def initialize(env = nil)
  @env = env
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

The method_missing method handles dynamic configuration access and validation.

Parameters:

  • name (Symbol)

    The name of the method being called

  • args (Array)

    Arguments passed to the method

Returns:

  • (Object)

    The result of the dynamic configuration lookup or method call



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/complex_config/proxy.rb', line 52

def method_missing(name, *args)
  if name =~ /\?\z/
    method_name, name = name, $`
    exist = ::ComplexConfig::Provider.exist?(name)
    (class << self; self; end).class_eval do
      define_method(method_name) do |env = nil|
        if exist
          __send__(name, *args)
        else
          nil
        end
      end
    end
    __send__(method_name, *args)
  else
    config = ::ComplexConfig::Provider[name]
    (class << self; self; end).class_eval do
      define_method(name) do |env = nil|
        if env ||= @env
          config[env] || ::ComplexConfig::Settings.new
        else
          config
        end
      end
    end
    __send__(name, *args)
  end
end

Instance Attribute Details

#envString? (readonly)

The environment name used for configuration

Returns:

  • (String, nil)

    the current value of env



12
13
14
# File 'lib/complex_config/proxy.rb', line 12

def env
  @env
end

Instance Method Details

#inspectString

The inspect method returns a string representation of the proxy object.

Returns:

  • (String)

    a string representation in the format “#<ComplexConfig::Proxy>”



32
33
34
# File 'lib/complex_config/proxy.rb', line 32

def inspect
  "#<#{to_s}>"
end

#reloadComplexConfig::Proxy

The reload method flushes the configuration cache and returns the receiver.

Returns:



40
41
42
43
# File 'lib/complex_config/proxy.rb', line 40

def reload
  ::ComplexConfig::Provider.flush_cache
  self
end

#to_sString

The to_s method returns a string representation of the proxy object.

Returns:

  • (String)

    the string ‘ComplexConfig::Proxy’



25
26
27
# File 'lib/complex_config/proxy.rb', line 25

def to_s
  'ComplexConfig::Proxy'
end