Module: ValidEnv::DslMethods

Included in:
ValidEnv
Defined in:
lib/valid-env/dsl_methods.rb

Overview

DslMethods houses the DSL methods for registering ENV variables that the application knows about.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Override method_missing to perform a look-up on ValidEnv.instance. Every method_missing call is assumed to be an ENV var lookup.

ValidEnv.foo_enabled? -> ValidEnv.instance.foo_enabled?

If ValidEnv.instance does not respond to the given method a MissingEnvVarRegistration error will be raised.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/valid-env/dsl_methods.rb', line 66

def method_missing(method, *args, &blk)
  if instance.respond_to?(method)
    instance.send(method, *args, &blk)
  else
    method = method.to_s
    fail MissingEnvVarRegistration, "      undefined method \#{method.inspect} for \#{self}. Is the\n      \#{method.upcase} env var registered in \#{self}?\n    ERROR_MSG\n  end\nend\n".strip_heredoc

Instance Method Details

#instanceObject

Expose a singleton-like interface for accessing an instance of ValidEnv since we only need one.

Note: Don’t use Singleton from Ruby standard library since that prevents us from creating ValidEnv instances in corresponding specs/tests.



10
11
12
# File 'lib/valid-env/dsl_methods.rb', line 10

def instance
  @instance ||= new
end

#optional(key, type = nil, default: nil) ⇒ Object

optional registers an optional ENV var with the given key.

Examples

optional :FOO
optional :FOO, :boolean, default: false


20
21
22
23
24
25
26
27
# File 'lib/valid-env/dsl_methods.rb', line 20

def optional(key, type = nil, default: nil)
  optional_env_var = OptionalEnvVar.new(
    key,
    type,
    default: default
  )
  register_env_var(optional_env_var)
end

#registered_env_varsObject

Returns the registered list of environment variables.



79
80
81
# File 'lib/valid-env/dsl_methods.rb', line 79

def registered_env_vars
  @registered_env_vars = @registered_env_vars || {}
end

#required(key, *args) ⇒ Object

required registers a required ENV var with the given key. It supports ActiveModel#validate options.

Examples

required :BAR
required :BAR, :boolean
required :BAR, :boolean, if: :some_other_value?


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/valid-env/dsl_methods.rb', line 38

def required(key, *args)
  options = args.extract_options!
  type = args.first
  default_value = options[:default]
  if_method = options[:if]

  additional_details = "if #{if_method}" if if_method
  required_env_var = RequiredEnvVar.new(
    key,
    type,
    default: default_value,
    additional_details: additional_details
  )
  register_env_var(required_env_var)

  validation_args = required_env_var.boolean? ? { boolean: true } : { presence: true }

  validation_args[:if] = if_method if if_method
  validates key, **validation_args
end