lib/anyway/env.rb in anyway_config-0.3.0 vs lib/anyway/env.rb in anyway_config-0.4.0
- old
+ new
@@ -1,7 +1,14 @@
module Anyway
class Env
+
+ # Regexp to detect array values
+ # Array value is a values that contains at least one comma
+ # and doesn't start/end with quote
+
+ ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/
+
def initialize
@data = {}
load
end
@@ -26,11 +33,11 @@
private
def load
ENV.each_pair do |key, val|
if config_key?(key)
mod, path = extract_module_path(key)
- set_by_path(get_hash(@data, mod), path, val)
+ set_by_path(get_hash(@data, mod), path, serialize_val(val))
end
end
end
def config_key?(key)
@@ -52,8 +59,29 @@
to[parts.first] = val
end
def get_hash(from, name)
(from[name] ||= {}.with_indifferent_access)
+ end
+
+ def serialize_val(value)
+ case value
+ when ARRAY_RXP
+ value.split(/\s*,\s*/).map(&method(:serialize_val))
+ when /\A(true|t|yes|y)\z/i
+ true
+ when /\A(false|f|no|n)\z/i
+ false
+ when /\A(nil|null)\z/i
+ nil
+ when /\A\d+\z/
+ value.to_i
+ when /\A\d*\.\d+\z/
+ value.to_f
+ when /\A['"].*['"]\z/
+ value.gsub(/(\A['"]|['"]\z)/,'')
+ else
+ value
+ end
end
end
end
\ No newline at end of file