Class: EnvValidator::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/env_validator/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



47
48
49
50
51
52
# File 'lib/env_validator/configuration.rb', line 47

def initialize
  @rules = {}
  @environments = {}
  @groups = {}
  @current_env = detect_environment
end

Instance Attribute Details

#current_envObject (readonly)

Returns the value of attribute current_env.



45
46
47
# File 'lib/env_validator/configuration.rb', line 45

def current_env
  @current_env
end

#environmentsObject (readonly)

Returns the value of attribute environments.



45
46
47
# File 'lib/env_validator/configuration.rb', line 45

def environments
  @environments
end

#groupsObject (readonly)

Returns the value of attribute groups.



45
46
47
# File 'lib/env_validator/configuration.rb', line 45

def groups
  @groups
end

#rulesObject (readonly)

Returns the value of attribute rules.



45
46
47
# File 'lib/env_validator/configuration.rb', line 45

def rules
  @rules
end

Instance Method Details

#applicable_rulesObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/env_validator/configuration.rb', line 72

def applicable_rules
  rules = @rules.values.dup

  # Apply environment-specific rules if they exist
  if @environments[@current_env]
    env_config = Configuration.new
    env_config.instance_eval(&@environments[@current_env])
    rules.concat(env_config.rules.values)
  end

  rules
end

#clear!Object



85
86
87
88
89
# File 'lib/env_validator/configuration.rb', line 85

def clear!
  @rules.clear
  @environments.clear
  @groups.clear
end

#environment(env_name, &block) ⇒ Object



64
65
66
# File 'lib/env_validator/configuration.rb', line 64

def environment(env_name, &block)
  @environments[env_name.to_sym] = block
end

#group(group_name, &block) ⇒ Object



68
69
70
# File 'lib/env_validator/configuration.rb', line 68

def group(group_name, &block)
  @groups[group_name.to_sym] = block
end

#optional(name, **options, &block) ⇒ Object



59
60
61
62
# File 'lib/env_validator/configuration.rb', line 59

def optional(name, **options, &block)
  custom_validator = block_given? ? block : nil
  @rules[name.to_sym] = Rule.new(name, required: false, custom_validator: custom_validator, **options)
end

#required(name, **options, &block) ⇒ Object



54
55
56
57
# File 'lib/env_validator/configuration.rb', line 54

def required(name, **options, &block)
  custom_validator = block_given? ? block : nil
  @rules[name.to_sym] = Rule.new(name, required: true, custom_validator: custom_validator, **options)
end