Class: StandardConfig::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_config/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Manager

Returns a new instance of Manager.



7
8
9
10
11
# File 'lib/standard_config/manager.rb', line 7

def initialize(schema)
  @schema = schema
  @providers = Concurrent::Map.new
  @static_configs = Concurrent::Map.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Access configuration scopes via method calls



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/standard_config/manager.rb', line 31

def method_missing(method_name, *args)
  method_str = method_name.to_s
  scope_name = method_str.end_with?("=") ? method_str.chomp("=").to_sym : method_name.to_sym

  # Handle field setter via unique scope resolution
  if method_str.end_with?("=")
    field = scope_name
    scopes = @schema.scopes_with_field(field)
    if scopes.size == 1
      s = scopes.first
      provider = @providers.compute_if_absent(s) do
        ConfigProvider.new(s, -> { create_static_config_for_scope(s) }, @schema)
      end
      provider.public_send(method_name, *args)
      return args.first
    end
  end

  # Handle field getter via unique scope resolution
  scopes = @schema.scopes_with_field(scope_name)
  if scopes.size == 1
    s = scopes.first
    provider = @providers.compute_if_absent(s) do
      ConfigProvider.new(s, -> { create_static_config_for_scope(s) }, @schema)
    end
    return provider.get_field(scope_name)
  end

  # Handle scope access
  provider = @providers[scope_name]
  return provider if provider

  # Create static provider for valid scopes on first access
  if @schema.valid_scope?(scope_name)
    return @providers.compute_if_absent(scope_name) do
      ConfigProvider.new(scope_name, -> { create_static_config_for_scope(scope_name) }, @schema)
    end
  end

  super
end

Instance Method Details

#register(scope_name, resolver_proc) ⇒ Object

Register a configuration provider for a scope



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/standard_config/manager.rb', line 14

def register(scope_name, resolver_proc)
  scope_name = scope_name.to_sym

  # Validate scope exists in schema
  unless @schema.valid_scope?(scope_name)
    raise ArgumentError, "Unknown configuration scope: #{scope_name}. Valid scopes: #{@schema.scopes.keys}"
  end

  @providers[scope_name] = ConfigProvider.new(scope_name, resolver_proc, @schema)
  self
end

#registered?(scope_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/standard_config/manager.rb', line 26

def registered?(scope_name)
  @providers.key?(scope_name.to_sym)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/standard_config/manager.rb', line 73

def respond_to_missing?(method_name, include_private = false)
  method_str = method_name.to_s
  scope_name = method_str.end_with?("=") ? method_str.chomp("=").to_sym : method_name.to_sym
  @schema.valid_scope?(scope_name) ||
    @schema.scopes_with_field(scope_name).any? ||
    super
end