module Envee

Adds casting fetchers and validation for values filled with a placeholder.

Version

Constants

VERSION

Public Instance Methods

bool(*args, &block) click to toggle source
# File lib/envee.rb, line 60
def bool(*args, &block)
  value = fetch(*args, &block)

  value && value !~ /\A(0|no|false|off|nil|null|)\z/i
rescue KeyError
  raise MissingValueError.new(args.first)
end
fl(*args, &block) click to toggle source
# File lib/envee.rb, line 36
def fl(*args, &block)
  Float(fetch(*args, &block))
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'Float')
rescue KeyError
  raise MissingValueError.new(args.first)
end
int(*args, &block) click to toggle source
# File lib/envee.rb, line 28
def int(*args, &block)
  Integer(fetch(*args, &block))
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'Integer')
rescue KeyError
  raise MissingValueError.new(args.first)
end
int_time(*args, &block) click to toggle source
# File lib/envee.rb, line 78
def int_time(*args, &block)
  value = Integer(fetch(*args, &block))
  return value if value.is_a?(Time)
  Time.at(value).utc
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'Integer')
rescue KeyError
  raise MissingValueError.new(args.first)
end
str(*args, &block) click to toggle source
# File lib/envee.rb, line 44
def str(*args, &block)
  String(fetch(*args, &block))
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'String')
rescue KeyError
  raise MissingValueError.new(args.first)
end
sym(*args, &block) click to toggle source
# File lib/envee.rb, line 52
def sym(*args, &block)
  String(fetch(*args, &block)).to_sym
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'Symbol')
rescue KeyError
  raise MissingValueError.new(args.first)
end
time(*args, &block) click to toggle source
# File lib/envee.rb, line 68
def time(*args, &block)
  value = fetch(*args, &block)
  return value if value.is_a?(Time)
  Time.parse(value).utc
rescue ArgumentError
  raise UncoercibleError.new(args.first, 'Time')
rescue KeyError
  raise MissingValueError.new(args.first)
end
validate!(options = {}) click to toggle source
# File lib/envee.rb, line 88
def validate!(options = {})
  missing = options[:placeholder] || 'CHANGEME'
  missing_keys = select{|_k, v| v.include?(missing)}.map(&:first)
  raise MissingValuesError, missing_keys unless missing_keys.empty?
end