module Configerator
Public Instance Methods
array(method = nil)
click to toggle source
bool()
click to toggle source
# File lib/configerator/configerator.rb, line 42 def bool ->(v) { v.to_s=='true'} end
float()
click to toggle source
# File lib/configerator/configerator.rb, line 38 def float ->(v) { v.to_f } end
int()
click to toggle source
Scope methods
# File lib/configerator/configerator.rb, line 34 def int ->(v) { v.to_i } end
namespace(namespace, prefix: true) { || ... }
click to toggle source
# File lib/configerator/configerator.rb, line 21 def namespace namespace, prefix: true, &block @processed = [] @prefix = "#{namespace}_" if prefix yield instance_eval "def #{namespace}?; !!(#{@processed.join(' && ')}) end", __FILE__, __LINE__ ensure @prefix = nil @processed = [] end
optional(name, method=nil)
click to toggle source
# File lib/configerator/configerator.rb, line 11 def optional(name, method=nil) value = cast(fetch_env(name), method) create(name, value) end
override(name, default, method=nil)
click to toggle source
# File lib/configerator/configerator.rb, line 16 def override(name, default, method=nil) value = cast(fetch_env(name) || default, method) create(name, value) end
required(name, method=nil, error_on_load: true)
click to toggle source
Initializers (DSL)
# File lib/configerator/configerator.rb, line 5 def required(name, method=nil, error_on_load: true) value = cast(fetch_env(name, error_on_load: error_on_load), method) create(name, value, error_on_load) end
string()
click to toggle source
# File lib/configerator/configerator.rb, line 46 def string nil end
symbol()
click to toggle source
# File lib/configerator/configerator.rb, line 50 def symbol ->(v) { v.to_sym } end
url()
click to toggle source
# File lib/configerator/configerator.rb, line 54 def url ->(v) { v && URI.parse(v) } end
Private Instance Methods
build_key(key)
click to toggle source
# File lib/configerator/configerator.rb, line 91 def build_key(key) key = "#{@prefix}#{key}" if @prefix key.to_s.upcase end
cast(value, method)
click to toggle source
Helpers
# File lib/configerator/configerator.rb, line 75 def cast(value, method) method ? method.call(value) : value end
create(name, value, error_on_load=true)
click to toggle source
# File lib/configerator/configerator.rb, line 79 def create(name, value, error_on_load=true) name = build_key(name).downcase instance_variable_set(:"@#{name}", value) instance_eval "def #{name}; @#{name} || (raise 'key not set: \"#{name.upcase}\"' unless #{error_on_load}) end", __FILE__, __LINE__ instance_eval "def #{name}?; !!#{name} end", __FILE__, __LINE__ @processed ||= [] @processed << name end
fetch_env(key, error_on_load: false)
click to toggle source
# File lib/configerator/configerator.rb, line 97 def fetch_env(key, error_on_load: false) key = build_key(key) error_on_load ? ENV.fetch(key) : ENV[key] end