configurethis 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73d8b25e91d50bd79c5daf51fefece507c39ace8
4
+ data.tar.gz: fe0402a579f5a74cfe79c7e741d64ea5f9628ea5
5
+ SHA512:
6
+ metadata.gz: dbac3f113ac7620eb98d1a1068ab23f3420497cfee0e737ca0ed98a0729eaaa9e0631a5ae5054cff412d663128d1bcce5f82f2ad0d86cb4d7d11eae853f4340e
7
+ data.tar.gz: 85eb1c79f1a913fd3e7f165249b9909314f73cb67c6196a812146d6f4b382c1e46605dfe5f6febcaa59b3d325825d48d3b07f54225aaecaed5ed5c8492ac883a
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 documentation
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ - "1.9.3"
5
+ script:
6
+ - rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in configurethis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matt Snyder
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,116 @@
1
+ [![Build Status](https://travis-ci.org/mattsnyder/configurethis.png?branch=master)](https://travis-ci.org/mattsnyder/configurethis)
2
+ # Configurethis
3
+ Clean up your configuration approach by using Configurethis. Configurethis allows you to access your config values using
4
+ method names instead of string literals to identify which config value you want to retrieve.
5
+
6
+ Typical Ruby code has craziness like this all over the place:
7
+ ```ruby
8
+ # config/initializers/load_config.rb
9
+ APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
10
+
11
+ # application.rb
12
+ if APP_CONFIG['perform_authentication']
13
+ # Do stuff
14
+ end
15
+ ```
16
+ But that's just ugly. It relies on string literals, constants, and gets worse if you have multiple config values, or even nested config values for that matter.
17
+ `Configurethis` makes your life better and your code healthier by avoiding literals, constants and makes it easy to setup multiple config files.
18
+ All you need to do is define a Class to act as your configuration container object, create a matching YAML file, and then access your values
19
+ as needed using method calls to your new Class!
20
+ ```ruby
21
+ # A cleaner way!
22
+ # config/initializers/configurethis.rb
23
+ Configurethis.root_path = File.join(Rails.root, "config")
24
+
25
+ # lib/password_settings.rb
26
+ class PasswordSettings
27
+ extend Configurethis
28
+ end
29
+
30
+ # some_ruby_file_in_my_app.rb
31
+ PasswordSettings.min_length #=> 10
32
+ PasswordSettings.require_uppercase #=> true
33
+ PasswordSettings.storage.keep_last #=> 3
34
+ ```
35
+ ```yml
36
+ ---
37
+ # config/password_settings.yml
38
+ minLength: 10
39
+ require_uppercase: true
40
+ storage:
41
+ keep_last: 3
42
+ ```
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ gem 'configurethis'
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install configurethis
57
+
58
+ ## Usage
59
+
60
+ ### Setup
61
+ To get started, specify where you want `Configurethis` to look for your .yml files at.
62
+ ```ruby
63
+ Configurethis.root_path = '/etc/my_app'
64
+ ```
65
+
66
+ If you are using `Configurethis` in a Rails app, create an initializer for `Configurethis` and add the following:
67
+ ```ruby
68
+ # config/initializers/configurethis.rb
69
+ Configurethis.root_path = File.join(Rails.root, "config")
70
+ ```
71
+
72
+ If your configuration is dependent on environment variables, you can specify it (Unfortunately at this time you need to do this once per each class that is environmentally dependent):
73
+ ```ruby
74
+ MyConfigurationClass.set_root = RAILS_ENV
75
+ ```
76
+
77
+ ### Creating a configuration
78
+ To turn your class into a configuration class, simply extend `Configurethis`
79
+ ```ruby
80
+ class MyRiakConfiguration
81
+ extend Configurethis
82
+ end
83
+ ```
84
+ And create a `.yml` file that matches your class name. In this case our `.yml` file would be named `my_riak_configuration.yml`:
85
+ And it might contain something like this:
86
+ ```yml
87
+ ---
88
+ pb_port: 9002
89
+ http_port: 9000
90
+ host: 127.0.0.1
91
+ riak_control:
92
+ cert: /opt/local/var/riak-1.2.0/riak.crt
93
+ ```
94
+ Now you can access those values as methods off your configuration class.
95
+ ```ruby
96
+ MyRiakConfiguration.pb_port #=> 9002
97
+ MyRiakConfiguration.http_port #=> 9000
98
+ MyRiakConfiguration.riak_control.cert #=> "/opt/local/var/riak-1.2.0/riak.crt"
99
+ ```
100
+
101
+ ### Overriding
102
+ If you do not want your `.yml` file to follow convention, you can choose your own name.
103
+ ```ruby
104
+ class IWantToBeDifferent
105
+ extend Configurethis
106
+ configure_this_with 'my_configuration_file.yml'
107
+ end
108
+ ```
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'configurethis/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "configurethis"
8
+ gem.version = Configurethis::VERSION
9
+ gem.authors = ["Matt Snyder"]
10
+ gem.email = ["snyder2112@me.com"]
11
+ gem.description = %q{Clean up your configuration approach by using Configurethis. Configurethis allows you to access your config values using method names instead of string literals to identify which config value you want to retrieve.}
12
+ gem.summary = gem.description
13
+ gem.homepage = "https://github.com/mattsnyder/configurethis"
14
+
15
+ gem.add_development_dependency "rspec-given", ">= 3.0.0"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,60 @@
1
+ require "configurethis/version"
2
+ require "configurethis/value_container"
3
+ require "configurethis/configuration"
4
+ require "configurethis/configurethis_properties"
5
+
6
+ module Configurethis
7
+ class << self
8
+ def root_path=(path)
9
+ ConfigurethisProperties.root_path = path
10
+ end
11
+
12
+ def use_defaults
13
+ ConfigurethisProperties.use_defaults
14
+ end
15
+ end
16
+
17
+ def configuration_path
18
+ configuration.path
19
+ end
20
+
21
+ def set_root=(key)
22
+ configuration.root = key.to_s
23
+ end
24
+
25
+ def configure_this_with(path)
26
+ @configuration_file = path
27
+ end
28
+
29
+ def method_missing(method, *args)
30
+ configuration[method.to_s]
31
+ end
32
+
33
+ def reload_configuration
34
+ @configuration = nil
35
+ return self
36
+ end
37
+
38
+ def configuration
39
+ @configuration ||= Configuration.new(configuration_file)
40
+ end
41
+ protected :configuration
42
+
43
+ def configuration_file
44
+ @configuration_file ||= underscore(self.to_s) + '.yml'
45
+ end
46
+ private :configuration_file
47
+
48
+ # Borrowed from activesupport/lib/active_support/inflector/methods.rb
49
+ # No need to bring in ActiveSupport for just this.
50
+ def underscore(camel_cased_word)
51
+ word = camel_cased_word.to_s.dup
52
+ word.gsub!('::', '/')
53
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
54
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
55
+ word.tr!("-", "_")
56
+ word.downcase!
57
+ word
58
+ end
59
+ private :underscore
60
+ end
@@ -0,0 +1,34 @@
1
+ require 'yaml'
2
+
3
+ module Configurethis
4
+ class Configuration
5
+ attr_reader :path
6
+
7
+ def initialize(configuration_file)
8
+ @path = File.join(ConfigurethisProperties.root_path, configuration_file)
9
+ end
10
+
11
+ def root=(key)
12
+ @values = load_configuration.fetch(key)
13
+ rescue ::KeyError
14
+ raise "'#{key}' is not configured in #{path}"
15
+ end
16
+
17
+ def [](key)
18
+ @values ||= load_configuration
19
+ val = @values.fetch(key)
20
+ return ValueContainer.new(val, path) if val.is_a?(Hash)
21
+ val
22
+ rescue ::KeyError
23
+ raise "'#{key}' is not configured in #{path}"
24
+ end
25
+
26
+ def load_configuration
27
+ File.open(path){ |f| ::YAML::load(f) }
28
+ rescue Exception => caught
29
+ raise "Could not locate configuration file: #{path}"
30
+ end
31
+
32
+
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ module Configurethis
2
+ class ConfigurethisProperties
3
+ class << self
4
+ def root_path=(path)
5
+ @@root_path = path
6
+ end
7
+
8
+ def root_path
9
+ @@root_path ||= ''
10
+ end
11
+
12
+ def use_defaults
13
+ @@root_path = nil
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Configurethis
2
+ class ValueContainer
3
+ def initialize(original, config_path)
4
+ @original_value = original
5
+ @config_path = config_path
6
+ end
7
+
8
+ def method_missing(method, *args)
9
+ val = @original_value.fetch(method.to_s)
10
+ return ValueContainer.new(val, @config_path) if val.is_a?(Hash)
11
+ val
12
+ rescue KeyError => caught
13
+ raise "Nested value '#{method.to_s}' is not configured in #{@config_path}"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Configurethis
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe Configurethis do
4
+ describe "making a class configurable from a yml file" do
5
+ context "when the base path is unmodified" do
6
+ Given { Configurethis.use_defaults }
7
+ Given (:klass) { ConventionalPath.reload_configuration }
8
+ Then { expect(klass.configuration_path).to eql('/conventional_path.yml') }
9
+ end
10
+
11
+ context "when the base configuration path has been modified" do
12
+ Given { Configurethis.root_path = '/my/custom/path' }
13
+
14
+ context "and the class specifies it's own configuration filename" do
15
+ Given (:klass) { OverridePath }
16
+ Then { expect(klass.configuration_path).to eql('/my/custom/path/configure/custom.yml') }
17
+ end
18
+
19
+ describe "and the class relies on convention over configuration for filename" do
20
+ context "and there is no namespace" do
21
+ Given (:klass) { ConventionalPath.reload_configuration }
22
+ Then { expect(klass.configuration_path).to eql('/my/custom/path/conventional_path.yml') }
23
+ end
24
+
25
+ context "and the class is namespaced" do
26
+ Given (:klass) { MyModule::ConventionalPath }
27
+ Then { expect(klass.configuration_path).to eql('/my/custom/path/my_module/conventional_path.yml') }
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "specifying a root config value for all access" do
34
+ Given { Configurethis.root_path = File.join(File.dirname(__FILE__), 'support/config') }
35
+
36
+ context "when the root value exists" do
37
+ Given (:rails_config) { RailsAppConfig }
38
+ Given { rails_config.set_root = :development }
39
+ Then { expect(rails_config.assets.compile).to be_false }
40
+
41
+ describe "and then we change the root value and access more values" do
42
+ Given { rails_config.set_root = :production }
43
+ Then { expect(rails_config.assets.compile).to be_true }
44
+ end
45
+ end
46
+
47
+ context "when the root value does not exist" do
48
+ Given (:rails_config) { RailsAppConfig }
49
+ Then { expect{ rails_config.set_root = :qa }.to raise_error(RuntimeError, "'qa' is not configured in #{rails_config.configuration_path}") }
50
+ end
51
+ end
52
+
53
+ describe "using configured values" do
54
+ Given { Configurethis.root_path = File.join(File.dirname(__FILE__), 'support/config') }
55
+
56
+ context "when the classes config file does not exist" do
57
+ Given (:config) { MissingConfiguration }
58
+ Then { expect{ config.some_value }.to raise_error(RuntimeError, "Could not locate configuration file: #{config.configuration_path}") }
59
+ end
60
+
61
+ context "when the value has not been set" do
62
+ Given (:config) { RiakConfig }
63
+ Then { expect{ config.storage_backend }.to raise_error(RuntimeError, "'storage_backend' is not configured in #{config.configuration_path}") }
64
+ end
65
+
66
+ context "when values are set" do
67
+ Given (:config) { RiakConfig }
68
+ Then { expect(config.pb_port).to eq(9002) }
69
+ Then { expect(config.http_port).to eq(9000) }
70
+ Then { expect(config.host).to eql('127.0.0.1') }
71
+
72
+ context "and nested" do
73
+ Then { expect(config.riak_control.cert).to eql('/opt/local/var/riak-1.2.0/riak.crt') }
74
+ Then { expect(config.production.riak_control.cert).to eql('/opt/local/var/riak-1.1.0/riak.crt') }
75
+ end
76
+
77
+ context "and deeply nested" do
78
+ Given (:nested_config) { NestedConfig }
79
+ Then { expect(nested_config.level1.level2.level3.level4).to eql("Hello World") }
80
+
81
+ context "and the value is not set" do
82
+ Then { expect{ nested_config.level1.level2.level7 }.to raise_error(RuntimeError, "Nested value 'level7' is not configured in #{nested_config.configuration_path}") }
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,11 @@
1
+ %w[
2
+ conventional_path
3
+ missing_configuration
4
+ namespaced_conventional_path
5
+ nested_config
6
+ override_path
7
+ rails_app_config
8
+ riak_config
9
+ ].each do |file|
10
+ require File.join("configurethis", "support", "classes", file)
11
+ end
@@ -0,0 +1,3 @@
1
+ class ConventionalPath
2
+ extend Configurethis
3
+ end
@@ -0,0 +1,3 @@
1
+ class MissingConfiguration
2
+ extend Configurethis
3
+ end
@@ -0,0 +1,4 @@
1
+ class MyCustomConf
2
+ include Configurethis
3
+ configure_this_with 'config/custom.yml'
4
+ end
@@ -0,0 +1,5 @@
1
+ module MyModule
2
+ class ConventionalPath
3
+ extend Configurethis
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ class NestedConfig
2
+ extend Configurethis
3
+ end
@@ -0,0 +1,4 @@
1
+ class OverridePath
2
+ extend Configurethis
3
+ configure_this_with 'configure/custom.yml'
4
+ end
@@ -0,0 +1,3 @@
1
+ class RailsAppConfig
2
+ extend Configurethis
3
+ end
@@ -0,0 +1,3 @@
1
+ class RiakConfig
2
+ extend Configurethis
3
+ end
@@ -0,0 +1,4 @@
1
+ level1:
2
+ level2:
3
+ level3:
4
+ level4: "Hello World"
@@ -0,0 +1,8 @@
1
+ ---
2
+ development:
3
+ assets:
4
+ compile: false
5
+
6
+ production:
7
+ assets:
8
+ compile: true
@@ -0,0 +1,10 @@
1
+ ---
2
+ pb_port: 9002
3
+ http_port: 9000
4
+ host: 127.0.0.1
5
+ riak_control:
6
+ cert: /opt/local/var/riak-1.2.0/riak.crt
7
+
8
+ production:
9
+ riak_control:
10
+ cert: /opt/local/var/riak-1.1.0/riak.crt
@@ -0,0 +1,24 @@
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
+
8
+ require 'configurethis'
9
+ require './spec/configurethis/support/classes'
10
+
11
+
12
+ require 'rspec/given'
13
+
14
+ RSpec.configure do |config|
15
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
+ config.run_all_when_everything_filtered = true
17
+ config.filter_run :focus
18
+
19
+ # Run specs in random order to surface order dependencies. If you find an
20
+ # order dependency and want to debug it, you can fix the order by providing
21
+ # the seed, which is printed after each run.
22
+ # --seed 1234
23
+ config.order = 'random'
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configurethis
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Snyder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-given
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: Clean up your configuration approach by using Configurethis. Configurethis
28
+ allows you to access your config values using method names instead of string literals
29
+ to identify which config value you want to retrieve.
30
+ email:
31
+ - snyder2112@me.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - .rspec
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - configurethis.gemspec
44
+ - lib/configurethis.rb
45
+ - lib/configurethis/configuration.rb
46
+ - lib/configurethis/configurethis_properties.rb
47
+ - lib/configurethis/value_container.rb
48
+ - lib/configurethis/version.rb
49
+ - spec/configurethis/configurethis_spec.rb
50
+ - spec/configurethis/support/classes.rb
51
+ - spec/configurethis/support/classes/conventional_path.rb
52
+ - spec/configurethis/support/classes/missing_configuration.rb
53
+ - spec/configurethis/support/classes/my_custom_conf.rb
54
+ - spec/configurethis/support/classes/namespaced_conventional_path.rb
55
+ - spec/configurethis/support/classes/nested_config.rb
56
+ - spec/configurethis/support/classes/override_path.rb
57
+ - spec/configurethis/support/classes/rails_app_config.rb
58
+ - spec/configurethis/support/classes/riak_config.rb
59
+ - spec/configurethis/support/config/nested_config.yml
60
+ - spec/configurethis/support/config/rails_app_config.yml
61
+ - spec/configurethis/support/config/riak_config.yml
62
+ - spec/spec_helper.rb
63
+ homepage: https://github.com/mattsnyder/configurethis
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Clean up your configuration approach by using Configurethis. Configurethis
86
+ allows you to access your config values using method names instead of string literals
87
+ to identify which config value you want to retrieve.
88
+ test_files:
89
+ - spec/configurethis/configurethis_spec.rb
90
+ - spec/configurethis/support/classes.rb
91
+ - spec/configurethis/support/classes/conventional_path.rb
92
+ - spec/configurethis/support/classes/missing_configuration.rb
93
+ - spec/configurethis/support/classes/my_custom_conf.rb
94
+ - spec/configurethis/support/classes/namespaced_conventional_path.rb
95
+ - spec/configurethis/support/classes/nested_config.rb
96
+ - spec/configurethis/support/classes/override_path.rb
97
+ - spec/configurethis/support/classes/rails_app_config.rb
98
+ - spec/configurethis/support/classes/riak_config.rb
99
+ - spec/configurethis/support/config/nested_config.yml
100
+ - spec/configurethis/support/config/rails_app_config.yml
101
+ - spec/configurethis/support/config/riak_config.yml
102
+ - spec/spec_helper.rb