hconfig 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hconfig.gemspec +22 -0
- data/lib/hconfig.rb +67 -0
- data/lib/hconfig/version.rb +3 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 722e17ec8f6840da97b31e85de9565180b0e607e
|
4
|
+
data.tar.gz: 721905876fe73c4047f504270306619d858ccff1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef08569cbc388189a44ccdecfb6d6af11fd8dfa62d8999b44a1e7c80cbd95f177dce19d9f1f237f552cbb6875b2b42923695f8dd62ae881a2c72e803d3031077
|
7
|
+
data.tar.gz: 7fcdac73a79fb9549f2ed853eafd4277789d440c2a7403924b909dc7b428be74eb8eca94bba629e043acfe17aa3fdfb58d5ad7d2eed2d5ed98b2d1c43b56ec90
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Matte Noble
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# hconfig
|
2
|
+
|
3
|
+
hconfig translates ENV vars into `Config.*` attributes, with presence
|
4
|
+
validation and type casting. hconfig is intended to be used in
|
5
|
+
environments (Heroku, in my case) where much of the config is stored in ENV.
|
6
|
+
|
7
|
+
hconfig is a standalone gem version of the `CastingConfigHelpers` module from
|
8
|
+
[Pliny](https://github.com/interagent/pliny).
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'hconfig'
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
module Config
|
20
|
+
extend HConfig
|
21
|
+
|
22
|
+
mandatory :database_url, string
|
23
|
+
optional :versioning_default, string
|
24
|
+
override :port, 5000, int
|
25
|
+
end
|
26
|
+
|
27
|
+
ENV["DATABASE_URL"] = "postgres:///thing-db"
|
28
|
+
|
29
|
+
Config.database_url
|
30
|
+
# => "postgres:///thing-db"
|
31
|
+
```
|
32
|
+
|
33
|
+
### RbConfig Note
|
34
|
+
|
35
|
+
If you're mixing this into a module named `Config`, RbConfig will warn you
|
36
|
+
about doing so. You can supress the warning via:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Object.send(:remove_const, :Config) if Object.const_defined?(:Config)
|
40
|
+
```
|
41
|
+
|
42
|
+
Alternatively, you can just namespace `Config` under your app/lib/etc.
|
43
|
+
|
44
|
+
### Mandatory Variables
|
45
|
+
|
46
|
+
An exception is raised when the ENV var is missing.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
mandatory :database_url, string
|
50
|
+
```
|
51
|
+
|
52
|
+
### Optional Variables
|
53
|
+
|
54
|
+
The value is returned if set, otherwise `nil`.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
optional :versioning_default, string
|
58
|
+
```
|
59
|
+
|
60
|
+
### Override
|
61
|
+
|
62
|
+
The value is returned if set, otherwise the default value will be
|
63
|
+
returned.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
override :port, 5000, int
|
67
|
+
```
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
```
|
72
|
+
$ bundle install
|
73
|
+
$ rake
|
74
|
+
```
|
75
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hconfig"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/hconfig.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hconfig/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hconfig"
|
8
|
+
spec.version = Hconfig::VERSION
|
9
|
+
spec.authors = ["Matte Noble"]
|
10
|
+
spec.email = ["me@mattenoble.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ENV var to Config object translation with validations.}
|
13
|
+
spec.description = %q{ENV var to Config object translation with validations.}
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
data/lib/hconfig.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "hconfig/version"
|
2
|
+
|
3
|
+
module HConfig
|
4
|
+
def mandatory(name, method=nil)
|
5
|
+
value = cast(ENV.fetch(name.to_s.upcase), method)
|
6
|
+
create(name, value)
|
7
|
+
end
|
8
|
+
|
9
|
+
def optional(name, method=nil)
|
10
|
+
value = cast(ENV[name.to_s.upcase], method)
|
11
|
+
create(name, value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def override(name, default, method=nil)
|
15
|
+
value = cast(ENV.fetch(name.to_s.upcase, default), method)
|
16
|
+
create(name, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def int
|
20
|
+
->(v) { v.to_i }
|
21
|
+
end
|
22
|
+
|
23
|
+
def float
|
24
|
+
->(v) { v.to_f }
|
25
|
+
end
|
26
|
+
|
27
|
+
def bool
|
28
|
+
->(v) { v.to_s=='true'}
|
29
|
+
end
|
30
|
+
|
31
|
+
def string
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def symbol
|
36
|
+
->(v) { v.to_sym }
|
37
|
+
end
|
38
|
+
|
39
|
+
# optional :accronyms, array(string)
|
40
|
+
# => ['a', 'b']
|
41
|
+
# optional :numbers, array(int)
|
42
|
+
# => [1, 2]
|
43
|
+
# optional :notype, array
|
44
|
+
# => ['a', 'b']
|
45
|
+
def array(method = nil)
|
46
|
+
-> (v) do
|
47
|
+
if v
|
48
|
+
v.split(",").map{|a| cast(a, method) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def cast(value, method)
|
56
|
+
method ? method.call(value) : value
|
57
|
+
end
|
58
|
+
|
59
|
+
def create(name, value)
|
60
|
+
instance_variable_set(:"@#{name}", value)
|
61
|
+
instance_eval "def #{name}; @#{name} end", __FILE__, __LINE__
|
62
|
+
if value.kind_of?(TrueClass) || value.kind_of?(FalseClass) || value.kind_of?(NilClass)
|
63
|
+
instance_eval "def #{name}?; !!@#{name} end", __FILE__, __LINE__
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hconfig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matte Noble
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ENV var to Config object translation with validations.
|
42
|
+
email:
|
43
|
+
- me@mattenoble.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- hconfig-0.1.0.gem
|
58
|
+
- hconfig.gemspec
|
59
|
+
- lib/hconfig.rb
|
60
|
+
- lib/hconfig/version.rb
|
61
|
+
homepage:
|
62
|
+
licenses: []
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: ENV var to Config object translation with validations.
|
84
|
+
test_files: []
|