Module: ConfigurableFromEnv::ClassMethods

Defined in:
lib/configurable_from_env.rb

Instance Method Summary collapse

Instance Method Details

#config_accessor(*attributes, from_env: nil, **options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/configurable_from_env.rb', line 15

def config_accessor(*attributes, from_env: nil, **options, &)
  if from_env && attributes.many?
    raise ArgumentError, "Only one accessor at a time can be created using the :from_env option"
  end

  env_value = EnvironmentValue.from(from_env)
  accessor = super(*attributes, **options, &)
  default_provided = options.key?(:default) || block_given?

  env_value&.read(required: !default_provided) do |value|
    public_send(:"#{attributes.first}=", value)
  end

  accessor
end