cupcakinator 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cupcakinator/base.rb +16 -2
- data/lib/cupcakinator/config.rb +20 -0
- data/lib/cupcakinator/version.rb +1 -1
- data/spec/cupcakinator/base_spec.rb +36 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3745cec57b86dfd91a0e85eb8969710e59ab49b1
|
4
|
+
data.tar.gz: 320afa16a00e277cbfb3a3e23c657643a7c8fb53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e625ed3d61e05765f104ce598fc128b71742d8a6241236f8ee090098221ed4abc942b77797ac4cae2fc0169e2d3dc72b29d5cfee71e334b361dc0d2ba8f9151c
|
7
|
+
data.tar.gz: ad29ae6175a559e31a58a2eb80251e237b8b528461cfe35961cb3f68ddde7fe2243d3ccd2a079212b1a49047a4828e206f1d98558f03eb710d4d9de115844503
|
data/lib/cupcakinator/base.rb
CHANGED
@@ -28,7 +28,8 @@ module Cupcakinator
|
|
28
28
|
# @option options.last [Hash] :file The configuration filename
|
29
29
|
# @option options.last [Hash] :method The method used to access the configuration options
|
30
30
|
# @option options.last [Hash] :root_key A key in the top level of the config file that will become the base
|
31
|
-
# @
|
31
|
+
# @option options.last [Hash] :allow_missing Allows the config file to be missing, config will return empty Hash
|
32
|
+
# @example Default usage - Foo will load ./config/config.yml into a method named 'config' and raise on missing
|
32
33
|
# class Foo
|
33
34
|
# include cupcakinator
|
34
35
|
# cupcakinate
|
@@ -67,6 +68,15 @@ module Cupcakinator
|
|
67
68
|
# >> puts Foo.new.config
|
68
69
|
# { :foo => 'bar' }
|
69
70
|
#
|
71
|
+
# @example with no config file
|
72
|
+
#
|
73
|
+
# class Foo
|
74
|
+
# include cupcakinator
|
75
|
+
# cupcakinate dir: Rails.root.join('config'), file: 'foo_config.yml', allow_missing: true
|
76
|
+
# end
|
77
|
+
# >> puts Foo.config
|
78
|
+
# {}
|
79
|
+
#
|
70
80
|
def cupcakinate(*options)
|
71
81
|
if !options.empty?
|
72
82
|
default_options = _cupcakinator_options
|
@@ -101,7 +111,11 @@ module Cupcakinator
|
|
101
111
|
@cupcakinator_config = Cupcakinator::Config.new(yaml_config)
|
102
112
|
end
|
103
113
|
rescue Errno::ENOENT
|
104
|
-
|
114
|
+
if (_cupcakinator_options.allow_missing rescue false)
|
115
|
+
@cupcakinator_config = Cupcakinator::Config.new({})
|
116
|
+
else
|
117
|
+
raise Cupcakinator::ConfigFileNotFoundError.new(filename, _cupcakinator_options)
|
118
|
+
end
|
105
119
|
rescue Psych::SyntaxError => e
|
106
120
|
raise Cupcakinator::ConfigFileInvalidError.new(filename, e.message)
|
107
121
|
end
|
data/lib/cupcakinator/config.rb
CHANGED
@@ -7,12 +7,16 @@ module Cupcakinator
|
|
7
7
|
# method
|
8
8
|
# dir
|
9
9
|
# file
|
10
|
+
# root_key
|
11
|
+
# allow_missing
|
10
12
|
class Config < Hash
|
11
13
|
include Hashie::Extensions::MethodAccess
|
12
14
|
include Hashie::Extensions::Coercion
|
13
15
|
|
14
16
|
coerce_value Hash, Config
|
15
17
|
|
18
|
+
# @param [Hash] h
|
19
|
+
# @return Cupcakinator::Config
|
16
20
|
def initialize(h={})
|
17
21
|
super
|
18
22
|
h.each_pair do |k,v|
|
@@ -20,6 +24,22 @@ module Cupcakinator
|
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
27
|
+
|
28
|
+
# @return [Hash] returns uncoerced Hash
|
29
|
+
def to_h
|
30
|
+
convert_config_to_hash(self.dup)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
|
37
|
+
def convert_config_to_hash(c)
|
38
|
+
h = Hash.new
|
39
|
+
c.each_pair{ |key, value| Config === value ? h[key] = convert_config_to_hash(value) : h[key] = value }
|
40
|
+
h
|
41
|
+
end
|
42
|
+
|
23
43
|
end
|
24
44
|
|
25
45
|
end
|
data/lib/cupcakinator/version.rb
CHANGED
@@ -84,6 +84,42 @@ describe Cupcakinator::Base do
|
|
84
84
|
expect{ CupcakinatorBaseSpecNoExist.load_cupcakinator_config }.to raise_error(Cupcakinator::ConfigFileNotFoundError)
|
85
85
|
end
|
86
86
|
|
87
|
+
it 'should raise ConfigFileNotFoundError if config file is not found and allow_missing is true' do
|
88
|
+
class CupcakinatorBaseSpecNoExist
|
89
|
+
include Cupcakinator
|
90
|
+
|
91
|
+
cupcakinate file: 'no_exist.yml', allow_missing: false
|
92
|
+
end
|
93
|
+
YAML.stub(:load_file).with('./no_exist.yml').and_raise(Errno::ENOENT)
|
94
|
+
YAML.stub(:load_file).with(anything).and_call_original
|
95
|
+
|
96
|
+
expect{ CupcakinatorBaseSpecNoExist.load_cupcakinator_config }.to raise_error(Cupcakinator::ConfigFileNotFoundError)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not raise ConfigFileNotFoundError if config file is not found and allow_missing is true' do
|
100
|
+
class CupcakinatorBaseSpecNoExist
|
101
|
+
include Cupcakinator
|
102
|
+
|
103
|
+
cupcakinate file: 'no_exist.yml', allow_missing: true
|
104
|
+
end
|
105
|
+
YAML.stub(:load_file).with('./no_exist.yml').and_raise(Errno::ENOENT)
|
106
|
+
YAML.stub(:load_file).with(anything).and_call_original
|
107
|
+
|
108
|
+
expect{ CupcakinatorBaseSpecNoExist.load_cupcakinator_config }.to_not raise_error(Cupcakinator::ConfigFileNotFoundError)
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should return empty Config if config file is not found and allow_missing is true' do
|
112
|
+
class CupcakinatorBaseSpecNoExist
|
113
|
+
include Cupcakinator
|
114
|
+
|
115
|
+
cupcakinate file: 'no_exist.yml', allow_missing: true
|
116
|
+
end
|
117
|
+
YAML.stub(:load_file).with('./no_exist.yml').and_raise(Errno::ENOENT)
|
118
|
+
YAML.stub(:load_file).with(anything).and_call_original
|
119
|
+
|
120
|
+
expect(CupcakinatorBaseSpecNoExist.config.to_h).to eq({})
|
121
|
+
end
|
122
|
+
|
87
123
|
it 'should raise ConfigFileInvalidError if config file is not found' do
|
88
124
|
dummy = double.as_null_object
|
89
125
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cupcakinator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|