Module: ConfigHelper
- Included in:
- Core::Config
- Defined in:
- lib/svcbase/config/config_helper.rb
Overview
helpers module to DRY out config fetches with optional default
-
iterates through args looking for first symbol that is present.
-
non-symbols are treated as literals instead of lookups (so only one matters)
-
raises an error if value is present but not valid for type (Integer or Float)
-
raises an error if value is missing and no default specified
-
if supplied, default must either be valid for type or nil
Instance Method Summary collapse
- #get_b!(*args) ⇒ Object
- #get_f!(*args) ⇒ Object
- #get_i!(*args) ⇒ Object
- #get_s!(*args) ⇒ Object
- #get_x!(*args) ⇒ Object
Instance Method Details
#get_b!(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/svcbase/config/config_helper.rb', line 38 def get_b!(*args) get_x!(*args) do |v| return v if [true, false].include?(v) raise ArgumentError, 'not a bool or string' unless v.is_a? String case v.downcase when 'true' then true when 'false' then false else raise ArgumentError, 'not true or false' end end end |
#get_f!(*args) ⇒ Object
30 31 32 |
# File 'lib/svcbase/config/config_helper.rb', line 30 def get_f!(*args) get_x!(*args) { |v| Float(v) } end |
#get_i!(*args) ⇒ Object
26 27 28 |
# File 'lib/svcbase/config/config_helper.rb', line 26 def get_i!(*args) get_x!(*args) { |v| Integer(v) } end |
#get_s!(*args) ⇒ Object
34 35 36 |
# File 'lib/svcbase/config/config_helper.rb', line 34 def get_s!(*args) get_x!(*args, &:to_s) end |
#get_x!(*args) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/svcbase/config/config_helper.rb', line 11 def get_x!(*args) while args.length.positive? k = args.shift if k.is_a? Symbol k = k.to_s return yield(self[k]) if key? k else raise 'default not last argument' unless args.empty? return nil if k.nil? return yield(k) end end raise 'value not found' end |