settings-env-loader 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in settings-env-loader.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ian Yang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Settings::Env::Loader
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'settings-env-loader'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install settings-env-loader
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
@@ -0,0 +1,79 @@
1
+ require "enumerator"
2
+
3
+ module Settings
4
+ module Env
5
+ module Loader
6
+ VERSION = "0.0.1"
7
+
8
+ # Merges ENV and modify the hash directly
9
+ def merge_env(prefix = nil, separator = '_')
10
+ Merger.new(prefix, separator, self).merge
11
+ self
12
+ end
13
+
14
+ # Iterates the Hash as ENV key value pairs
15
+ def each_env(prefix = nil, separator = '_')
16
+ Enumerator.new do |y|
17
+ Exporter.new(prefix, separator, self).each(y)
18
+ end
19
+ end
20
+
21
+ class Merger
22
+ def initialize(prefix, separator, hash)
23
+ @prefix = prefix
24
+ @separator = separator
25
+ @hash = hash
26
+ end
27
+
28
+ def merge
29
+ @hash.each do |key, value|
30
+ env_name = [@prefix, key].compact.join(@separator).upcase
31
+
32
+ if value.is_a?(Hash)
33
+ Merger.new(env_name, @separator, value).merge
34
+ else
35
+ env_value = ENV[env_name]
36
+ if env_value
37
+ @hash[key] = convert(env_value, value)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def convert(value, origin_value)
44
+ case origin_value
45
+ when Symbol
46
+ value.to_sym
47
+ when Fixnum
48
+ value.to_i
49
+ when Float
50
+ value.to_f
51
+ when Array
52
+ Array(YAML.load(value))
53
+ else
54
+ value
55
+ end
56
+ end
57
+ end
58
+
59
+ class Exporter
60
+ def initialize(prefix, separator, hash)
61
+ @prefix = prefix
62
+ @separator = separator
63
+ @hash = hash
64
+ end
65
+
66
+ def each(y)
67
+ @hash.each do |key, value|
68
+ env_name = [@prefix, key].compact.join(@separator).upcase
69
+ if value.is_a?(Hash)
70
+ Exporter.new(env_name, @separator, value).each(y)
71
+ else
72
+ y << [env_name, value.to_s]
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1 @@
1
+ require "settings-env-loader"
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'settings-env-loader'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "settings-env-loader"
8
+ gem.version = Settings::Env::Loader::VERSION
9
+ gem.authors = ["Ian Yang"]
10
+ gem.email = ["me@iany.me"]
11
+ gem.description = %q{Scan ENV and override correspondong settings in a nested Hash}
12
+ gem.summary = %q{
13
+ The Hash must provide all configuration options. It
14
+ eases the deployment to platform such as heroku.
15
+
16
+ It is recommended to use settingslogic to load settings first, and then
17
+ merge ENV into it.
18
+ }
19
+ gem.homepage = "https://github.com/doitian/settings-env-loader"
20
+
21
+ gem.files = `git ls-files`.split($/)
22
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
+ gem.require_paths = ["lib"]
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'rake'
27
+ end
@@ -0,0 +1,118 @@
1
+ require 'settings-env-loader'
2
+
3
+ describe Settings::Env::Loader do
4
+ let(:hash) do
5
+ {
6
+ :float => 1.2,
7
+ :fixnum => 20,
8
+ :array => [1, 2],
9
+ :hash => {
10
+ key: 'name',
11
+ value: 'settings-env-loader'
12
+ }
13
+ }
14
+ end
15
+
16
+ subject { hash }
17
+
18
+ before do
19
+ hash.extend Settings::Env::Loader
20
+ end
21
+
22
+ before do
23
+ $remember_env = ENV.to_hash
24
+ ENV.replace $remember_env.dup
25
+ end
26
+
27
+ after do
28
+ ENV.replace $remember_env
29
+ end
30
+
31
+ describe 'merge_env' do
32
+ context 'when prefix is nil' do
33
+ context 'ENV[FIXNUM] = 1' do
34
+ before do
35
+ ENV['FIXNUM'] = '1'
36
+ hash.merge_env
37
+ end
38
+
39
+ its([:fixnum]) { should == 1 }
40
+ end
41
+ context 'ENV[FIXNUM] = 1.1' do
42
+ before do
43
+ ENV['FIXNUM'] = '1.1'
44
+ hash.merge_env
45
+ end
46
+
47
+ its([:fixnum]) { should == 1 }
48
+ end
49
+ context 'ENV[FLOAT] = 1.5' do
50
+ before do
51
+ ENV['FLOAT'] = '1.5'
52
+ hash.merge_env
53
+ end
54
+
55
+ its([:float]) { should == 1.5 }
56
+ end
57
+ context 'ENV[ARRAY] = [10,20]' do
58
+ before do
59
+ ENV['ARRAY'] = '[10,20]'
60
+ hash.merge_env
61
+ end
62
+
63
+ its([:array]) { should == [10,20] }
64
+ end
65
+ context 'ENV[HASH_KEY] = key' do
66
+ before do
67
+ ENV['HASH_KEY'] = 'key'
68
+ hash.merge_env
69
+ end
70
+
71
+ its([:hash]) { should == { :key => 'key', :value => 'settings-env-loader'} }
72
+ end
73
+
74
+ context 'when no ENV' do
75
+ it 'should not be changed' do
76
+ before = hash.dup
77
+ hash.merge_env
78
+ hash.should == before
79
+ end
80
+ end
81
+ end
82
+
83
+ context 'when prefix is SETTINGS' do
84
+ context 'ENV[FIXNUM] = 1' do
85
+ before do
86
+ ENV['FIXNUM'] = '1'
87
+ hash.merge_env('SETTINGS')
88
+ end
89
+
90
+ its([:fixnum]) { should == 20 }
91
+ end
92
+ context 'ENV[SETTINGS_FIXNUM] = 1' do
93
+ before do
94
+ ENV['SETTINGS_FIXNUM'] = '1'
95
+ hash.merge_env('SETTINGS')
96
+ end
97
+
98
+ its([:fixnum]) { should == 1 }
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'each_env' do
104
+ context 'when prefix is nil' do
105
+ subject { Hash[*hash.each_env.to_a.flatten] }
106
+ its(['FLOAT']) { should == '1.2' }
107
+ its(['FIXNUM']) { should == '20' }
108
+ its(['ARRAY']) { should == '[1, 2]' }
109
+ its(['HASH_KEY']) { should == 'name' }
110
+ its(['HASH_VALUE']) { should =='settings-env-loader' }
111
+ end
112
+ context 'when prefix is SETTINGS' do
113
+ subject { Hash[*hash.each_env('SETTINGS').to_a.flatten] }
114
+ its(['FIXNUM']) { should be_nil }
115
+ its(['SETTINGS_FIXNUM']) { should == '20' }
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings-env-loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Yang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Scan ENV and override correspondong settings in a nested Hash
47
+ email:
48
+ - me@iany.me
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/settings-env-loader.rb
60
+ - lib/settings_env_loader.rb
61
+ - settings-env-loader.gemspec
62
+ - spec/lib/settings-env-loader_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/doitian/settings-env-loader
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ segments:
77
+ - 0
78
+ hash: -1968224689981837943
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -1968224689981837943
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: The Hash must provide all configuration options. It eases the deployment
94
+ to platform such as heroku. It is recommended to use settingslogic to load settings
95
+ first, and then merge ENV into it.
96
+ test_files:
97
+ - spec/lib/settings-env-loader_spec.rb
98
+ - spec/spec_helper.rb