config_reader 0.0.9 → 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae749330c14b4055e3f45286b3c30036f5e8eeee
4
+ data.tar.gz: 87e0b4ec76ad3f9a923bd0124732f8de3909c499
5
+ SHA512:
6
+ metadata.gz: ce883b8f16a952fb8f9e459c43d2702e32f8f5c98fe2066b0363b3d676370926ef96b09380e97b5f65639a64e7699d49327e980840dc68d6e50e4e968bea9862
7
+ data.tar.gz: ddf297e0d4e4eb77f8e2d9b7af228c22b067034af11cd36ceaf3ed579ee8799b473ebf9598d10b213f21ce2bfb1f2cd7a4207f57cd36a7a767df97bd866703f8
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
+ .bundle
1
2
  pkg
2
3
  *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - '1.9.3'
4
+ - '2.0.0'
5
+ - '2.1.1'
data/Gemfile.lock CHANGED
@@ -1,18 +1,45 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- config_reader (0.0.9)
4
+ config_reader (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
+ arrayfields (4.9.2)
10
+ chronic (0.10.2)
11
+ coderay (1.1.0)
12
+ coerce (0.0.6)
13
+ chronic (>= 0.6.2)
14
+ fattr (2.2.2)
15
+ highline (1.6.21)
16
+ main (6.0.0)
17
+ arrayfields (>= 4.7.4)
18
+ chronic (>= 0.6.2)
19
+ fattr (>= 2.2.0)
20
+ map (>= 5.1.0)
21
+ map (6.5.3)
22
+ method_source (0.8.2)
23
+ pry (0.9.12.6)
24
+ coderay (~> 1.0)
25
+ method_source (~> 0.8)
26
+ slop (~> 3.4)
9
27
  rake (0.9.2.2)
10
28
  rspec (1.3.2)
29
+ sekrets (1.7.0)
30
+ coerce (>= 0.0.3)
31
+ fattr (>= 2.2.1)
32
+ highline (>= 1.6.15)
33
+ main (>= 5.1.1)
34
+ map (>= 6.3.0)
35
+ slop (3.5.0)
11
36
 
12
37
  PLATFORMS
13
38
  ruby
14
39
 
15
40
  DEPENDENCIES
16
41
  config_reader!
42
+ pry
17
43
  rake
18
44
  rspec (= 1.3.2)
45
+ sekrets (~> 1.7.0)
data/README.rdoc CHANGED
@@ -1,19 +1,21 @@
1
1
  = ConfigReader
2
2
 
3
+ {<img src="https://codeclimate.com/github/UnderpantsGnome/config_reader-gem.png" />}[https://codeclimate.com/github/UnderpantsGnome/config_reader-gem] {<img src="https://travis-ci.org/UnderpantsGnome/config_reader-gem.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/UnderpantsGnome/config_reader-gem]
4
+
3
5
  Provides a way to manage environment specific configuration settings. It will use the defaults for any environment and override any values you specify for an environment.
4
6
 
5
7
  Example config file:
6
8
 
7
9
  defaults:
8
10
  site_url: http://localhost:3000
9
- host_name: CHANGE-ME.com
10
- mail_from: noreply@CHANGE_ME.com
11
- site_name: CHANGE-ME
12
- admin_email: admin@CHANGE-ME.com
11
+ host_name: example.com
12
+ mail_from: noreply@example.com
13
+ site_name: example
14
+ admin_email: admin@example.com
13
15
  blackbird: true
14
16
 
15
17
  production:
16
- site_url: http://CHANGE-ME.com
18
+ site_url: http://example.com
17
19
  blackbird: false
18
20
 
19
21
  test:
@@ -25,13 +27,25 @@ Example class:
25
27
  self.config_file = 'config/my_config.yml'
26
28
  end
27
29
 
30
+ == Sekrets
31
+ Now also includes Sekrets integration. https://github.com/ahoward/sekrets
32
+
33
+ The format of the sekrets file is the same as the regular file.
34
+
35
+ Example class with Sekrets:
36
+
37
+ class MyConfig < ConfigReader
38
+ self.config_file = 'config/my_config.yml'
39
+ self.sekrets_file = 'config/my_config.yml.enc'
40
+ end
41
+
28
42
  Usage:
29
43
 
30
- MyConfig.mail_from #=> noreply@CHANGE_ME.com
31
- MyConfig[:mail_from] #=> noreply@CHANGE_ME.com
44
+ MyConfig.mail_from #=> noreply@example.com
45
+ MyConfig[:mail_from] #=> noreply@example.com
32
46
 
33
47
  == Note on Patches/Pull Requests
34
-
48
+
35
49
  * Fork the project.
36
50
  * Make your feature addition or bug fix.
37
51
  * Add tests for it. This is important so I don't break it in a
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
3
  require 'spec/rake/spectask'
4
4
  Spec::Rake::SpecTask.new(:spec) do |spec|
@@ -1,24 +1,26 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "config_reader/version"
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'config_reader/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "config_reader"
6
+ s.name = 'config_reader'
7
7
  s.version = ConfigReader::VERSION
8
- s.authors = ["Michael Moen"]
9
- s.email = ["michael@underpantsgnome.com"]
10
- s.homepage = ""
8
+ s.authors = ['Michael Moen']
9
+ s.email = ['michael@underpantsgnome.com']
10
+ s.homepage = ''
11
11
  s.summary = %q{Provides a way to manage environment specific configuration settings.}
12
12
  s.description = %q{Provides a way to manage environment specific configuration settings.}
13
13
 
14
- s.rubyforge_project = "config_reader"
14
+ s.rubyforge_project = 'config_reader'
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
19
+ s.require_paths = ['lib']
20
20
 
21
21
  # specify any dependencies here; for example:
22
- s.add_development_dependency "rake"
23
- s.add_development_dependency "rspec", "1.3.2"
22
+ s.add_development_dependency 'rake'
23
+ s.add_development_dependency 'rspec', '1.3.2'
24
+ s.add_development_dependency 'sekrets', '~> 1.7.0'
25
+ s.add_development_dependency 'pry'
24
26
  end
@@ -1,3 +1,3 @@
1
1
  class ConfigReader
2
- VERSION = "0.0.9"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/config_reader.rb CHANGED
@@ -9,10 +9,9 @@ rescue LoadError
9
9
  end
10
10
 
11
11
  class ConfigReader
12
- @config_file = nil
13
- @config = nil
14
-
15
12
  class << self
13
+ attr_accessor :config_file, :config, :sekrets_file
14
+
16
15
  def config
17
16
  @config = nil unless defined?(@config)
18
17
  @config ||= reload
@@ -23,27 +22,9 @@ class ConfigReader
23
22
  end
24
23
 
25
24
  def reload
26
- raise 'No config file set' unless @config_file
27
-
28
- if defined?(ERB)
29
- conf = YAML.load(ERB.new(File.open(find_config).read).result)
30
- else
31
- conf = YAML.load(File.open(find_config).read)
32
- end
33
-
34
- raise 'No config found' unless conf
35
-
36
- if defined?(Rails) && Rails.env
37
- env = Rails.env
38
- elsif defined?(RAILS_ENV)
39
- env = RAILS_ENV
40
- elsif defined?(APP_ENV)
41
- env = APP_ENV
42
- end
43
-
44
- _conf = conf['defaults']
45
- _conf.merge!(conf[env]) if conf[env]
46
- ConfigReader::MagicHash.convert_hash(_conf)
25
+ # require 'pry'
26
+ # binding.pry
27
+ merge_configs(find_env, load_config, load_sekrets)
47
28
  end
48
29
 
49
30
  def [](key)
@@ -57,6 +38,7 @@ class ConfigReader
57
38
  config_file = File.join(dir, @config_file)
58
39
  return config_file if File.exist?(config_file)
59
40
  end
41
+
60
42
  ''
61
43
  end
62
44
 
@@ -67,5 +49,59 @@ class ConfigReader
67
49
  def inspect
68
50
  puts config.inspect
69
51
  end
52
+
53
+ def find_env
54
+ if defined?(Rails) && Rails.env
55
+ Rails.env
56
+ elsif defined?(RAILS_ENV)
57
+ RAILS_ENV
58
+ elsif defined?(Padrino) && Padrino.env
59
+ Padrino.env.to_s
60
+ elsif defined?(PADRINO_ENV)
61
+ PADRINO_ENV
62
+ elsif ENV['RACK_ENV']
63
+ ENV['RACK_ENV']
64
+ elsif defined?(APP_ENV)
65
+ APP_ENV
66
+ end
67
+ end
68
+
69
+ def load_config
70
+ raise 'No config file set' unless @config_file
71
+
72
+ if defined?(ERB)
73
+ conf = YAML.load(ERB.new(File.open(find_config).read).result)
74
+ else
75
+ conf = YAML.load(File.open(find_config).read)
76
+ end
77
+
78
+ raise 'No config found' unless conf
79
+
80
+ conf
81
+ end
82
+
83
+ def load_sekrets
84
+ sekrets = {}
85
+
86
+ if @sekrets_file
87
+ begin
88
+ require 'sekrets'
89
+ sekrets = ::Sekrets.settings_for(@sekrets_file)
90
+ raise 'No sekrets found' unless sekrets
91
+ rescue LoadError
92
+ $stderr.puts "You specified a sekrets_file, but the sekrets gem isn't available."
93
+ end
94
+ end
95
+
96
+ sekrets
97
+ end
98
+
99
+ def merge_configs(env, conf, sekrets)
100
+ _conf = conf['defaults']
101
+ _conf.merge!(sekrets['defaults']) if sekrets && sekrets['defaults']
102
+ _conf.merge!(conf[env]) if conf[env]
103
+ _conf.merge!(sekrets[env]) if sekrets && sekrets[env]
104
+ ConfigReader::MagicHash.convert_hash(_conf)
105
+ end
70
106
  end
71
107
  end
@@ -3,18 +3,18 @@ require 'spec_helper'
3
3
  describe "ConfigReader" do
4
4
  describe "parsing a YAML file" do
5
5
  it "should find values with method_missing" do
6
- TestConfig.app_name.should == 'default_app'
6
+ TestConfig.app_name.should == 'test_app'
7
7
  end
8
8
 
9
9
  it "should find values using [] and a string" do
10
- TestConfig['app_name'].should == 'default_app'
10
+ TestConfig['app_name'].should == 'test_app'
11
11
  end
12
12
 
13
13
  it "should find values using [] and a symbol" do
14
- TestConfig[:app_name].should == 'default_app'
14
+ TestConfig[:app_name].should == 'test_app'
15
15
  end
16
16
 
17
- it "should find nested values using using method_missing" do
17
+ it "should find nested values using method_missing" do
18
18
  TestConfig.nested_key.value.should == 'test'
19
19
  end
20
20
 
@@ -25,5 +25,60 @@ describe "ConfigReader" do
25
25
  it "should find nested values using [] and a string" do
26
26
  TestConfig['nested_key']['value'].should == 'test'
27
27
  end
28
+
29
+ it "should not find sekrets only nested values using method_missing" do
30
+ lambda { TestConfig.sekrets_only }.should raise_error
31
+ end
32
+
33
+ it "should not find sekrets only nested values using [] and a symbol" do
34
+ lambda { TestConfig[:sekrets_only][:value] }.should raise_error
35
+ end
36
+
37
+ it "should not find sekrets only nested values using [] and a string" do
38
+ lambda { TestConfig['sekrets_only']['value'] }.should raise_error
39
+ end
40
+
41
+ end
42
+
43
+ context 'using sekrets' do
44
+ ENV['SEKRETS_KEY'] = 'shhh'
45
+
46
+ describe "parsing a YAML file" do
47
+ it "should find values with method_missing" do
48
+ SekretsConfig.app_name.should == 'test_app_sekret'
49
+ end
50
+
51
+ it "should find values using [] and a string" do
52
+ SekretsConfig['app_name'].should == 'test_app_sekret'
53
+ end
54
+
55
+ it "should find values using [] and a symbol" do
56
+ SekretsConfig[:app_name].should == 'test_app_sekret'
57
+ end
58
+
59
+ it "should find nested values using method_missing" do
60
+ SekretsConfig.nested_key.value.should == 'test_sekret'
61
+ end
62
+
63
+ it "should find nested values using [] and a symbol" do
64
+ SekretsConfig[:nested_key][:value].should == 'test_sekret'
65
+ end
66
+
67
+ it "should find nested values using [] and a string" do
68
+ SekretsConfig['nested_key']['value'].should == 'test_sekret'
69
+ end
70
+
71
+ it "should find sekrets only nested values using method_missing" do
72
+ SekretsConfig.nested_key.value.should == 'test_sekret'
73
+ end
74
+
75
+ it "should find sekrets only nested values using [] and a symbol" do
76
+ SekretsConfig[:nested_key][:value].should == 'test_sekret'
77
+ end
78
+
79
+ it "should find sekrets only nested values using [] and a string" do
80
+ SekretsConfig['nested_key']['value'].should == 'test_sekret'
81
+ end
82
+ end
28
83
  end
29
84
  end
@@ -0,0 +1,4 @@
1
+ class SekretsConfig < ConfigReader
2
+ self.config_file = 'spec/test_config.yml'
3
+ self.sekrets_file = 'spec/sekrets_config.yml.enc'
4
+ end
Binary file
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'spec'
4
4
  # require 'spec/autorun'
5
5
  require 'config_reader'
6
6
  require 'test_config'
7
+ require 'sekrets_config'
7
8
 
8
- Spec::Runner.configure do |config|
9
-
10
- end
9
+ ENV['RACK_ENV'] = 'test'
10
+ ENV['SEKRETS_KEY'] = 'shhh'
data/spec/test_config.yml CHANGED
@@ -1,8 +1,8 @@
1
- ---
1
+ ---
2
2
  defaults:
3
3
  app_name: default_app
4
- nested_key:
5
- value: 'test'
4
+ nested_key:
5
+ value: test
6
6
 
7
7
  test:
8
8
  app_name: test_app
metadata CHANGED
@@ -1,38 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Michael Moen
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-02-03 00:00:00.000000000Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
- requirement: &70272720215240 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70272720215240
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rspec
27
- requirement: &70272720194600 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - =
31
+ - - '='
31
32
  - !ruby/object:Gem::Version
32
33
  version: 1.3.2
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70272720194600
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: sekrets
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.7.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
36
69
  description: Provides a way to manage environment specific configuration settings.
37
70
  email:
38
71
  - michael@underpantsgnome.com
@@ -40,7 +73,8 @@ executables: []
40
73
  extensions: []
41
74
  extra_rdoc_files: []
42
75
  files:
43
- - .gitignore
76
+ - ".gitignore"
77
+ - ".travis.yml"
44
78
  - Gemfile
45
79
  - Gemfile.lock
46
80
  - History.rdoc
@@ -52,43 +86,38 @@ files:
52
86
  - lib/config_reader/magic_hash.rb
53
87
  - lib/config_reader/version.rb
54
88
  - spec/config_reader_spec.rb
55
- - spec/spec.opts
89
+ - spec/sekrets_config.rb
90
+ - spec/sekrets_config.yml.enc
56
91
  - spec/spec_helper.rb
57
92
  - spec/test_config.rb
58
93
  - spec/test_config.yml
59
94
  homepage: ''
60
95
  licenses: []
96
+ metadata: {}
61
97
  post_install_message:
62
98
  rdoc_options: []
63
99
  require_paths:
64
100
  - lib
65
101
  required_ruby_version: !ruby/object:Gem::Requirement
66
- none: false
67
102
  requirements:
68
- - - ! '>='
103
+ - - ">="
69
104
  - !ruby/object:Gem::Version
70
105
  version: '0'
71
- segments:
72
- - 0
73
- hash: -4112497528289826160
74
106
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
107
  requirements:
77
- - - ! '>='
108
+ - - ">="
78
109
  - !ruby/object:Gem::Version
79
110
  version: '0'
80
- segments:
81
- - 0
82
- hash: -4112497528289826160
83
111
  requirements: []
84
112
  rubyforge_project: config_reader
85
- rubygems_version: 1.8.10
113
+ rubygems_version: 2.2.2
86
114
  signing_key:
87
- specification_version: 3
115
+ specification_version: 4
88
116
  summary: Provides a way to manage environment specific configuration settings.
89
117
  test_files:
90
118
  - spec/config_reader_spec.rb
91
- - spec/spec.opts
119
+ - spec/sekrets_config.rb
120
+ - spec/sekrets_config.yml.enc
92
121
  - spec/spec_helper.rb
93
122
  - spec/test_config.rb
94
123
  - spec/test_config.yml
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --colour