constfig 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ bin/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format nested
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in constfig.gemspec
4
+ gemspec
5
+ gem 'rspec'
6
+ gem 'guard'
7
+ gem 'guard-rspec'
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec, cli: '--color --format nested' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vitaly Kushner
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,73 @@
1
+ # Constfig
2
+
3
+ Simple configuration for Ruby.
4
+
5
+ Allows you to define configuration CONSTANTS that take values from ENV.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'constfig'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install constfig
20
+
21
+ ## Usage
22
+
23
+ There is only one function provided by the gem: `define_config`.
24
+
25
+ ### With a default (optional variable)
26
+
27
+ You can call it with a default, like this:
28
+
29
+ define_config :DEFAULT_DOMAIN, "astrails.com"
30
+
31
+ In which case it will first look if `ENV['DEFAULT_DOMAIN']` is available, and
32
+ if not will use the 'astrails.com'. A constant `DEFAULT_DOMAIN` will be
33
+ defined.
34
+
35
+ ### Without a default (required variable)
36
+
37
+ Or you can call it without the default:
38
+
39
+ define_config :DEFAULT_DOMAIN
40
+
41
+ In which case it will raise exception `Constfig::Undefined` if
42
+ `ENV['DEFAULT_DOMAIN']` is not available.
43
+
44
+ ### Variable type
45
+
46
+ One last thing. Non-string variables are supported. If you provide a non-string
47
+ default (boolean, integer, float or symbol), the value that is coming from
48
+ `ENV` will be converted to the same type (using `to_i`, `to_f`, and
49
+ `to_symbol`). For the true/false types `"true"`, `"TRUE"`, and `"1"` will be
50
+ treated as `true`, anything else will be treated as `false`.
51
+
52
+ In the case of required variables, you can supply a `Class` in place of the
53
+ default, and it will be used for the type conversion. Like this:
54
+
55
+ define_config :EXPIRATION_DAYS, Fixnum
56
+
57
+ For boolean variables you can supply either `TrueClass`, or `FalseClass`.
58
+
59
+ ### Existing constants
60
+
61
+ This gem will not re-define existing constants.
62
+
63
+ ### Managing environment
64
+
65
+ You can use the [dotenv gem](https://github.com/bkeepers/dotenv) to manage your `ENV`.
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task :default => :spec
data/constfig.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'constfig/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "constfig"
8
+ gem.version = Constfig::VERSION
9
+ gem.authors = ["Vitaly Kushner"]
10
+ gem.email = ["vitaly@astrails.com"]
11
+ gem.description = %q{Simple Constnt Configuration for Ruby. Creates CONSTANTS with values from ENV}
12
+ gem.summary = %q{Simple Constnt configuration for Ruby.}
13
+ gem.homepage = "http://astrails.com/blog/constfig"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
data/lib/constfig.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'constfig/config'
2
+
3
+ Object.send :include, Constfig::Config
@@ -0,0 +1,40 @@
1
+ require "constfig/version"
2
+ module Constfig
3
+ class Undefined < RuntimeError; end
4
+ module Config
5
+ def define_config name, default = (no_default = true;nil)
6
+ return if Object.const_defined?(name)
7
+
8
+ has_default_value = !no_default # && default.class != Class
9
+
10
+ if value = ENV[name.to_s]
11
+ if has_default_value
12
+ type = default.is_a?(Class) ? default.name : default.class.name
13
+ value = convert_to_type value, type
14
+ end
15
+ else
16
+ raise RuntimeError, "missing config #{name}, and no default was provided" unless has_default_value
17
+ value = default
18
+ end
19
+
20
+ Object.const_set name, value
21
+ end
22
+
23
+ private
24
+
25
+ def convert_to_type(value, type)
26
+ case type
27
+ when 'Fixnum'
28
+ value.to_i
29
+ when 'Float'
30
+ value.to_f
31
+ when 'Symbol'
32
+ value.to_sym
33
+ when 'TrueClass', 'FalseClass'
34
+ '1' == value || 'true' == value || 'TRUE' == value
35
+ else
36
+ value
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Constfig
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Constfig do
4
+ before(:each) do
5
+ Object.send :remove_const, :FOO if Object.const_defined?(:FOO)
6
+ ENV.delete 'FOO'
7
+ end
8
+
9
+ it "should define config var from a default" do
10
+ define_config :FOO, 123
11
+ FOO.should == 123
12
+ end
13
+
14
+ it "should define config var from ENV" do
15
+ ENV['FOO'] = '456'
16
+ define_config :FOO
17
+ FOO.should == '456'
18
+ end
19
+
20
+ it "should prefer ENV over default" do
21
+ ENV['FOO'] = 'bar'
22
+ define_config :FOO, 'baz'
23
+ FOO.should == 'bar'
24
+ end
25
+
26
+ it "should raise exception when no default and no ENV" do
27
+ ->{
28
+ define_config :FOO
29
+ }.should raise_error(/missing/)
30
+ end
31
+
32
+ it "should not change an already defined constant" do
33
+ Object.const_set :FOO, 'foo'
34
+ define_config :FOO, 'bar'
35
+ FOO.should == 'foo'
36
+ end
37
+
38
+ describe :conversions do
39
+ def self.verify_conversion(env, default, expected)
40
+ it "should convert #{env.inspect}\tto\t#{expected.inspect}\twhen default is\t#{default.inspect}" do
41
+ ENV['FOO'] = env
42
+ define_config :FOO, default
43
+ FOO.should == expected
44
+ end
45
+ end
46
+
47
+ verify_conversion '12.3', '456', '12.3'
48
+ verify_conversion '12.3', 456, 12
49
+ verify_conversion '12.3', 456.0, 12.3
50
+ verify_conversion 'foo', :bar, :foo
51
+ verify_conversion '0', true, false
52
+ verify_conversion 'false', true, false
53
+ verify_conversion 'FALSE', true, false
54
+ verify_conversion 'foo', true, false
55
+ verify_conversion '1', true, true
56
+ verify_conversion 'true', true, true
57
+ verify_conversion 'TRUE', true, true
58
+
59
+ verify_conversion '12.3', Fixnum, 12
60
+ verify_conversion '12.3', Float, 12.3
61
+ verify_conversion '12.3', String, '12.3'
62
+ verify_conversion 'foo', Symbol, :foo
63
+ verify_conversion 'foo', TrueClass, false
64
+ verify_conversion 'foo', FalseClass, false
65
+ verify_conversion '0', TrueClass, false
66
+ verify_conversion '0', FalseClass, false
67
+ verify_conversion '1', TrueClass, true
68
+ verify_conversion '1', FalseClass, true
69
+ verify_conversion 'true', TrueClass, true
70
+ verify_conversion 'true', FalseClass, true
71
+ verify_conversion 'TRUE', TrueClass, true
72
+ verify_conversion 'TRUE', FalseClass, true
73
+ end
74
+
75
+ end
@@ -0,0 +1,6 @@
1
+ require 'constfig'
2
+
3
+ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
4
+
5
+ RSpec.configure do |config|
6
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vitaly Kushner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple Constnt Configuration for Ruby. Creates CONSTANTS with values
15
+ from ENV
16
+ email:
17
+ - vitaly@astrails.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - .rspec
24
+ - Gemfile
25
+ - Guardfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - constfig.gemspec
30
+ - lib/constfig.rb
31
+ - lib/constfig/config.rb
32
+ - lib/constfig/version.rb
33
+ - spec/constfig/constfig_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: http://astrails.com/blog/constfig
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Simple Constnt configuration for Ruby.
59
+ test_files:
60
+ - spec/constfig/constfig_spec.rb
61
+ - spec/spec_helper.rb