a9n 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14a5336e3c89478cdefd0df4a7c71dd78e809b10
4
- data.tar.gz: 5ddb8cbb02e172e3bd26e43c2397ae0d49581a9a
3
+ metadata.gz: c6fafa85c44eb1e47cfc4a8c8c75d1d5ad46c982
4
+ data.tar.gz: 3e014475898cc21ec793573cae9873b35539fddd
5
5
  SHA512:
6
- metadata.gz: 84e86886b37e5c7f785f6c9e9b0221aa05c9b0feced8f7992328d9bc475ff4842272d6011ef98cedb7153b289a40f63390e278d8a17ebde5647e71eccf3614b2
7
- data.tar.gz: 4995758ee232692f671f8f944865712ff169bdb4ac0c166a0fe6ce0eebd95295f35909a6d35189dca44b5913c139565f58531309c8cf7ca37633a61f3c98a097
6
+ metadata.gz: 0ad1197d5321b3c868ff02c4d1265538d2fee85a9a24bf12a801dbac3e94f8f6705a5059315d5fec3472c2f2388b25c32d56006ed002cecea2adb30b6c98125a
7
+ data.tar.gz: e2e61a41f5b9b53bc86f6640f7d8160acdfae31d04d4a81c847eab5d883ff92095d475b623a80f475a71318e0376c328002880d45b37c7498a66012b15a84729
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
  gem 'rake'
5
- gem 'rspec', '~> 2.14.0.rc1'
5
+ gem 'rspec', '~> 2.14.0'
6
6
  gem 'coveralls', require: false
data/README.md CHANGED
@@ -10,7 +10,8 @@
10
10
  [codeclimate]: https://codeclimate.com/github/knapo/a9n
11
11
  [coveralls]: https://coveralls.io/r/knapo/a9n
12
12
 
13
- Simple tool for managing extra configuration in ruby/rails apps. Supports Rails 2.x, 3.x, 4.x and Ruby 1.9, 2.0. For Ruby 1.8 use version < 1.0
13
+ Simple tool for managing extra configuration in ruby/rails apps. Supports Rails 2.x, 3.x, 4.x and Ruby 1.9, 2.0.
14
+ Ruby 1.8 is not supported in version 0.1.2 and higher.
14
15
 
15
16
  ## Installation
16
17
 
data/lib/a9n.rb CHANGED
@@ -43,8 +43,17 @@ module A9n
43
43
  end
44
44
 
45
45
  def load
46
- base = load_yml('config/configuration.yml.example')
47
- local = load_yml('config/configuration.yml')
46
+ env_config = load_env_config
47
+ default_config = load_default_config
48
+
49
+ whole_config = default_config.merge(env_config)
50
+
51
+ @@configuration = Struct.new(whole_config)
52
+ end
53
+
54
+ def load_env_config
55
+ base = load_yml('config/configuration.yml.example', env)
56
+ local = load_yml('config/configuration.yml', env)
48
57
 
49
58
  if base.nil? && local.nil?
50
59
  raise MissingConfigurationFile.new("Neither config/configuration.yml.example nor config/configuration.yml was found")
@@ -54,18 +63,27 @@ module A9n
54
63
  verify!(base, local)
55
64
  end
56
65
 
57
- @@configuration = Struct.new(local || base)
66
+ local || base
67
+ end
68
+
69
+ def load_default_config
70
+ data = load_yml('config/configuration.yml.example', 'defaults', false)
71
+ data ||= load_yml('config/configuration.yml', 'defaults', false)
72
+ data ||= {}
73
+ return data
58
74
  end
59
75
 
60
- def load_yml(file)
76
+ def load_yml(file, env, raise_when_not_found = true)
61
77
  path = File.join(self.root, file)
62
- return unless File.exists?(path)
78
+ return nil unless File.exists?(path)
63
79
  yml = YAML.load(ERB.new(File.read(path)).result)
64
80
 
65
- if yml[self.env].is_a?(Hash)
66
- return yml[self.env].deep_symbolize_keys
81
+ if yml[env].is_a?(Hash)
82
+ return yml[env].deep_symbolize_keys
83
+ elsif raise_when_not_found
84
+ raise MissingConfigurationData.new("Configuration data for #{env} was not found in #{file}")
67
85
  else
68
- raise MissingConfigurationData.new("Configuration data for #{self.env} was not found in #{file}")
86
+ return nil
69
87
  end
70
88
  end
71
89
 
@@ -1,3 +1,3 @@
1
1
  module A9n
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -12,7 +12,7 @@ describe A9n do
12
12
  end
13
13
 
14
14
  context 'when custom non-rails app is being used' do
15
- let(:local_app) { double(:env => 'test', :root => '/apps/a9n') }
15
+ let(:local_app) { double(env: 'test', root: '/apps/a9n') }
16
16
  before { described_class.local_app = local_app }
17
17
 
18
18
  specify { described_class.local_app.should == local_app }
@@ -22,7 +22,7 @@ describe A9n do
22
22
  end
23
23
 
24
24
  describe '.root' do
25
- let(:local_app) { double(:env => 'test', :root => '/apps/a9n') }
25
+ let(:local_app) { double(env: 'test', root: '/apps/a9n') }
26
26
  before { described_class.local_app = local_app }
27
27
 
28
28
  context 'with custom path' do
@@ -48,17 +48,33 @@ describe A9n do
48
48
 
49
49
  describe '.load' do
50
50
  let(:base_sample_config){
51
- { :app_url => 'http://127.0.0.1:3000' }
51
+ { app_url: 'http://127.0.0.1:3000', api_key: 'base1234' }
52
52
  }
53
53
  let(:local_sample_config){
54
- { :app_host => '127.0.0.1:3000' }
54
+ { app_host: '127.0.0.1:3000', api_key: 'local1234' }
55
55
  }
56
- subject { described_class }
56
+ let(:base_default_config){
57
+ { page_title: 'Base Kiełbasa', api_key: 'base1234default' }
58
+ }
59
+ let(:local_default_config){
60
+ { page_title: 'Local Kiełbasa', api_key: 'local1234default' }
61
+ }
62
+ let(:env){
63
+ 'tropical'
64
+ }
65
+ subject {
66
+ described_class
67
+ }
68
+ before do
69
+ allow(described_class).to receive(:env).and_return(env)
70
+ end
57
71
 
58
72
  context 'when no configuration file exists' do
59
73
  before do
60
- expect(described_class).to receive(:load_yml).with('config/configuration.yml.example').and_return(nil)
61
- expect(described_class).to receive(:load_yml).with('config/configuration.yml').and_return(nil)
74
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', env).and_return(nil)
75
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', env).and_return(nil)
76
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', 'defaults', false).never
77
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', 'defaults', false).never
62
78
  expect(described_class).to receive(:verify!).never
63
79
  end
64
80
  it 'raises expection' do
@@ -68,44 +84,59 @@ describe A9n do
68
84
  end
69
85
  end
70
86
 
71
- context 'when base configuration file exists' do
87
+ context 'when base configuration file exists with defaults' do
72
88
  before do
73
- expect(described_class).to receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
74
- expect(described_class).to receive(:load_yml).with('config/configuration.yml').and_return(nil)
89
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', env).and_return(base_sample_config)
90
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', env).and_return(nil)
91
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', 'defaults', false).and_return(base_default_config)
92
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', 'defaults', false).never
93
+
75
94
  expect(described_class).to receive(:verify!).never
76
95
  described_class.load
77
96
  end
78
97
 
79
98
  its(:app_url) { should_not be_nil }
80
99
  its(:app_url) { should == subject.fetch(:app_url) }
100
+ its(:page_title) { should == 'Base Kiełbasa' }
101
+ its(:api_key) { should == 'base1234' }
81
102
  specify {
82
103
  expect { subject.app_host }.to raise_error(described_class::NoSuchConfigurationVariable)
83
104
  }
84
105
  end
85
106
 
86
- context 'when local configuration file exists' do
107
+ context 'when local configuration file exists with defaults' do
87
108
  before do
88
- expect(described_class).to receive(:load_yml).with('config/configuration.yml.example').and_return(nil)
89
- expect(described_class).to receive(:load_yml).with('config/configuration.yml').and_return(local_sample_config)
109
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', env).and_return(nil)
110
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', env).and_return(local_sample_config)
111
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', 'defaults', false).and_return(nil)
112
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', 'defaults', false).and_return(local_default_config)
90
113
  expect(described_class).to receive(:verify!).never
91
114
  described_class.load
92
115
  end
93
116
 
94
117
  its(:app_host) { should_not be_nil }
118
+ its(:page_title) { should == 'Local Kiełbasa' }
119
+ its(:api_key) { should == 'local1234' }
95
120
  specify {
96
121
  expect { subject.app_url }.to raise_error(described_class::NoSuchConfigurationVariable)
97
122
  }
98
123
  end
99
124
 
100
- context 'when both local and base configuration file exists' do
125
+ context 'when both local and base configuration file exists without defaults' do
101
126
  context 'with same data' do
102
127
  before do
103
- expect(described_class).to receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
104
- expect(described_class).to receive(:load_yml).with('config/configuration.yml').and_return(base_sample_config)
128
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', env).and_return(base_sample_config)
129
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', env).and_return(base_sample_config)
130
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', 'defaults', false).and_return(nil)
131
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', 'defaults', false).and_return(nil)
105
132
  described_class.load
106
133
  end
107
134
 
108
- its(:app_url) { should_not be_nil }
135
+ its(:app_url) { should_not be_nil }
136
+ its(:api_key) { should == 'base1234' }
137
+ specify {
138
+ expect { subject.page_title }.to raise_error(described_class::NoSuchConfigurationVariable)
139
+ }
109
140
  specify {
110
141
  expect { subject.app_host }.to raise_error(described_class::NoSuchConfigurationVariable)
111
142
  }
@@ -113,8 +144,10 @@ describe A9n do
113
144
 
114
145
  context 'with different data' do
115
146
  before do
116
- expect(described_class).to receive(:load_yml).with('config/configuration.yml.example').and_return(base_sample_config)
117
- expect(described_class).to receive(:load_yml).with('config/configuration.yml').and_return(local_sample_config)
147
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', env).and_return(base_sample_config)
148
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', env).and_return(local_sample_config)
149
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml.example', 'defaults', false).never
150
+ expect(described_class).to receive(:load_yml).with('config/configuration.yml', 'defaults', false).never
118
151
  end
119
152
  it 'raises expection' do
120
153
  expect {
@@ -127,14 +160,15 @@ describe A9n do
127
160
 
128
161
  describe '.load_yml' do
129
162
  let(:root) { File.dirname(__FILE__) }
130
- subject { described_class.load_yml(file_path) }
163
+ let(:env) { 'test' }
164
+ subject { described_class.load_yml(file_path, env) }
131
165
 
132
166
  before do
133
167
  expect(described_class).to receive(:root).at_least(:once).and_return(root)
168
+ expect(described_class).to receive(:env).never
134
169
  end
135
170
 
136
171
  context 'when file not exists' do
137
- before { expect(described_class).to receive(:env).never }
138
172
  let(:file_path) { 'file_not_existing_in_universe.yml' }
139
173
 
140
174
  it 'returns nil' do
@@ -146,12 +180,9 @@ describe A9n do
146
180
  let(:file_path) { 'fixtures/configuration.yml'}
147
181
  before {
148
182
  ENV['DWARF'] = 'erbized dwarf'
149
- expect(described_class).to receive(:env).twice.and_return(env)
150
183
  }
151
184
 
152
185
  context 'and has data' do
153
- let(:env) { 'test' }
154
-
155
186
  it 'returns non-empty hash' do
156
187
  subject.should be_kind_of(Hash)
157
188
  subject.keys.should_not be_empty
@@ -187,7 +218,7 @@ describe A9n do
187
218
 
188
219
  context 'local_app_env is set' do
189
220
  before {
190
- expect(described_class).to receive(:local_app).and_return(double(:env => 'dwarf_env')).exactly(3).times
221
+ expect(described_class).to receive(:local_app).and_return(double(env: 'dwarf_env')).exactly(3).times
191
222
  expect(described_class).to receive(:get_env_var).never
192
223
  }
193
224
  its(:env) { should == 'dwarf_env' }
@@ -1,9 +1,13 @@
1
+ defaults:
2
+ default_dwarf: "default dwarf"
3
+ overriden_dwarf: "not yet overriden dwarf"
1
4
  development:
2
5
  nil_dwarf: ~
3
6
  false_dwarf: false
4
7
  true_dwarf: true
5
8
  string_dwarf: "dwarf"
6
9
  erb_dwarf: "<%= ENV['DWARF'] %>"
10
+ overriden_dwarf: "already overriden dwarf"
7
11
  hash_dwarf:
8
12
  dwarf_1: "hello 1"
9
13
  dwarf_2: "hello 2"
@@ -13,6 +17,7 @@ test:
13
17
  true_dwarf: true
14
18
  string_dwarf: "dwarf"
15
19
  erb_dwarf: "<%= ENV['DWARF'] %>"
20
+ overriden_dwarf: "already overriden dwarf"
16
21
  hash_dwarf:
17
22
  dwarf_1: "hello 1"
18
23
  dwarf_2: "hello 2"
@@ -3,11 +3,11 @@ require 'spec_helper'
3
3
  describe A9n::Struct do
4
4
  subject {
5
5
  described_class.new({
6
- :non_empty_dwarf => 'dwarf',
7
- :nil_dwarf => nil,
8
- :false_dwarf => false,
9
- :true_dwarf => true,
10
- :hash_dwarf => { :dwarf => 'hello' }
6
+ non_empty_dwarf: 'dwarf',
7
+ nil_dwarf: nil,
8
+ false_dwarf: false,
9
+ true_dwarf: true,
10
+ hash_dwarf: { dwarf: 'hello' }
11
11
  })
12
12
  }
13
13
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a9n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Knapik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-05 00:00:00.000000000 Z
11
+ date: 2013-11-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple tool for managing extra configuration in ruby/rails apps
14
14
  email: