stacked_config 1.1.0 → 1.2.0

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: e0158b5db88a7322e9e0c2aef2dba1dca08ed9bd
4
- data.tar.gz: 3e80f4ae117b047fbcfda02d56396e01a6f7a013
3
+ metadata.gz: 282dd3a444e99efb2871011e34b58bdb8242c081
4
+ data.tar.gz: 5881f955417d6c531d7b239bb89270ca50201694
5
5
  SHA512:
6
- metadata.gz: 0a53fc2c107073a657985d86acddba0182a482c65d9b11f31269f757328431be2865bc2c2ec560dc71723a566a72719930060b94219f08b61b06789ce715906a
7
- data.tar.gz: e8952818ffb801400d1b92ac26eb4e12f8e0537ff2435a3bda6dfca14ebdc1ddf795b489c70d9720a088dee64328d2342007f34b732a6603bca80939c4109144
6
+ metadata.gz: b42615073c9ecbf56ff322931b2a2886a75f4ec9325cb0c4ea211a86a43f0f99afb18784389c2ea7cd610ffeb9a46d9951ecdf90ef286d0eb253afeec134a1f9
7
+ data.tar.gz: 10595e497a75007147911ebd1f379f73793e495e4816f92aa1bf2ad2bd30576c09a5a1360b99a3d1353ac60e1a97da7d3ad361a1a2bc19cb86fec885d1e9b42a
@@ -13,5 +13,6 @@ require 'stacked_config/layers/user_layer'
13
13
  require 'stacked_config/layers/env_layer'
14
14
  require 'stacked_config/layers/command_line_layer'
15
15
  require 'stacked_config/layers/provided_config_file_layer'
16
+ require 'stacked_config/layers/project_layer'
16
17
  require 'stacked_config/orchestrator'
17
18
 
@@ -0,0 +1,61 @@
1
+ module StackedConfig
2
+ module Layers
3
+
4
+ class ProjectLayer < SuperStack::Layer
5
+
6
+ attr_accessor :project_file_basename
7
+ attr_reader :started_from_directory
8
+
9
+ def initialize(file_or_directory, project_file_basename=nil)
10
+ if Dir.exists? file_or_directory
11
+ raise 'You have to provide the basename of the project file !' if project_file_basename.nil?
12
+ @started_from_directory = File.expand_path file_or_directory
13
+ self.project_file_basename = project_file_basename
14
+ else
15
+ raise "Invalid project file '#{file_or_directory}' !" unless File.readable? file_or_directory
16
+ normalized = File.expand_path file_or_directory
17
+ @started_from_directory = File.dirname normalized
18
+ self.project_file_basename = File.basename normalized
19
+ end
20
+ end
21
+
22
+ def file_name
23
+ @file_name ||= find_root_file project_file_basename, started_from_directory
24
+ end
25
+
26
+ def project_root
27
+ File.dirname file_name
28
+ end
29
+
30
+ def project_file_basename=(file)
31
+ @file_name = nil
32
+ @project_file_basename = file
33
+ end
34
+
35
+ private
36
+
37
+ def find_root_file(file_basename, initial_directory)
38
+ prev_dir = nil
39
+ cur_dir = initial_directory
40
+ found_file = nil
41
+
42
+ raise "Invalid directory '#{initial_directory}'" unless File.readable? initial_directory
43
+
44
+ while found_file.nil? and cur_dir != prev_dir
45
+ candidate_file = File.join cur_dir, file_basename
46
+ if File.readable? candidate_file
47
+ found_file = candidate_file
48
+ end
49
+ prev_dir = cur_dir
50
+ cur_dir = File.expand_path '..', cur_dir
51
+ end
52
+
53
+ raise "Cannot find any root file named '#{file_basename}' starting from '#{initial_directory}'." if found_file.nil?
54
+
55
+ found_file
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -4,7 +4,7 @@ module StackedConfig
4
4
  include StackedConfig::ProgramDescriptionHelper
5
5
 
6
6
  attr_reader :system_layer, :global_layer, :executable_gem_layer, :user_layer, :env_layer,
7
- :command_line_layer, :provided_config_file_layer
7
+ :command_line_layer, :provided_config_file_layer, :project_layer
8
8
 
9
9
  def initialize
10
10
  super
@@ -19,6 +19,13 @@ module StackedConfig
19
19
  File.basename($PROGRAM_NAME).gsub /\.[^\.]+$/, ''
20
20
  end
21
21
 
22
+ def include_project_layer(file_or_directory, project_file_basename=nil, priority = 65)
23
+ @project_layer = StackedConfig::Layers::ProjectLayer.new file_or_directory, project_file_basename
24
+ env_layer.name = 'Project level'
25
+ env_layer.priority = priority
26
+ self << env_layer
27
+ end
28
+
22
29
  def include_env_layer(filter = nil, priority = 70)
23
30
  @env_layer = StackedConfig::Layers::EnvLayer.new filter
24
31
  env_layer.name = 'Environment variables level'
@@ -1,3 +1,3 @@
1
1
  module StackedConfig
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -116,6 +116,11 @@ describe StackedConfig::Orchestrator do
116
116
 
117
117
  end
118
118
 
119
+ it 'should not have project layer by defaut' do
120
+ expect(subject.project_layer).to be_nil
121
+ end
122
+
123
+
119
124
  it 'should not have environment variables by defaut' do
120
125
  expect(subject.env_layer).to be_nil
121
126
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe StackedConfig::Layers::ProjectLayer do
4
+
5
+ let(:test_root) { File.expand_path '../../test/tst_project_layer', __FILE__ }
6
+ let(:search_from_place1) { File.join test_root, 'single', 'level1', 'level2', 'level3' }
7
+ let(:search_from_place2) { File.join test_root, 'multiple', 'level1', 'level2', 'level3' }
8
+ let(:search_from_place3) { File.join test_root, 'multiple', 'level1', 'level2' }
9
+ let(:config_file_name) { 'test_config_file' }
10
+
11
+ let(:file1) { File.join test_root, 'single', 'level1', config_file_name}
12
+ let(:file2) { File.join test_root, 'multiple', 'level1', 'level2', 'level3', config_file_name}
13
+ let(:file3) { File.join test_root, 'multiple', 'level1', config_file_name}
14
+
15
+ subject { described_class.new test_root, config_file_name }
16
+
17
+ it 'should find the find the first config file in the folders hierarchy' do
18
+ expect(subject.send :find_root_file, config_file_name, search_from_place1).to eq file1
19
+ expect(subject.send :find_root_file, config_file_name, search_from_place2).to eq file2
20
+ expect(subject.send :find_root_file, config_file_name, search_from_place3).to eq file3
21
+ end
22
+
23
+ it 'should raise an exception if no file is found' do
24
+ expect {subject.send :find_root_file, :unknown_config_file, search_from_place1 }.to raise_error
25
+ end
26
+
27
+
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stacked_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurent B.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-29 00:00:00.000000000 Z
11
+ date: 2016-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,6 +115,7 @@ files:
115
115
  - lib/stacked_config/layers/gem_layer.rb
116
116
  - lib/stacked_config/layers/generic_layer.rb
117
117
  - lib/stacked_config/layers/global_layer.rb
118
+ - lib/stacked_config/layers/project_layer.rb
118
119
  - lib/stacked_config/layers/provided_config_file_layer.rb
119
120
  - lib/stacked_config/layers/system_layer.rb
120
121
  - lib/stacked_config/layers/user_layer.rb
@@ -128,6 +129,7 @@ files:
128
129
  - spec/gem_layer_spec.rb
129
130
  - spec/global_layer_spec.rb
130
131
  - spec/orchestrator_spec.rb
132
+ - spec/project_layer_spec.rb
131
133
  - spec/provided_config_file_layer_spec.rb
132
134
  - spec/source_helper_spec.rb
133
135
  - spec/spec_helper.rb
@@ -135,6 +137,10 @@ files:
135
137
  - spec/user_layer_spec.rb
136
138
  - stacked_config.gemspec
137
139
  - test/specific.yml
140
+ - test/tst_project_layer/multiple/level1/level2/level3/test_config_file
141
+ - test/tst_project_layer/multiple/level1/test_config_file
142
+ - test/tst_project_layer/single/level1/level2/level3/.keep
143
+ - test/tst_project_layer/single/level1/test_config_file
138
144
  - test/tstgem/.rspec
139
145
  - test/tstgem/Gemfile
140
146
  - test/tstgem/LICENSE.txt
@@ -182,12 +188,17 @@ test_files:
182
188
  - spec/gem_layer_spec.rb
183
189
  - spec/global_layer_spec.rb
184
190
  - spec/orchestrator_spec.rb
191
+ - spec/project_layer_spec.rb
185
192
  - spec/provided_config_file_layer_spec.rb
186
193
  - spec/source_helper_spec.rb
187
194
  - spec/spec_helper.rb
188
195
  - spec/system_layer_spec.rb
189
196
  - spec/user_layer_spec.rb
190
197
  - test/specific.yml
198
+ - test/tst_project_layer/multiple/level1/level2/level3/test_config_file
199
+ - test/tst_project_layer/multiple/level1/test_config_file
200
+ - test/tst_project_layer/single/level1/level2/level3/.keep
201
+ - test/tst_project_layer/single/level1/test_config_file
191
202
  - test/tstgem/.rspec
192
203
  - test/tstgem/Gemfile
193
204
  - test/tstgem/LICENSE.txt