Class: Env::Vars
- Inherits:
-
Object
- Object
- Env::Vars
- Defined in:
- lib/env_vars.rb
Constant Summary collapse
- VERSION =
"0.6.0"
- BOOL_TRUE =
["yes", "true", "1", true]
- BOOL_FALSE =
["no", "false"]
- MissingEnvironmentVariable =
Class.new(StandardError)
- MissingCallable =
Class.new(StandardError)
Instance Method Summary collapse
- #array(type = string) ⇒ Object
- #bigdecimal ⇒ Object
- #bool ⇒ Object
- #float ⇒ Object
-
#initialize(env = ENV, &block) ⇒ Vars
constructor
A new instance of Vars.
- #int ⇒ Object
- #mandatory(name, type, aliases: []) ⇒ Object
- #optional(name, type, default = nil, aliases: []) ⇒ Object
- #property(name, func = nil, cache: true, &block) ⇒ Object
- #set(name, type, default = nil, required: false, aliases: []) ⇒ Object
- #string ⇒ Object
- #symbol ⇒ Object
- #validate!(env_var, required) ⇒ Object
Constructor Details
#initialize(env = ENV, &block) ⇒ Vars
Returns a new instance of Vars.
14 15 16 17 |
# File 'lib/env_vars.rb', line 14 def initialize(env = ENV, &block) @env = env instance_eval(&block) end |
Instance Method Details
#array(type = string) ⇒ Object
85 86 87 |
# File 'lib/env_vars.rb', line 85 def array(type = string) [:array, type] end |
#bigdecimal ⇒ Object
81 82 83 |
# File 'lib/env_vars.rb', line 81 def bigdecimal :bigdecimal end |
#bool ⇒ Object
69 70 71 |
# File 'lib/env_vars.rb', line 69 def bool :bool end |
#float ⇒ Object
77 78 79 |
# File 'lib/env_vars.rb', line 77 def float :float end |
#int ⇒ Object
61 62 63 |
# File 'lib/env_vars.rb', line 61 def int :int end |
#mandatory(name, type, aliases: []) ⇒ Object
41 42 43 |
# File 'lib/env_vars.rb', line 41 def mandatory(name, type, aliases: []) set(name, type, required: true, aliases: aliases) end |
#optional(name, type, default = nil, aliases: []) ⇒ Object
45 46 47 |
# File 'lib/env_vars.rb', line 45 def optional(name, type, default = nil, aliases: []) set(name, type, default, aliases: aliases) end |
#property(name, func = nil, cache: true, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/env_vars.rb', line 49 def property(name, func = nil, cache: true, &block) callable = (func || block) raise MissingCallable, "arg[1] must respond to #call or pass a block" unless callable if cache value = callable.call define_singleton_method(name) { value } else define_singleton_method(name) { callable.call } end end |
#set(name, type, default = nil, required: false, aliases: []) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/env_vars.rb', line 19 def set(name, type, default = nil, required: false, aliases: []) name = name.to_s env_var = name.upcase name = "#{name}?" if type == bool validate!(env_var, required) define_singleton_method(name) do return default unless @env.key?(env_var) coerce(type, @env[env_var]) end aliases.each do |alias_name| define_singleton_method(alias_name, method(name)) end end |
#string ⇒ Object
65 66 67 |
# File 'lib/env_vars.rb', line 65 def string :string end |
#symbol ⇒ Object
73 74 75 |
# File 'lib/env_vars.rb', line 73 def symbol :symbol end |
#validate!(env_var, required) ⇒ Object
36 37 38 39 |
# File 'lib/env_vars.rb', line 36 def validate!(env_var, required) return unless required raise MissingEnvironmentVariable, "#{env_var} is not defined" unless @env.key?(env_var) end |