Class: Nero::EnvTag
Instance Method Summary collapse
- #deprecated? ⇒ Boolean
-
#initialize(coerce: nil, optional: false, deprecated: nil) ⇒ EnvTag
constructor
A new instance of EnvTag.
- #resolve(args, context:) ⇒ Object
Constructor Details
#initialize(coerce: nil, optional: false, deprecated: nil) ⇒ EnvTag
Returns a new instance of EnvTag.
5 6 7 8 9 |
# File 'lib/nero/env_tag.rb', line 5 def initialize(coerce: nil, optional: false, deprecated: nil) @deprecated = deprecated @coerce = coerce @optional = optional end |
Instance Method Details
#deprecated? ⇒ Boolean
11 |
# File 'lib/nero/env_tag.rb', line 11 def deprecated? = !!@deprecated |
#resolve(args, context:) ⇒ Object
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 38 39 40 41 42 43 44 |
# File 'lib/nero/env_tag.rb', line 13 def resolve(args, context:) if (msg, opts = @deprecated) context.deprecate(msg, **opts) end var_name = args[0] default = args[1] if var_name.nil? context.add_error("!env tags require a var_name, e.g. `!env FOO`, or `!env [FOO, fallback]`") return end raw = context.environ[var_name] if raw.nil? && default.nil? context.add_error("environment variable #{var_name} is not set") unless @optional || context.all_optional? return end # A fallback is a string like an env value. For coercing tags a sequence is passed as is, # so !env/list [X, [a, b]] works (other coercions report it as an error). value = raw || ((default.is_a?(Array) && @coerce) ? default : default.to_s) return value unless @coerce begin @coerce.call(value) rescue ArgumentError, TypeError => e context.add_error("cannot coerce #{var_name}=#{value.inspect}: #{e.}") nil end end |