figly 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42364b75b7532c60705dbac8b6ef845d5c050fd1
4
- data.tar.gz: 967175d1098a36cb2667ef092b3c5e9af0f681cf
3
+ metadata.gz: ca71726e5c151db9b95fbaac7d18642457640496
4
+ data.tar.gz: 8d4950a3615f85f555e7191def270af19dd03b58
5
5
  SHA512:
6
- metadata.gz: 55611aa6fd7c901dae7cb0d521840b7587f2273f1f14877cb225737a07d40a09cf7cc4f28a077182c18c41ba85f4100cf172cce29bfb543c9661b7ba42a82254
7
- data.tar.gz: 40a0f3233a2544f8b95a3cc5a22560ddc96c46ce85c0609b021de7f3aa48d69eb3b0dffc43292aa5e067f1ae98420c0e9863aa9e80ba525699b00e58933109c0
6
+ metadata.gz: bd31fed56870c2e2ee55291f99841ad8503847471d6c8488f5302dd46b827f2fc8cf3a9dac627b56647ba991f5b7943444f089d4626ce7c953d569f144aa73d3
7
+ data.tar.gz: 4d3e715cfc2c92d05b99fe92540222a010bc056838c74ee8a30ce49a66bff32d3f60699eacc0f242a50b7d54f4b7dcb7b16e8b562e261e37eaeea1b426af267d
@@ -2,26 +2,66 @@ require "figly/version"
2
2
  require "figly/settings"
3
3
 
4
4
  module Figly
5
- def self.setup(path)
5
+ class ParserError < StandardError; end
6
+ class UnsupportedError < StandardError; end
7
+
8
+ def self.load_file(path)
6
9
  raise "File does not exist: #{path}" unless File.exists?(path)
7
10
  ext = File.extname(path)
8
- @@data = case ext
11
+ data = case ext
9
12
  when '.toml'
10
- require 'toml'
11
- TOML.load_file(path)
13
+ begin
14
+ require 'toml'
15
+ # HACK: TOML captures Parslet errors and puts them so they get swallowed
16
+ # here we redirect stdout to an IO buffer that we can read from and test
17
+ # that the value doesn't match an error
18
+ old_stdout = $stdout
19
+ $stdout = StringIO.new('','w')
20
+ d = TOML.load_file(path)
21
+ cap = $stdout.string
22
+ raise ParserError, cap if cap =~ /^Failed to match/
23
+ $stdout = old_stdout
24
+ d
25
+ rescue Exception => e
26
+ raise ParserError, e.message
27
+ end
12
28
  when '.yml'
13
- require 'yaml'
14
- YAML.load_file(path)
29
+ begin
30
+ require 'yaml'
31
+ YAML.load_file(path)
32
+ rescue Exception => e
33
+ raise ParserError, e.message
34
+ end
15
35
  when '.json'
16
- require 'json'
17
- JSON.parse(File.read(path))
36
+ begin
37
+ require 'json'
38
+ JSON.parse(File.read(path))
39
+ rescue Exception => e
40
+ raise ParserError, e.message
41
+ end
18
42
  else
19
- raise "Unsupported file extension (#{ext})"
43
+ raise UnsupportedError, "Unsupported file extension (#{ext})"
44
+ end
45
+
46
+ # Here we merge config files if there are multiple load calls
47
+ if defined?(@@data) && !@@data.nil?
48
+ _deep_merge(@@data, data)
49
+ else
50
+ @@data = data
20
51
  end
21
- @@data
52
+ end
53
+
54
+ ## Useful for testing
55
+ def self.clean
56
+ @@data = nil
22
57
  end
23
58
 
24
59
  def self.data
25
60
  @@data
26
61
  end
62
+
63
+ def self._deep_merge(first, second)
64
+ merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
65
+ first.merge!(second, &merger)
66
+ end
27
67
  end
@@ -4,5 +4,3 @@ Pry.config.prompt = lambda do |context, nesting, pry|
4
4
  end
5
5
 
6
6
  config = File.expand_path(File.join(__FILE__, '../../../spec/support/config.yml'))
7
- Figly.setup config
8
-
@@ -1,3 +1,3 @@
1
1
  module Figly
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -2,9 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe Figly do
4
4
  context 'YAML' do
5
- before do
6
- Figly.setup(config_file)
7
- end
5
+ before { Figly.load_file(config_file) }
6
+ after { Figly.clean }
8
7
 
9
8
  it 'should correctly access and integer on the top level' do
10
9
  expect(Figly::Settings.some_key).to eq(234)
@@ -32,20 +31,30 @@ describe Figly do
32
31
  end
33
32
 
34
33
  context 'TOML' do
35
- before do
36
- Figly.setup(config_file 'toml')
37
- end
34
+ before { Figly.load_file(config_file 'toml') }
35
+ after { Figly.clean }
38
36
  it 'should parse and work for TOML' do
39
37
  expect(Figly::Settings.a.b).to eq({"c"=>{"d"=>"test"}})
40
38
  end
41
39
  end
42
40
 
43
41
  context 'JSON' do
44
- before do
45
- Figly.setup(config_file 'json')
46
- end
42
+ before { Figly.load_file(config_file 'json') }
43
+ after { Figly.clean }
47
44
  it 'should parse and work with JSON' do
48
45
  expect(Figly::Settings.userId).to eq(1)
49
46
  end
50
47
  end
48
+
49
+ context 'multi files' do
50
+ before {
51
+ Figly.load_file(config_file 'json')
52
+ Figly.load_file(config_file 'toml')
53
+ }
54
+
55
+ it 'should have both sets of data merged' do
56
+ expect(Figly::Settings.userId).to eq(1)
57
+ expect(Figly::Settings.a.b.c).to eq({"d"=>"test"})
58
+ end
59
+ end
51
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Canty