Class: BBK::Utils::Config
- Inherits:
-
Object
- Object
- BBK::Utils::Config
- Defined in:
- lib/bbk/utils/config.rb
Defined Under Namespace
Classes: BooleanCaster, KeyError
Constant Summary collapse
- PREFIX_SEP =
'_'
Instance Attribute Summary collapse
-
#env_prefix ⇒ Object
readonly
Returns the value of attribute env_prefix.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#store ⇒ Object
Returns the value of attribute store.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #as_json(*_args) ⇒ Object
- #content(key) ⇒ Object
- #fetch(key, default = nil) ⇒ Object
-
#initialize(name: nil, prefix: nil, parent: nil) ⇒ Config
constructor
A new instance of Config.
- #map(env, file, required: true, desc: nil, bool: false, key: nil, rewrite: true) ⇒ Object
- #optional(env, default: nil, desc: nil, bool: false, type: nil, key: nil, rewrite: true) ⇒ Object
- #require(env, desc: nil, bool: false, type: nil, key: nil, rewrite: true) ⇒ Object
- #root? ⇒ Boolean
- #run!(source = ENV) ⇒ Object
- #subconfig(prefix:, name: nil) {|sub| ... } ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_s ⇒ Object
- #to_yaml(*_args) ⇒ Object
Constructor Details
#initialize(name: nil, prefix: nil, parent: nil) ⇒ Config
Returns a new instance of Config.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bbk/utils/config.rb', line 49 def initialize(name: nil, prefix: nil, parent: nil) @name = name @store = {} @parent = parent @subconfigs = [] @prefix = normalize_key(prefix) @prefixes = if parent.nil? [@prefix] else parent.prefixes.dup + [@prefix] end.compact @env_prefix = normalize_key(@prefixes.join(PREFIX_SEP)) end |
Instance Attribute Details
#env_prefix ⇒ Object (readonly)
Returns the value of attribute env_prefix.
10 11 12 |
# File 'lib/bbk/utils/config.rb', line 10 def env_prefix @env_prefix end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/bbk/utils/config.rb', line 9 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/bbk/utils/config.rb', line 10 def parent @parent end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
10 11 12 |
# File 'lib/bbk/utils/config.rb', line 10 def prefix @prefix end |
#store ⇒ Object
Returns the value of attribute store.
9 10 11 |
# File 'lib/bbk/utils/config.rb', line 9 def store @store end |
Class Method Details
.instance(prefix: nil) ⇒ Object
37 38 39 |
# File 'lib/bbk/utils/config.rb', line 37 def self.instance(prefix: nil) @instance ||= new(prefix: prefix) end |
Instance Method Details
#[](key) ⇒ Object
129 130 131 |
# File 'lib/bbk/utils/config.rb', line 129 def [](key) self.get(key, search_up: true, search_down: true)[:value] end |
#[]=(key, value) ⇒ Object
133 134 135 |
# File 'lib/bbk/utils/config.rb', line 133 def []=(key, value) @store[normalize_key(key)][:value] = value end |
#as_json(*_args) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/bbk/utils/config.rb', line 174 def as_json(*_args) values = store_with_subconfigs.values.sort_by do |item| [item[:file].present? ? 0 : 1, item[:required] ? 0 : 1] end.reduce({}) do |ret, item| ret.merge(item[:env] => item) end @name ? { @name => values } : values end |
#content(key) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/bbk/utils/config.rb', line 137 def content(key) item = @store[normalize_key(key)] if (file = item[:file]) File.read(file) else item[:value] end end |
#fetch(key, default = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/bbk/utils/config.rb', line 146 def fetch(key, default = nil) if (rec = self.get(key, search_up: true, search_down: true)) && rec.key?(:value) rec[:value] else default end rescue KeyError default end |
#map(env, file, required: true, desc: nil, bool: false, key: nil, rewrite: true) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bbk/utils/config.rb', line 63 def map(env, file, required: true, desc: nil, bool: false, key: nil, rewrite: true) conf_key = full_prefixed_key(env) return if @store.key?(conf_key) && !rewrite @store[conf_key] = { env: full_prefixed_key(key || env), file: file, required: required, desc: desc, bool: bool, type: nil } end |
#optional(env, default: nil, desc: nil, bool: false, type: nil, key: nil, rewrite: true) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/bbk/utils/config.rb', line 94 def optional(env, default: nil, desc: nil, bool: false, type: nil, key: nil, rewrite: true) raise ArgumentError.new('Specified type and bool') if bool && type.present? type = BBK::Utils::Config::BooleanCaster.singleton_method(:cast) if bool conf_key = full_prefixed_key(env) return if @store.key?(conf_key) && !rewrite @store[conf_key] = { env: full_prefixed_key(key || env), file: nil, required: false, default: default, desc: desc, bool: true, type: type } end |
#require(env, desc: nil, bool: false, type: nil, key: nil, rewrite: true) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bbk/utils/config.rb', line 77 def require(env, desc: nil, bool: false, type: nil, key: nil, rewrite: true) raise ArgumentError.new('Specified type and bool') if bool && type.present? type = BBK::Config::BooleanCaster.singleton_method(:cast) if bool conf_key = full_prefixed_key(env) return if @store.key?(conf_key) && !rewrite @store[conf_key] = { env: full_prefixed_key(key || env), file: nil, required: true, desc: desc, bool: bool, type: type } end |
#root? ⇒ Boolean
192 193 194 |
# File 'lib/bbk/utils/config.rb', line 192 def root? @parent.nil? end |
#run!(source = ENV) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/bbk/utils/config.rb', line 112 def run!(source = ENV) @store.each_value do |item| process(source, item) end @subconfigs.each {|sub| sub.run!(source) } nil end |
#subconfig(prefix:, name: nil) {|sub| ... } ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/bbk/utils/config.rb', line 120 def subconfig(prefix:, name: nil) raise ArgumentError.new("Subconfig with prefix #{prefix} already exists") if @subconfigs.any? {|sub| sub.prefix == prefix.to_s } sub = self.class.new(name: name, prefix: prefix, parent: self) @subconfigs << sub yield sub if block_given? sub end |
#to_json(*_args) ⇒ Object
184 185 186 |
# File 'lib/bbk/utils/config.rb', line 184 def to_json(*_args) JSON.pretty_generate(as_json) end |
#to_s ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bbk/utils/config.rb', line 156 def to_s result = StringIO.new result.puts "Environment variables#{@name ? " for #{@name}" : ''}:" padding = ' ' * 3 sorted = store_with_subconfigs.values.sort_by do |item| [item[:file].present? ? 0 : 1, item[:required] ? 0 : 1] end sorted.each do |item| if item[:file] result.puts print_file_item(item, padding) else result.puts print_item(item, padding) end end result.string end |
#to_yaml(*_args) ⇒ Object
188 189 190 |
# File 'lib/bbk/utils/config.rb', line 188 def to_yaml(*_args) JSON.parse(to_json).to_yaml end |