figly 1.0.2 → 1.0.3

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: ca71726e5c151db9b95fbaac7d18642457640496
4
- data.tar.gz: 8d4950a3615f85f555e7191def270af19dd03b58
3
+ metadata.gz: 92096fbb361ef15bd69d38780f8a5d1c4b2d1093
4
+ data.tar.gz: 54be27cfa55e83e7b4a12eea18f6e0f38d53e13e
5
5
  SHA512:
6
- metadata.gz: bd31fed56870c2e2ee55291f99841ad8503847471d6c8488f5302dd46b827f2fc8cf3a9dac627b56647ba991f5b7943444f089d4626ce7c953d569f144aa73d3
7
- data.tar.gz: 4d3e715cfc2c92d05b99fe92540222a010bc056838c74ee8a30ce49a66bff32d3f60699eacc0f242a50b7d54f4b7dcb7b16e8b562e261e37eaeea1b426af267d
6
+ metadata.gz: 9ecce235ec39a27f398de581105cfaa69f4beafc83838a73c4a61ee7e815b5b960786c93126c7ac46b6244d6cc34745925a9d48b2cfd224079f72ec4ee6ef285
7
+ data.tar.gz: 32b0cdc33f3691a5f006fdc9f13ca09fd4b3bdb5ec9de9aa2a99e9ed77f605e800e4a1893b7fda8764386f19ad560510cfffef3f638d297fa86b37141798fd75
data/README.md CHANGED
@@ -18,9 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- The only setup that's required is to set the path of you're configuration file that must be in YAML. Just throw the following code into an initializer:
21
+ The only setup that's required is to set the path of you're configuration file that must be in YAML. Just throw the following code into an initializer.:
22
22
 
23
- Figly.setup "path/to/config.yml"
23
+ Figly.load_file "path/to/config.yml"
24
+
25
+ **NOTE: You can load multiple config files of different types and they will be deep merged together in your settings**
24
26
 
25
27
  If you're config looks like this:
26
28
 
@@ -33,27 +35,33 @@ You can do the following:
33
35
 
34
36
  Figly::Settings.some_key
35
37
  #=> 234
36
-
38
+
37
39
  Figly::Settings.nest1
38
40
  #=> {"nest2" => {"nest3" => "Yay"}}
39
-
41
+
40
42
  Figly::Settings.nest1.nest2.nest3
41
43
  #=> "Yay"
42
44
 
45
+ Figly currently supports the following file extensions, and will infer the parser based on the extension:
46
+
47
+ - .yml => YAML
48
+ - .toml => TOML
49
+ - .json => JSON
50
+
43
51
  ## Testing
44
52
 
45
- If you want to contribute start by making sure the tests work. Also if you want to isolate the bundled gems to the sandbox (the path below is already in the .gitignore), run:
53
+ If you want to contribute start by making sure the tests work:
46
54
 
47
- bundle install --path vendor/bundle
55
+ bundle install
48
56
 
49
- To access a REPL environment that loads the libraries as well as does the initial setup with the `spec/support/config.yml` set as the path:
57
+ To access a REPL environment that loads the libraries:
50
58
 
51
59
  ./bin/console
52
-
60
+
53
61
  To run tests:
54
62
 
55
63
  rspec spec
56
-
64
+
57
65
  ## Contributing
58
66
 
59
67
  1. Fork it ( https://github.com/onetwopunch/figly/fork )
@@ -3,10 +3,11 @@ require "figly/settings"
3
3
 
4
4
  module Figly
5
5
  class ParserError < StandardError; end
6
- class UnsupportedError < StandardError; end
6
+ class UnsupportedFormatError < StandardError; end
7
+ class ConfigNotFoundError < StandardError; end
7
8
 
8
9
  def self.load_file(path)
9
- raise "File does not exist: #{path}" unless File.exists?(path)
10
+ raise ConfigNotFoundError, "File does not exist: #{path}" unless File.exists?(path)
10
11
  ext = File.extname(path)
11
12
  data = case ext
12
13
  when '.toml'
@@ -17,13 +18,15 @@ module Figly
17
18
  # that the value doesn't match an error
18
19
  old_stdout = $stdout
19
20
  $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
21
+ TOML.load_file(path).tap do |d|
22
+ cap = $stdout.string
23
+ raise ParserError, cap if cap =~ /^Failed to match/
24
+ end
25
25
  rescue Exception => e
26
26
  raise ParserError, e.message
27
+ ensure
28
+ # Make sure to reset the old stdout even if an exception is thrown
29
+ $stdout = old_stdout
27
30
  end
28
31
  when '.yml'
29
32
  begin
@@ -40,7 +43,7 @@ module Figly
40
43
  raise ParserError, e.message
41
44
  end
42
45
  else
43
- raise UnsupportedError, "Unsupported file extension (#{ext})"
46
+ raise UnsupportedFormatError, "Unsupported file extension (#{ext})"
44
47
  end
45
48
 
46
49
  # Here we merge config files if there are multiple load calls
@@ -1,3 +1,3 @@
1
1
  module Figly
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -57,4 +57,29 @@ describe Figly do
57
57
  expect(Figly::Settings.a.b.c).to eq({"d"=>"test"})
58
58
  end
59
59
  end
60
+
61
+ context 'errors' do
62
+ context 'ParserError' do
63
+ it 'should error on bad.yml' do
64
+ expect{Figly.load_file('spec/support/bad.yml')}.to raise_error(Figly::ParserError)
65
+ end
66
+ it 'should error on bad.json' do
67
+ expect{Figly.load_file('spec/support/bad.json')}.to raise_error(Figly::ParserError)
68
+ end
69
+ it 'should error on bad.toml' do
70
+ expect{Figly.load_file('spec/support/bad.toml')}.to raise_error(Figly::ParserError)
71
+ end
72
+ it 'should reset $stdout to the default even on an error' do
73
+ expect{ Figly.load_file('spec/support/bad.toml') rescue nil }.not_to change { $stdout }
74
+ end
75
+ end
76
+
77
+ it 'should raise an error if the config file is not found' do
78
+ expect{ Figly.load_file('spec/support/nonexistent.json') }.to raise_error(Figly::ConfigNotFoundError)
79
+ end
80
+
81
+ it 'should raise an error on an unsupported config file format' do
82
+ expect{ Figly.load_file("spec/support/config.ini") }.to raise_error(Figly::UnsupportedFormatError)
83
+ end
84
+ end
60
85
  end
@@ -0,0 +1,3 @@
1
+ {
2
+ "hello": 'world'
3
+ }
@@ -0,0 +1,2 @@
1
+ [section]
2
+ key = 'value'
@@ -0,0 +1,2 @@
1
+ x
2
+ language:
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Canty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: toml
@@ -102,6 +102,10 @@ files:
102
102
  - lib/figly/version.rb
103
103
  - spec/figly_spec.rb
104
104
  - spec/spec_helper.rb
105
+ - spec/support/bad.json
106
+ - spec/support/bad.toml
107
+ - spec/support/bad.yml
108
+ - spec/support/config.ini
105
109
  - spec/support/config.json
106
110
  - spec/support/config.toml
107
111
  - spec/support/config.yml
@@ -134,6 +138,10 @@ summary: A tiny gem that allows you to access config settings from YAML, TOML, o
134
138
  test_files:
135
139
  - spec/figly_spec.rb
136
140
  - spec/spec_helper.rb
141
+ - spec/support/bad.json
142
+ - spec/support/bad.toml
143
+ - spec/support/bad.yml
144
+ - spec/support/config.ini
137
145
  - spec/support/config.json
138
146
  - spec/support/config.toml
139
147
  - spec/support/config.yml