Class: SuperConfig::Base

Inherits:
Object
  • Object
show all
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

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



114
115
116
# File 'lib/superconfig.rb', line 114

def array(type = string)
  [:array, type]
end

#bigdecimalObject



109
110
111
112
# File 'lib/superconfig.rb', line 109

def bigdecimal
  require "bigdecimal"
  :bigdecimal
end

#boolObject



97
98
99
# File 'lib/superconfig.rb', line 97

def bool
  :bool
end

#credential(name, &block) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/superconfig.rb', line 80

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

#floatObject



105
106
107
# File 'lib/superconfig.rb', line 105

def float
  :float
end

#intObject



89
90
91
# File 'lib/superconfig.rb', line 89

def int
  :int
end

#jsonObject



118
119
120
# File 'lib/superconfig.rb', line 118

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/superconfig.rb', line 64

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

#reportObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/superconfig.rb', line 122

def report
  attrs = @attributes.sort

  report = attrs.each_with_object([]) do |(env_var, info), buffer|
    icon, message = 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, message, "(#{label})"].join(" ")
  end

  "#{report.join("\n")}\n"
end

#set(name, value) ⇒ Object



60
61
62
# File 'lib/superconfig.rb', line 60

def set(name, value)
  property(name) { value }
end

#stringObject



93
94
95
# File 'lib/superconfig.rb', line 93

def string
  :string
end

#symbolObject



101
102
103
# File 'lib/superconfig.rb', line 101

def symbol
  :symbol
end

#to_sObject 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)

  message = env_var.to_s
  message << " (#{description})" if description
  message << " is not defined."

  raise MissingEnvironmentVariable, message if @raise_exception

  message = "[SUPERCONF] #{message}"
  message = "\e[31m#{message}\e[0m" if @stderr.tty?
  @stderr << message << "\n"
end