basic_config 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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in basic_config.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stepan Tubanov
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,99 @@
1
+ # BasicConfig
2
+
3
+ Friendly configuration wrapper. If you find yourself using things like:
4
+
5
+ ```ruby
6
+ AppConfig = YAML.load_file('config/app.yml')[environment].symbolize_keys
7
+ ```
8
+
9
+ then you might like this.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'basic_config'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it via RubyGems as:
22
+
23
+ $ gem install basic_config
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ settings = BasicConfig.load_file('config.yml')
29
+
30
+ # Access your configuration with simple method calls
31
+ settings.some_param
32
+
33
+ # At any level of nesting
34
+ settings.smtp.address
35
+
36
+ # Hash access also works
37
+ settings[:smtp][:address]
38
+
39
+ # And you can check if particular key exists
40
+ puts('Yes') if settings.include?(:smtp)
41
+ ```
42
+
43
+ If your file has sections for different environments:
44
+ ```
45
+ development:
46
+ host: localhost
47
+ port: 123
48
+ test
49
+ host: localhost
50
+ port: 456
51
+ ```
52
+ then you can load the right environment with `load_env`:
53
+ ```ruby
54
+ AppConfig = BasicConfig.load_env('config.yml', Rails.env)
55
+ ```
56
+
57
+ ## Why should I use it instead of plain Hash variables?
58
+
59
+ ### It raises errors when you unintentionally read non-existent keys:
60
+
61
+ If you are using a `Hash`:
62
+ ```ruby
63
+ secret_token = AppConfig[:something]
64
+ ```
65
+ and for some reason your configuration does not have `:something` in it - you'll
66
+ get a `nil`. Worst case: this `nil` will live inside your system compromising
67
+ or corrupting some data until you finally notice and track it down back to this line.
68
+
69
+ If you are using a `BasicConfig`:
70
+ ```ruby
71
+ secret_token = AppConfig.something
72
+ ```
73
+ you'll get `NoMethodError` in that particular line with the name of the key that
74
+ is missing.
75
+
76
+ *Note:* There is also an `include?` method which you can use to check if
77
+ particular key exist in your config - `AppConfig.include?(:something)`.
78
+ Additionaly, for some keys it makes sense to get a `nil` when they do not exist and for this
79
+ purpose there is a `[]` method which is delegated to underlying hash.
80
+
81
+ ### Works recursively.
82
+
83
+ If your YAML is more than 1 level deep then simple `symbolize_keys` is not going to be enough:
84
+ ```ruby
85
+ AppConfig[:something]['have_to_use_string_here']
86
+ ```
87
+
88
+ With BasicConfig above would look like this:
89
+ ```ruby
90
+ AppConfig.something.have_to_use_string_here
91
+ ```
92
+
93
+ ### Easier to test.
94
+
95
+ You can stub out any config variable just like a normal method in your tests.
96
+
97
+ ```ruby
98
+ AppConfig.stub(:something).and_return('anything')
99
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/basic_config', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Stepan Tubanov']
6
+ gem.email = ['stepan773@gmail.com']
7
+ gem.description = 'Makes it easier to use configuration by wrapping it in a struct-like object'
8
+ gem.summary = 'Friendly configuration wrapper'
9
+ gem.homepage = 'https://github.com/stephan778/basic_config'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.test_files = gem.files.grep(%r{^(spec)/})
13
+ gem.name = 'basic_config'
14
+ gem.require_path = 'lib'
15
+ gem.version = '0.0.1'
16
+
17
+ gem.add_development_dependency('rspec', '~> 2.10')
18
+ end
@@ -0,0 +1,54 @@
1
+ require 'yaml'
2
+
3
+ class BasicConfig
4
+ def initialize(hash)
5
+ raise ArgumentError, 'Hash can not be nil' if hash.nil?
6
+
7
+ # Symbolize keys: don't want to add ActiveSupport dependency just for this.
8
+ @hash = hash.inject({}) do |h, (key, value)|
9
+ h[key.to_sym] = value
10
+ h
11
+ end
12
+
13
+ @hash.each do |key, value|
14
+ @hash[key] = BasicConfig.new(value) if value.is_a?(Hash)
15
+ end
16
+ end
17
+
18
+ def [](key)
19
+ @hash[key]
20
+ end
21
+
22
+ def include?(key)
23
+ @hash.has_key?(key)
24
+ end
25
+
26
+ def method_missing(meth, *args, &block)
27
+ if include?(meth)
28
+ raise ArgumentError, 'Getter can not receive any arguments' if !args.empty? || block_given?
29
+ @hash[meth]
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def respond_to?(meth)
36
+ include?(meth) or super
37
+ end
38
+
39
+ def to_hash
40
+ @hash.dup.tap do |h|
41
+ h.each do |key, value|
42
+ h[key] = value.to_hash if value.is_a?(BasicConfig)
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.load_file(name)
48
+ BasicConfig.new(YAML.load_file(name))
49
+ end
50
+
51
+ def self.load_env(name, env)
52
+ BasicConfig.new(YAML.load_file(name)[env])
53
+ end
54
+ end
@@ -0,0 +1,76 @@
1
+ require_relative '../lib/basic_config'
2
+ require 'active_support/core_ext/hash'
3
+
4
+ describe BasicConfig do
5
+ let(:hash) do
6
+ {
7
+ 'one' => 'something',
8
+ 'two' => 'other_value',
9
+ 'three' => {
10
+ 'nested' => '123'
11
+ }
12
+ }
13
+ end
14
+
15
+ subject { BasicConfig.new(hash) }
16
+
17
+ it 'can not be created with nil' do
18
+ expect { BasicConfig.new(nil) }.to raise_error(ArgumentError)
19
+ end
20
+
21
+ it 'provides with getters for keys' do
22
+ subject.one.should == 'something'
23
+ subject.two.should == 'other_value'
24
+ end
25
+
26
+ it 'symbolizes keys' do
27
+ subject.to_hash.keys.first.should be_a Symbol
28
+ end
29
+
30
+ it 'transforms nested hashes into BasicConfigs' do
31
+ subject.three.should be_a BasicConfig
32
+ subject.three.nested.should == '123'
33
+ end
34
+
35
+ it 'correctly handles respond_to? queries' do
36
+ subject.should respond_to :one
37
+ subject.should respond_to :three
38
+ subject.should_not respond_to :unknown
39
+ end
40
+
41
+ it 'raises NoMethodError for unknown keys' do
42
+ expect { subject.four }.to raise_error(NoMethodError)
43
+ end
44
+
45
+ it 'can be converted back to hash' do
46
+ converted = subject.to_hash.stringify_keys
47
+ converted['three'] = converted['three'].stringify_keys
48
+ converted.should == hash
49
+ end
50
+
51
+ describe '::load_file' do
52
+ it 'uses YAML to load files' do
53
+ YAML.should_receive(:load_file).with('file').and_return(:content)
54
+ BasicConfig.should_receive(:new).with(:content)
55
+ BasicConfig.load_file('file')
56
+ end
57
+ end
58
+
59
+ describe '::load_env' do
60
+ let(:content) do
61
+ {
62
+ 'development' => {
63
+ 'value' => 'x'
64
+ },
65
+ 'test' => {
66
+ 'value' => 'y'
67
+ }
68
+ }
69
+ end
70
+ it 'selects env section from YAML loaded file' do
71
+ YAML.should_receive(:load_file).with('file').and_return(content)
72
+ BasicConfig.should_receive(:new).with(content['development'])
73
+ BasicConfig.load_env('file', 'development')
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stepan Tubanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-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: '2.10'
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: '2.10'
30
+ description: Makes it easier to use configuration by wrapping it in a struct-like
31
+ object
32
+ email:
33
+ - stepan773@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - basic_config.gemspec
44
+ - lib/basic_config.rb
45
+ - spec/basic_config_spec.rb
46
+ homepage: https://github.com/stephan778/basic_config
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.19
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Friendly configuration wrapper
70
+ test_files:
71
+ - spec/basic_config_spec.rb