Class: SuperConfig::Base
- Inherits:
-
Object
- Object
- SuperConfig::Base
- Defined in:
- lib/superconfig.rb
Constant Summary collapse
- BOOL_TRUE =
["yes", "true", "1", true].freeze
- BOOL_FALSE =
%w[no false].freeze
Instance Method Summary collapse
- #array(type = string) ⇒ Object
- #bigdecimal ⇒ Object
- #bool ⇒ Object
- #credential(name, &block) ⇒ Object
- #float ⇒ Object
-
#initialize(env: ENV, raise_exception: true, stderr: $stderr, &block) ⇒ Base
constructor
A new instance of Base.
- #int ⇒ Object
- #json ⇒ Object
- #mandatory(name, type, aliases: [], description: nil) ⇒ Object
- #optional(name, type, default = nil, aliases: [], description: nil) ⇒ Object
-
#property(name, func = nil, cache: true, description: nil, &block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #report ⇒ Object
- #set(name, value) ⇒ Object
- #string ⇒ Object
- #symbol ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #validate!(env_var, required, description) ⇒ Object
Constructor Details
#initialize(env: ENV, raise_exception: true, stderr: $stderr, &block) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 23 24 |
# File 'lib/superconfig.rb', line 17 def initialize(env: ENV, raise_exception: true, stderr: $stderr, &block) @env = env @raise_exception = raise_exception @stderr = stderr @attributes = {} @__cache__ = {} instance_eval(&block) end |
Instance Method Details
#array(type = string) ⇒ Object
116 117 118 |
# File 'lib/superconfig.rb', line 116 def array(type = string) [:array, type] end |
#bigdecimal ⇒ Object
111 112 113 114 |
# File 'lib/superconfig.rb', line 111 def bigdecimal require "bigdecimal" :bigdecimal end |
#bool ⇒ Object
99 100 101 |
# File 'lib/superconfig.rb', line 99 def bool :bool end |
#credential(name, &block) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/superconfig.rb', line 82 def credential(name, &block) define_singleton_method(name) do @__cache__[:"_credential_#{name}"] ||= begin value = Rails.application.credentials.fetch(name) block ? block.call(value) : value # rubocop:disable Performance/RedundantBlockCall end end end |
#float ⇒ Object
107 108 109 |
# File 'lib/superconfig.rb', line 107 def float :float end |
#int ⇒ Object
91 92 93 |
# File 'lib/superconfig.rb', line 91 def int :int end |
#json ⇒ Object
120 121 122 |
# File 'lib/superconfig.rb', line 120 def json :json end |
#mandatory(name, type, aliases: [], description: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/superconfig.rb', line 46 def mandatory(name, type, aliases: [], description: nil) assign( name, type, required: true, aliases:, description: ) end |
#optional(name, type, default = nil, aliases: [], description: nil) ⇒ Object
56 57 58 |
# File 'lib/superconfig.rb', line 56 def optional(name, type, default = nil, aliases: [], description: nil) assign(name, type, default, aliases:, description:) end |
#property(name, func = nil, cache: true, description: nil, &block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/superconfig.rb', line 66 def property(name, func = nil, cache: true, description: nil, &block) # rubocop:disable Lint/UnusedMethodArgument callable = func || block unless callable raise MissingCallable, "arg[1] must respond to #call or pass a block" end if cache define_singleton_method(name) do @__cache__[name.to_sym] ||= callable.call end else define_singleton_method(name) { callable.call } end end |
#report ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/superconfig.rb', line 124 def report attrs = @attributes.sort report = attrs.each_with_object([]) do |(env_var, info), buffer| icon, = if @env.key?(env_var) ["✅", "is set"] elsif info[:required] ["❌", "is not set"] elsif !info[:required] && !info[:default].nil? ["✅", "is not set, but has default value"] else ["⚠️", "is not set"] end label = if info[:required] "mandatory" else "optional" end buffer << [icon, env_var, , "(#{label})"].join(" ") end "#{report.join("\n")}\n" end |
#set(name, value) ⇒ Object
60 61 62 63 64 |
# File 'lib/superconfig.rb', line 60 def set(name, value) silence_warnings do property(name) { value } end end |
#string ⇒ Object
95 96 97 |
# File 'lib/superconfig.rb', line 95 def string :string end |
#symbol ⇒ Object
103 104 105 |
# File 'lib/superconfig.rb', line 103 def symbol :symbol end |
#to_s ⇒ Object Also known as: inspect
26 27 28 |
# File 'lib/superconfig.rb', line 26 def to_s "#<SuperConfig>" end |
#validate!(env_var, required, description) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/superconfig.rb', line 31 def validate!(env_var, required, description) return unless required return if @env.key?(env_var) = env_var.to_s << " (#{description})" if description << " is not defined." raise MissingEnvironmentVariable, if @raise_exception = "[SUPERCONF] #{}" = "\e[31m#{}\e[0m" if @stderr.tty? @stderr << << "\n" end |