Class: StandardConfig::ConfigProvider

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

Instance Method Summary collapse

Constructor Details

#initialize(scope_name, resolver_proc, schema = nil) ⇒ ConfigProvider

Returns a new instance of ConfigProvider.



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

def initialize(scope_name, resolver_proc, schema = nil)
  @scope_name = scope_name
  @resolver_proc = resolver_proc
  @schema = schema
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/standard_config/config_provider.rb', line 11

def method_missing(method_name, *args)
  if method_name.to_s.end_with?("=")
    # Setter - only works for static configs (OpenStruct objects)
    field_name = method_name.to_s.chomp("=").to_sym
    validate_field!(field_name)

    config_object = @resolver_proc.call
    if config_object.respond_to?(method_name)
      config_object.send(method_name, args.first)
    elsif config_object.respond_to?(:[]=)
      # Support hash-like providers
      value = args.first
      config_object[field_name] = value
      # Also set string key for convenience if symbol not used by provider
      begin
        config_object[field_name.to_s] = value
      rescue StandardError
        # ignore if provider doesn't accept string keys
      end
    else
      raise NoMethodError, "Configuration object doesn't support setting #{field_name}"
    end
  else
    # Getter
    get_field(method_name)
  end
end

Instance Method Details

#get_field(field_name) ⇒ Object



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
# File 'lib/standard_config/config_provider.rb', line 39

def get_field(field_name)
  validate_field!(field_name)

  config_object = @resolver_proc.call
  raw_value = if config_object.respond_to?(field_name)
                config_object.send(field_name)
  elsif config_object.respond_to?(:[])
                config_object[field_name] || config_object[field_name.to_s]
  else
                nil
  end

  # Cast the value according to schema
  field_def = @schema&.field_definition(@scope_name, field_name)
  return raw_value unless field_def

  casted = @schema&.cast_value(raw_value, field_def.type) || raw_value
  # Return dup for mutable structures to prevent accidental mutation of shared defaults
  if casted.is_a?(Array)
    casted.dup
  elsif casted.is_a?(Hash)
    casted.dup
  else
    casted
  end
end

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

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/standard_config/config_provider.rb', line 66

def respond_to_missing?(method_name, include_private = false)
  field_name = method_name.to_s.end_with?("=") ? method_name.to_s.chomp("=").to_sym : method_name.to_sym
  @schema&.valid_field?(@scope_name, field_name) || super
end