Module: Synthra::ConfigFile

Defined in:
lib/synthra/config_file.rb

Overview

Configuration File Support

Loads configuration from YAML files following Rails conventions. Supports environment-specific configuration.

Examples:

synthra.yml

default_mode: random
seed: 12345
locale: en
limits:
  max_array_size: 100
  max_recursion: 5

test:
  seed: 42  # Deterministic in tests

development:
  seed: null  # Random in dev

Constant Summary collapse

CONFIG_PATHS =

Default config file locations (searched in order)

[
  "config/synthra.yml",
  "synthra.yml",
  ".synthra.yml"
].freeze

Class Method Summary collapse

Class Method Details

.apply(config) ⇒ void

This method returns an undefined value.

Apply configuration to Synthra

Parameters:

  • config (Hash)

    configuration hash



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/synthra/config_file.rb', line 64

def apply(config)
  Synthra.configure do |c|
    # Core settings
    c.default_mode = config["default_mode"].to_sym if config["default_mode"]
    c.max_unique_retries = config["max_unique_retries"] if config["max_unique_retries"]
    c.allow_eval = config["allow_eval"] if config.key?("allow_eval")
    c.fast_mode = config["fast_mode"] if config.key?("fast_mode")
    
    # Field handling
    c.optional_field_presence_rate = config["optional_field_presence_rate"] if config["optional_field_presence_rate"]
    c.nullable_field_null_rate = config["nullable_field_null_rate"] if config["nullable_field_null_rate"]
    
    # Limits
    if config["limits"]
      c.limits.max_latency_ms = config["limits"]["max_latency_ms"] if config["limits"]["max_latency_ms"]
      c.limits.max_recursion = config["limits"]["max_recursion"] if config["limits"]["max_recursion"]
      c.limits.max_array_size = config["limits"]["max_array_size"] if config["limits"]["max_array_size"]
      c.limits.max_text_length = config["limits"]["max_text_length"] if config["limits"]["max_text_length"]
    end
  end
end

.deep_merge(base, override) ⇒ Object (private)



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/synthra/config_file.rb', line 116

def deep_merge(base, override)
  result = base.dup
  override.each do |key, value|
    if base[key].is_a?(Hash) && value.is_a?(Hash)
      result[key] = deep_merge(base[key], value)
    else
      result[key] = value
    end
  end
  result
end

.determine_environmentObject (private)



108
109
110
# File 'lib/synthra/config_file.rb', line 108

def determine_environment
  ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["SYNTHRA_ENV"] || "development"
end

.find_config_fileString?

Find configuration file in default locations

Returns:

  • (String, nil)

    path to config file or nil



102
103
104
# File 'lib/synthra/config_file.rb', line 102

def find_config_file
  CONFIG_PATHS.find { |p| File.exist?(p) }
end

.known_environmentsObject (private)



112
113
114
# File 'lib/synthra/config_file.rb', line 112

def known_environments
  %w[development test production staging]
end

.load(path = nil, env: nil) ⇒ Hash

Load configuration from file

Parameters:

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

    explicit path or nil to search defaults

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

    environment name (default: RAILS_ENV or RACK_ENV or 'development')

Returns:

  • (Hash)

    configuration hash



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/synthra/config_file.rb', line 40

def load(path = nil, env: nil)
  path ||= find_config_file
  return {} unless path && File.exist?(path)

  env ||= determine_environment
  
  raw_config = YAML.safe_load(
    File.read(path),
    permitted_classes: [Symbol, Date, Time],
    aliases: true
  ) || {}
  
  # Merge base config with environment-specific config
  base_config = raw_config.except(*known_environments)
  env_config = raw_config[env.to_s] || {}
  
  deep_merge(base_config, env_config)
end

.load_and_apply(path = nil, env: nil) ⇒ Hash

Load and apply configuration in one step

Parameters:

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

    config file path

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

    environment

Returns:

  • (Hash)

    loaded configuration



92
93
94
95
96
# File 'lib/synthra/config_file.rb', line 92

def load_and_apply(path = nil, env: nil)
  config = load(path, env: env)
  apply(config) if config.any?
  config
end