rock_config 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da922994b893029a37676d683f7f6f25ea14d3918e1d6006e1367bb5480d2b06
4
+ data.tar.gz: dfd153a9810b3fc09615d7ca0a0bee2b186670ee3ebebfdbfbea29d1c157d3f0
5
+ SHA512:
6
+ metadata.gz: 137ba8ac8db010451004d69d60604a4def636dba1bc632ae364b291b107b6d75ea2f581b42005cf24155e4093ee2a283739b4e2208b6d602fc0f7fe557ae2db3
7
+ data.tar.gz: efffdea09564a100810bc6940c6eda6b5e826c74ec4d3f1c136388a5181085e37622d88d3e7ccc7ecb2d556c9ca960322015edc6390b970e19c67d9cc1ce77e6
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ - 1.8.7
11
+ - ree
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RockConfig
1
+ # RockConfig [![Build Status](https://secure.travis-ci.org/mekishizufu/rock_config.png)](http://travis-ci.org/mekishizufu/rock_config) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mekishizufu/rock_config)
2
2
 
3
3
  RockConfig is a simple convention based library for reading and accessing config files.
4
4
 
@@ -33,7 +33,7 @@ RockConfig allows you to read these settings with an API like this:
33
33
  elastic_config = RockConfig.for "elastic_search"
34
34
  elastic_config.host # > 127.0.0.1
35
35
 
36
- RockConfig automatically chooses the current application environment. You select the environment yourself
36
+ RockConfig automatically chooses the current application environment. You can select the environment yourself
37
37
  with:
38
38
 
39
39
  RockConfig.for "elastic_search", "production"
@@ -21,10 +21,10 @@ module RockConfig
21
21
  private
22
22
 
23
23
  def detect_environment
24
- @detected_environment ||= lambda do
25
- detector = EnvironmentDetector.new(ENV)
26
- detector.detect
27
- end.call
24
+ @detected_environment ||= begin
25
+ detector = EnvironmentDetector.new(ENV)
26
+ detector.detect
27
+ end
28
28
  end
29
29
 
30
30
  def configuration
@@ -4,18 +4,31 @@ module RockConfig
4
4
  @hash = hash
5
5
  end
6
6
 
7
- def method_missing(*args, &block)
8
- if value = @hash[args.first.to_s]
9
- if Hash === value
10
- Config.new(value)
11
- else
12
- value
13
- end
14
- end
7
+ def [](key)
8
+ fetch(key)
9
+ end
10
+
11
+ def method_missing(name, *args, &block)
12
+ fetch(name.to_s)
15
13
  end
16
14
 
17
15
  def raw
18
16
  @hash.dup
19
17
  end
18
+
19
+ private
20
+
21
+ def fetch(key, default = nil)
22
+ value = @hash[key.to_s]
23
+ value_or_config(value) unless value.nil?
24
+ end
25
+
26
+ def value_or_config(value)
27
+ if Hash === value
28
+ Config.new(value)
29
+ else
30
+ value
31
+ end
32
+ end
20
33
  end
21
34
  end
@@ -7,11 +7,11 @@ module RockConfig
7
7
 
8
8
  def fetch(config_name, environment)
9
9
  if config = @configs[config_name]
10
- if config_for_environment = config.send(environment)
10
+ if config_for_environment = config[environment]
11
11
  return config_for_environment
12
12
  else
13
- raise EnvironmentNotFoundError,
14
- 'Environment "%s" not found' % environment
13
+ raise EnvironmentNotFoundError,
14
+ 'Environment "%s" not found for config file "%s"' % [environment, config_name]
15
15
  end
16
16
  end
17
17
  end
@@ -23,6 +23,5 @@ module RockConfig
23
23
  end
24
24
  end
25
25
  end
26
-
27
26
  end
28
27
  end
@@ -13,7 +13,7 @@ module RockConfig
13
13
  end
14
14
  end
15
15
 
16
- raise ConfigNotFoundError, "Config not found"
16
+ raise ConfigNotFoundError, 'Config file "%s" not found' % file_name
17
17
  end
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module RockConfig
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "yaml"
2
+ require "erb"
2
3
 
3
4
  module RockConfig
4
5
  class YamlLoader
@@ -25,8 +26,9 @@ module RockConfig
25
26
  end
26
27
 
27
28
  def load_yaml_from(path)
28
- YAML.load_file(path)
29
- rescue Psych::SyntaxError => e
29
+ template = ERB.new File.read path
30
+ YAML.load template.result binding
31
+ rescue Exception => e
30
32
  raise ConfigLoadError, e.message
31
33
  end
32
34
  end
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["mekishizufu@gmail.com"]
7
7
  gem.summary = %q{RockConfig allows you to use custom config files easily.}
8
8
  gem.description = %q{RockConfig allows you to use custom config files easily.}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/mekishizufu/rock_config"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = RockConfig::VERSION
17
17
 
18
+ gem.add_development_dependency "rake", "~> 13.0.1"
18
19
  gem.add_development_dependency "rspec", "~> 2.11.0"
19
20
  gem.add_development_dependency "pry", "~> 0.9.10"
20
21
  gem.add_development_dependency "pry-nav", "~> 0.2.2"
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  module RockConfig
4
4
  describe Config do
5
5
  it "accepts a hash" do
6
- hash = {foo: "bar"}
6
+ hash = {:foo => "bar"}
7
7
 
8
8
  config = Config.new(hash)
9
9
  config.raw.should eq(hash)
@@ -27,7 +27,7 @@ module RockConfig
27
27
  config.for_environment("I do not exist").should be_nil
28
28
  end
29
29
 
30
- it "returns correct values when asked for" do
30
+ it "returns correct values" do
31
31
  hash = {
32
32
  "development" => {
33
33
  "host" => "localhost"
@@ -38,7 +38,20 @@ module RockConfig
38
38
  config.development.host.should eq("localhost")
39
39
  end
40
40
 
41
- it "returns correct nested values when asked for" do
41
+ it "returns correct boolean values" do
42
+ hash = {
43
+ "development" => {
44
+ "test_mode" => true,
45
+ "computer_says" => false
46
+ }
47
+ }
48
+
49
+ config = Config.new(hash)
50
+ config.development.test_mode.should == true
51
+ config.development.computer_says.should == false
52
+ end
53
+
54
+ it "returns correct nested values" do
42
55
  hash = {
43
56
  "development" => {
44
57
  "elastic" => {
@@ -50,5 +63,18 @@ module RockConfig
50
63
  config = Config.new(hash)
51
64
  config.development.elastic.host.should eq("localhost")
52
65
  end
66
+
67
+ it "supports accessing the values by brackets" do
68
+ hash = {
69
+ "development" => {
70
+ "elastic" => {
71
+ "host" => "localhost"
72
+ }
73
+ }
74
+ }
75
+
76
+ config = Config.new(hash)
77
+ config["development"]["elastic"]["host"].should eq("localhost")
78
+ end
53
79
  end
54
80
  end
@@ -1,5 +1,7 @@
1
1
  development:
2
2
  magic_number: 213
3
+ test_false: false
4
+ test_true: true
3
5
 
4
6
  test:
5
7
  magic_number: 214
@@ -0,0 +1,10 @@
1
+ development:
2
+ magic_number: <%= ENV['magic_number'] %>
3
+ test_false: false
4
+ test_true: true
5
+
6
+ test:
7
+ magic_number: <%= ENV['magic_number'] %>
8
+
9
+ production:
10
+ magic_number: <%= ENV['magic_number'] %>
@@ -1,8 +1 @@
1
- development
2
- magic_number: 213
3
-
4
- test:
5
- magic_number: 214
6
-
7
- production:
8
- magic_number: 215
1
+ !@#!2
@@ -2,34 +2,28 @@ require "spec_helper"
2
2
 
3
3
  module RockConfig
4
4
  describe Manager do
5
- let(:configuration) { Configuration.new }
5
+ let(:configuration) do
6
+ configuration = Configuration.new
7
+ configuration.scanned_directories << File.join(Dir.pwd, "spec", "fixtures")
8
+ configuration.config_loaders << YamlLoader.new
6
9
 
7
- it "returns config if found" do
8
- result = mock("Config")
9
- result.should_receive(:send).with("development") { "yay" }
10
-
11
- scanner = mock("Scanner")
12
- scanner.should_receive(:new) .with(configuration) { scanner }
13
- scanner.should_receive(:find).with("sample") { result }
14
-
15
- manager = Manager.new(configuration, scanner)
16
- manager_result = manager.fetch "sample", "development"
10
+ configuration
11
+ end
17
12
 
18
- manager_result.should eq("yay")
13
+ it "returns config if found" do
14
+ manager = Manager.new(configuration)
15
+ manager_result = manager.fetch "database", "test"
16
+ manager_result.should_not be_nil
19
17
  end
20
18
 
21
19
  it "raises error if the config doesnt have the environment" do
22
- result = mock("Config")
23
- result.should_receive(:send).with("development") { nil }
24
-
25
- scanner = mock("Scanner")
26
- scanner.should_receive(:new) .with(configuration) { scanner }
27
- scanner.should_receive(:find).with("sample") { result }
20
+ manager = Manager.new(configuration)
21
+ manager_result = manager.fetch "database", "development"
28
22
 
29
- manager = Manager.new(configuration, scanner)
30
23
  expect do
31
- manager_result = manager.fetch "sample", "development"
32
- end.to raise_error(EnvironmentNotFoundError)
24
+ manager_result = manager.fetch "database", "me no exist yo"
25
+ end.to raise_error(EnvironmentNotFoundError,
26
+ 'Environment "me no exist yo" not found for config file "database"')
33
27
  end
34
28
  end
35
29
  end
@@ -8,7 +8,10 @@ module RockConfig
8
8
  config.config_loaders << YamlLoader.new
9
9
  end
10
10
 
11
- RockConfig.for("database", "development").magic_number.should eq(213)
11
+ config = RockConfig.for("database", "development")
12
+ config.magic_number.should eq(213)
13
+ config.test_false.should == false
14
+ config.test_true.should == true
12
15
  end
13
16
  end
14
17
  end
@@ -15,7 +15,7 @@ module RockConfig
15
15
  it "finds nothing when the target doesnt exist" do
16
16
  expect do
17
17
  scanner.find("database")
18
- end.to raise_error(ConfigNotFoundError)
18
+ end.to raise_error(ConfigNotFoundError, 'Config file "database" not found')
19
19
  end
20
20
 
21
21
  it "scans additional directories" do
@@ -11,6 +11,15 @@ module RockConfig
11
11
  config.should_not be_nil
12
12
  end
13
13
 
14
+ it "evaluates erb expression within the file" do
15
+ directory = File.join(Dir.pwd, "spec", "fixtures")
16
+
17
+ ENV['magic_number'] = '213'
18
+ loader = YamlLoader.new
19
+ config = loader.find_at(directory, "database_erb")
20
+ config.raw['development']['magic_number'].should eq(213)
21
+ end
22
+
14
23
  it "throws exception when given valid path to an invalid file" do
15
24
  directory = File.join(Dir.pwd, "spec", "fixtures")
16
25
  loader = YamlLoader.new
metadata CHANGED
@@ -1,62 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rock_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jiri Pospisil
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-03 00:00:00.000000000 Z
11
+ date: 2020-08-28 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 13.0.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 13.0.1
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rspec
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
- - - ~>
31
+ - - "~>"
20
32
  - !ruby/object:Gem::Version
21
33
  version: 2.11.0
22
34
  type: :development
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ~>
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
40
  version: 2.11.0
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: pry
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ~>
45
+ - - "~>"
36
46
  - !ruby/object:Gem::Version
37
47
  version: 0.9.10
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ~>
52
+ - - "~>"
44
53
  - !ruby/object:Gem::Version
45
54
  version: 0.9.10
46
55
  - !ruby/object:Gem::Dependency
47
56
  name: pry-nav
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ~>
59
+ - - "~>"
52
60
  - !ruby/object:Gem::Version
53
61
  version: 0.2.2
54
62
  type: :development
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ~>
66
+ - - "~>"
60
67
  - !ruby/object:Gem::Version
61
68
  version: 0.2.2
62
69
  description: RockConfig allows you to use custom config files easily.
@@ -66,8 +73,9 @@ executables: []
66
73
  extensions: []
67
74
  extra_rdoc_files: []
68
75
  files:
69
- - .gitignore
70
- - .rspec
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
71
79
  - Gemfile
72
80
  - LICENSE
73
81
  - README.md
@@ -86,41 +94,41 @@ files:
86
94
  - spec/configuration_spec.rb
87
95
  - spec/environment_detector_spec.rb
88
96
  - spec/fixtures/database.yml
97
+ - spec/fixtures/database_erb.yml
89
98
  - spec/fixtures/database_invalid.yml
90
99
  - spec/manager_spec.rb
91
100
  - spec/rock_config_spec.rb
92
101
  - spec/scanner_spec.rb
93
102
  - spec/spec_helper.rb
94
103
  - spec/yaml_loader_spec.rb
95
- homepage: ''
104
+ homepage: https://github.com/mekishizufu/rock_config
96
105
  licenses: []
106
+ metadata: {}
97
107
  post_install_message:
98
108
  rdoc_options: []
99
109
  require_paths:
100
110
  - lib
101
111
  required_ruby_version: !ruby/object:Gem::Requirement
102
- none: false
103
112
  requirements:
104
- - - ! '>='
113
+ - - ">="
105
114
  - !ruby/object:Gem::Version
106
115
  version: '0'
107
116
  required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
117
  requirements:
110
- - - ! '>='
118
+ - - ">="
111
119
  - !ruby/object:Gem::Version
112
120
  version: '0'
113
121
  requirements: []
114
- rubyforge_project:
115
- rubygems_version: 1.8.23
122
+ rubygems_version: 3.0.3
116
123
  signing_key:
117
- specification_version: 3
124
+ specification_version: 4
118
125
  summary: RockConfig allows you to use custom config files easily.
119
126
  test_files:
120
127
  - spec/config_spec.rb
121
128
  - spec/configuration_spec.rb
122
129
  - spec/environment_detector_spec.rb
123
130
  - spec/fixtures/database.yml
131
+ - spec/fixtures/database_erb.yml
124
132
  - spec/fixtures/database_invalid.yml
125
133
  - spec/manager_spec.rb
126
134
  - spec/rock_config_spec.rb