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 +4 -4
- data/lib/figly.rb +50 -10
- data/lib/figly/console.rb +0 -2
- data/lib/figly/version.rb +1 -1
- data/spec/figly_spec.rb +18 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca71726e5c151db9b95fbaac7d18642457640496
|
4
|
+
data.tar.gz: 8d4950a3615f85f555e7191def270af19dd03b58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd31fed56870c2e2ee55291f99841ad8503847471d6c8488f5302dd46b827f2fc8cf3a9dac627b56647ba991f5b7943444f089d4626ce7c953d569f144aa73d3
|
7
|
+
data.tar.gz: 4d3e715cfc2c92d05b99fe92540222a010bc056838c74ee8a30ce49a66bff32d3f60699eacc0f242a50b7d54f4b7dcb7b16e8b562e261e37eaeea1b426af267d
|
data/lib/figly.rb
CHANGED
@@ -2,26 +2,66 @@ require "figly/version"
|
|
2
2
|
require "figly/settings"
|
3
3
|
|
4
4
|
module Figly
|
5
|
-
|
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
|
-
|
11
|
+
data = case ext
|
9
12
|
when '.toml'
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
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
|
-
|
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
|
data/lib/figly/console.rb
CHANGED
data/lib/figly/version.rb
CHANGED
data/spec/figly_spec.rb
CHANGED
@@ -2,9 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Figly do
|
4
4
|
context 'YAML' do
|
5
|
-
before
|
6
|
-
|
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
|
36
|
-
|
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
|
45
|
-
|
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
|