lib/figly.rb in figly-1.0.1 vs lib/figly.rb in figly-1.0.2
- old
+ new
@@ -1,27 +1,67 @@
require "figly/version"
require "figly/settings"
module Figly
- def self.setup(path)
+ class ParserError < StandardError; end
+ class UnsupportedError < StandardError; end
+
+ def self.load_file(path)
raise "File does not exist: #{path}" unless File.exists?(path)
ext = File.extname(path)
- @@data = case ext
+ data = case ext
when '.toml'
- require 'toml'
- TOML.load_file(path)
+ begin
+ require 'toml'
+ # HACK: TOML captures Parslet errors and puts them so they get swallowed
+ # here we redirect stdout to an IO buffer that we can read from and test
+ # that the value doesn't match an error
+ old_stdout = $stdout
+ $stdout = StringIO.new('','w')
+ d = TOML.load_file(path)
+ cap = $stdout.string
+ raise ParserError, cap if cap =~ /^Failed to match/
+ $stdout = old_stdout
+ d
+ rescue Exception => e
+ raise ParserError, e.message
+ end
when '.yml'
- require 'yaml'
- YAML.load_file(path)
+ begin
+ require 'yaml'
+ YAML.load_file(path)
+ rescue Exception => e
+ raise ParserError, e.message
+ end
when '.json'
- require 'json'
- JSON.parse(File.read(path))
+ begin
+ require 'json'
+ JSON.parse(File.read(path))
+ rescue Exception => e
+ raise ParserError, e.message
+ end
else
- raise "Unsupported file extension (#{ext})"
+ raise UnsupportedError, "Unsupported file extension (#{ext})"
end
- @@data
+
+ # Here we merge config files if there are multiple load calls
+ if defined?(@@data) && !@@data.nil?
+ _deep_merge(@@data, data)
+ else
+ @@data = data
+ end
end
+ ## Useful for testing
+ def self.clean
+ @@data = nil
+ end
+
def self.data
@@data
+ end
+
+ def self._deep_merge(first, second)
+ merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
+ first.merge!(second, &merger)
end
end