lib/figaro/env.rb in figaro-0.7.0 vs lib/figaro/env.rb in figaro-1.0.0.rc1
- old
+ new
@@ -1,16 +1,45 @@
module Figaro
- class Env < Hash
- def self.from(hash)
- new.replace(hash)
+ module ENV
+ extend self
+
+ def respond_to?(method, *)
+ key, punctuation = extract_key_from_method(method)
+
+ case punctuation
+ when "!" then has_key?(key) || super
+ when "?", nil then true
+ else super
+ end
end
+ private
+
def method_missing(method, *)
- pair = ENV.detect { |k, _| k.upcase == method.to_s.upcase }
- pair ? pair[1] : super
+ key, punctuation = extract_key_from_method(method)
+
+ case punctuation
+ when "!" then send(key) || missing_key!(key)
+ when "?" then !!send(key)
+ when nil then get_value(key)
+ else super
+ end
end
- def respond_to?(method, *)
- ENV.keys.any? { |k| k.upcase == method.to_s.upcase } || super
+ def extract_key_from_method(method)
+ method.to_s.downcase.match(/^(.+?)([!?=])?$/).captures
+ end
+
+ def has_key?(key)
+ ::ENV.any? { |k, _| k.downcase == key }
+ end
+
+ def missing_key!(key)
+ raise MissingKey.new("Missing required Figaro configuration key #{key.inspect}.")
+ end
+
+ def get_value(key)
+ _, value = ::ENV.detect { |k, _| k.downcase == key }
+ value
end
end
end