Module: ENVied::Configurable::ClassMethods
- Defined in:
- lib/envied.rb
Instance Method Summary collapse
- #default_if?(cond, value = nil, &block) ⇒ Boolean
- #default_unless?(cond, value = nil, &block) ⇒ Boolean
-
#parse_env(env) ⇒ Object
Creates a configuration instance from env.
- #variable(name, type = :String, options = {}) ⇒ Object
Instance Method Details
#default_if?(cond, value = nil, &block) ⇒ Boolean
65 66 67 68 |
# File 'lib/envied.rb', line 65 def default_if?(cond, value = nil, &block) = { default: (value || block) } cond ? : Hash.new end |
#default_unless?(cond, value = nil, &block) ⇒ Boolean
70 71 72 |
# File 'lib/envied.rb', line 70 def default_unless?(cond, value = nil, &block) default_if?(!cond, value, &block) end |
#parse_env(env) ⇒ Object
Creates a configuration instance from env.
Will raise VariableMissingError for variables not present in ENV.
Will raise VariableTypeError for variables whose ENV-value can’t be coerced to the configured type.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/envied.rb', line 45 def parse_env(env) atts = attribute_set.map(&:name).each_with_object({}) do |name, result| variable = attribute_set[name] default = variable.[:default] value = env[name.to_s] || env[name.to_s.upcase] if !(value || default) raise VariableMissingError, variable end try_coercion(variable, value, default) result[name] = value if value end new(atts) end |
#variable(name, type = :String, options = {}) ⇒ Object
61 62 63 |
# File 'lib/envied.rb', line 61 def variable(name, type = :String, = {}) attribute name, type, { strict: true }.merge() end |