envied 0.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: 44b4eb082007cd5398a1071cdc35c4120355a4fc
4
+ data.tar.gz: 023f24e04c08eac42a0d276456be612b4eea865c
5
+ SHA512:
6
+ metadata.gz: e9aa2e24f9dbdec5eda80fe0785c33fdb2e2a737feebf44cdd5f9f9b770664e56931964d68444caa633d9f9e554ba01071e6588929ad812fd83424646e494943
7
+ data.tar.gz: afbac06eaf2229c84befdb43abd1c6e097b363b291ad2bb72e81088edff38451b9a76a43cd9fdfee51599d9925b81bdfe8d057e21172b513d9e061d192214692
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/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in envied.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Gert Goet
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,67 @@
1
+ # ENVied or ENV on EPO
2
+
3
+ TL;DR `ENVied` will improve your life drama-ti-cally.
4
+
5
+ Say you're nicely configuring your app via ENV-variables, 'ey?
6
+ Then maybe, just like me, you had the itch to check whether all variables your app needs, are present.
7
+ Or you sometimes wish that ENV-variables should not only contain strings, but integers and booleans.
8
+
9
+ Wooha! You really should try `ENVied`.
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'envied'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ # in config/application.rb
26
+ # somewhere after 'Bundler.require(*Rails.groups)':
27
+ ENVied.configure do |env|
28
+ env.variable :rails_env
29
+ env.variable :force_ssl, :Boolean
30
+ env.variable :port, :Integer
31
+ end
32
+
33
+ ENVied.require! # raises when configured variables are not present in ENV
34
+
35
+ # existing app config starts here
36
+ module Blog
37
+ class Application < Rails::Application
38
+ config.force_ssl = ENVied.force_ssl
39
+ ...
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Testing
45
+
46
+ ```bash
47
+ bundle install --binstubs
48
+
49
+ bin/rspec
50
+ # or
51
+ bin/rake
52
+ ```
53
+
54
+ ## Developing
55
+
56
+ ```bash
57
+ bin/pry --gem
58
+ ```
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( http://github.com/<my-github-username>/envied/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ desc "Run the specs"
7
+ task default: :spec
data/envied.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'envied'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "envied"
8
+ spec.version = ENVied::VERSION
9
+ spec.authors = ["Gert Goet"]
10
+ spec.email = ["gert@thinkcreate.nl"]
11
+ spec.summary = %q{ENV on epo}
12
+ spec.description = %q{no, really}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "virtus", '~> 1.0.1'
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", '3.0.0.beta2'
25
+ end
data/lib/envied.rb ADDED
@@ -0,0 +1,84 @@
1
+ class ENVied
2
+ VERSION = "0.0.1"
3
+ module Configurable
4
+ require 'virtus'
5
+
6
+ class EnvMissing < StandardError;end
7
+ class EnvWrongType < StandardError;end
8
+
9
+ def self.included(base)
10
+ base.class_eval do
11
+ include Virtus.model
12
+ end
13
+ base.extend ClassMethods
14
+ end
15
+
16
+ module ClassMethods
17
+ # Creates an instance of Configuration.
18
+ #
19
+ # Will raise EnvMissing for variables Configuration defined
20
+ # but are missing from env.
21
+ #
22
+ # Will raise EnvWrongType for variables that can't be coerced to the
23
+ # type defined in Configuration.
24
+ #
25
+ # @param env [Hash] the env
26
+ def parse_env(env)
27
+ atts = attribute_set.map(&:name).each_with_object({}) do |name, result|
28
+ @current_att = name
29
+ unless result[name] = env[name.to_s] || env[name.to_s.upcase]
30
+ raise EnvMissing, "Please provide ENV['#{name.to_s.upcase}']"
31
+ end
32
+ end
33
+ new(atts)
34
+ rescue Virtus::CoercionError => e
35
+ configured_type = attribute_set[@current_att].options[:primitive]
36
+ raise EnvWrongType,
37
+ "ENV['#{@current_att.to_s.upcase}'] should be of type %s" % configured_type
38
+ end
39
+
40
+ def variable(name, type = String, options = {})
41
+ attribute name, type, { strict: true }.merge(options)
42
+ end
43
+ end
44
+ end
45
+
46
+ class << self
47
+ attr_accessor :configuration
48
+ end
49
+
50
+ def self.configure(&block)
51
+ @configuration = Class.new { include Configurable }.tap(&block)
52
+ # or define this thing as ENVied::Configuration? prolly not threadsafe
53
+ ensure
54
+ @instance = nil
55
+ end
56
+
57
+ def self.instance
58
+ @instance ||= @configuration.parse_env(ENV.to_hash)
59
+ end
60
+ class << self
61
+ alias_method :require!, :instance
62
+ end
63
+
64
+ def self.[](key)
65
+ instance.attributes[key]
66
+ #instance[key] # will raise and complain that <class <>> doesn't response; better?
67
+ end
68
+
69
+ def self.method_missing(method, *args, &block)
70
+ respond_to_missing?(method) ? instance.public_send(method, *args, &block) : super
71
+ end
72
+
73
+ def self.respond_to_missing?(method, include_private = false)
74
+ instance.attributes.key?(method) || super
75
+ end
76
+ end
77
+
78
+ #ENVied.configure do |env|
79
+ # env.variable :port, :Integer
80
+ # env.variable :force_ssl, :Boolean
81
+ # env.variable :require, :Boolean
82
+ #end
83
+ #
84
+ #ENVied.require
@@ -0,0 +1,3 @@
1
+ #class ENVied
2
+ # VERSION = "0.0.1"
3
+ #end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe ENVied do
4
+ subject { described_class }
5
+
6
+ it { should respond_to :require! }
7
+ it { should respond_to :configure }
8
+
9
+ context 'configured' do
10
+
11
+ def unconfigured
12
+ configured_with
13
+ self
14
+ end
15
+
16
+ def configured_with(hash = {})
17
+ described_class.configure do |env|
18
+ hash.each do |name, type|
19
+ env.variable(name, type)
20
+ end
21
+ end
22
+ self
23
+ end
24
+
25
+ def and_ENV(env = {})
26
+ stub_const("ENV", env)
27
+ described_class
28
+ end
29
+
30
+ def and_no_ENV
31
+ and_ENV
32
+ end
33
+
34
+ it 'responds to configured variables' do
35
+ configured_with(a: :Integer).and_ENV({'A' => '1'})
36
+ is_expected.to respond_to :a
37
+ end
38
+
39
+ it 'responds not to unconfigured variables' do
40
+ unconfigured.and_ENV({'A' => '1'})
41
+ is_expected.to_not respond_to :a
42
+ end
43
+
44
+ context 'ENV contains not all configured variables' do
45
+ before { configured_with(a: :Integer).and_no_ENV }
46
+
47
+ it 'raises EnvMissing on calling required!' do
48
+ expect {
49
+ ENVied.require!
50
+ }.to raise_error(ENVied::Configurable::EnvMissing)
51
+ end
52
+
53
+ it 'raises EnvMissing when interacted with and configured variable not present in ENV' do
54
+ expect {
55
+ ENVied.any_missing_method
56
+ }.to raise_error(ENVied::Configurable::EnvMissing)
57
+ end
58
+ end
59
+
60
+ context 'ENV containing variable of different type' do
61
+ before { configured_with(a: :Integer).and_ENV('A' => 'NaN') }
62
+
63
+ specify do
64
+ expect {
65
+ ENVied.a
66
+ }.to raise_error(ENVied::Configurable::EnvWrongType)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ $:.unshift File.expand_path("../../lib", __FILE__)
6
+ require 'envied'
Binary file
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: envied
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gert Goet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0.beta2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0.beta2
69
+ description: no, really
70
+ email:
71
+ - gert@thinkcreate.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - envied.gemspec
82
+ - lib/envied.rb
83
+ - lib/envied/version.rb
84
+ - spec/envied_spec.rb
85
+ - spec/spec_helper.rb
86
+ - underconstruction.gif
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: ENV on epo
111
+ test_files:
112
+ - spec/envied_spec.rb
113
+ - spec/spec_helper.rb