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.



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

def initialize(schema)
  @schema = schema
  @providers = {}
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



29
30
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
# File 'lib/standard_config/manager.rb', line 29

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
      register(s, -> { create_static_config_for_scope(s) }) unless @providers.key?(s)
      @providers[s].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
    register(s, -> { create_static_config_for_scope(s) }) unless @providers.key?(s)
    return @providers[s].get_field(scope_name)
  end

  # Handle scope access
  if @providers.key?(scope_name)
    return @providers[scope_name]
  end

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

  super
end

Instance Method Details

#register(scope_name, resolver_proc) ⇒ Object

Register a configuration provider for a scope



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

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)


24
25
26
# File 'lib/standard_config/manager.rb', line 24

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

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

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/standard_config/manager.rb', line 67

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