prius 0.2.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b4efd54f9144bd36ec263a5cb4207ee0adc8b80c
4
+ data.tar.gz: 4a1cfedd0b63a10146b9164ca3cbc78f276afbfd
5
+ SHA512:
6
+ metadata.gz: f5f916b7b56ae454bed8bdcc82f37e7bad980a6003af01aa669776d725131e0fe74e479d8ad06be9be34e760d1cc4047debb538e94e4c8e471c64d37144687e5
7
+ data.tar.gz: 6d6788a1500a1efc21c7620fc09e902c3f4606feba3ce6211feb493eec44211248a5ba8da7f94dec700572e16a1e306d524b1370fde375f34114c5f43351bdff
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Prius
2
+
3
+ Environmentally-friendly application config for Ruby.
4
+
data/lib/prius.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "prius/registry"
2
+ require "prius/railtie" if defined?(Rails)
3
+
4
+ module Prius
5
+ def self.load(name, type: :string, allow_nil: false)
6
+ registry.load(name, type: type, allow_nil: allow_nil)
7
+ end
8
+
9
+ def self.get(name)
10
+ registry.get(name)
11
+ end
12
+
13
+ def self.registry
14
+ @registry ||= Registry.new(ENV)
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Prius
2
+ class MissingValueError < StandardError; end
3
+ class TypeMismatchError < StandardError; end
4
+ class UndeclaredNameError < StandardError; end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Prius
2
+ class Railtie < Rails::Railtie
3
+ config.before_configuration do
4
+ if ::Rails.root.join("config", "prius.rb").exist?
5
+ require ::Rails.root.join("config", "prius.rb")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ require "prius/errors"
2
+
3
+ module Prius
4
+ class Registry
5
+ TYPES = [:string, :int, :bool]
6
+
7
+ def initialize(env)
8
+ @env = env
9
+ @registry = {}
10
+ end
11
+
12
+ def load(name, env_var: nil, type: :string, allow_nil: false)
13
+ env_var ||= name.to_s.upcase
14
+ @registry[name] = case type
15
+ when :string then load_string(env_var, allow_nil)
16
+ when :int then load_int(env_var, allow_nil)
17
+ when :bool then load_bool(env_var, allow_nil)
18
+ else raise ArgumentError
19
+ end
20
+ end
21
+
22
+ def get(name)
23
+ @registry.fetch(name)
24
+ rescue KeyError
25
+ raise UndeclaredNameError, "config value '#{name}' never loaded"
26
+ end
27
+
28
+ private
29
+
30
+ def check_valid_type(type)
31
+ unless TYPES.include?(type)
32
+ raise ArgumentError, "invalid type '#{type}'"
33
+ end
34
+ end
35
+
36
+ def load_string(name, allow_nil)
37
+ @env.fetch(name)
38
+ rescue KeyError
39
+ return nil if allow_nil
40
+ raise MissingValueError, "config value '#{name}' not present"
41
+ end
42
+
43
+ def load_int(name, allow_nil)
44
+ value = load_string(name, allow_nil)
45
+ return value if value.nil?
46
+
47
+ unless /\A[0-9]+\z/.match(value)
48
+ raise TypeMismatchError, "'#{name}' value '#{value}' is not an integer"
49
+ end
50
+ value.to_i
51
+ end
52
+
53
+ def load_bool(name, allow_nil)
54
+ value = load_string(name, allow_nil)
55
+ return nil if value.nil?
56
+
57
+ if /\A(yes|y|true|t|1)\z/i.match(value)
58
+ return true
59
+ elsif /\A(no|n|false|f|0)\z/i.match(value)
60
+ return false
61
+ end
62
+ raise TypeMismatchError, "'#{name}' value '#{value}' is not an boolean"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Prius
2
+ VERSION = "0.2.0".freeze
3
+ end
data/prius.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 'prius/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "prius"
8
+ spec.version = Prius::VERSION
9
+ spec.authors = ["Harry Marr"]
10
+ spec.email = ["engineering@gocardless.com"]
11
+ spec.description = %q{Environmentally-friendly config}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/gocardless/prius"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_development_dependency "rspec", "~> 3.1"
22
+ end
@@ -0,0 +1,97 @@
1
+ require "prius/registry"
2
+
3
+ describe Prius::Registry do
4
+ let(:env) { { "NAME" => "Harry", "AGE" => "25", "ALIVE" => "yes" } }
5
+ let(:registry) { Prius::Registry.new(env) }
6
+
7
+ describe "#load" do
8
+ context "given a name that's present in the environment" do
9
+ it "doesn't blow up" do
10
+ expect { registry.load(:name) }.to_not raise_error
11
+ end
12
+ end
13
+
14
+ context "given a name that's not present in the environment" do
15
+ it "blows up" do
16
+ expect { registry.load(:slogan) }.
17
+ to raise_error(Prius::MissingValueError)
18
+ end
19
+
20
+ context "when allow_nil is true" do
21
+ it "doesn't blow up" do
22
+ expect { registry.load(:slogan, allow_nil: true) }.to_not raise_error
23
+ end
24
+ end
25
+ end
26
+
27
+ context "given an invalid type" do
28
+ it "blows up" do
29
+ expect { registry.load(:name, type: :lightsabre) }.
30
+ to raise_error(ArgumentError)
31
+ end
32
+ end
33
+
34
+ context "when specifying :int as the type" do
35
+ context "given a integer value" do
36
+ it "doesn't blow up" do
37
+ expect { registry.load(:age, type: :int) }.to_not raise_error
38
+ end
39
+
40
+ it "stores an int" do
41
+ registry.load(:age, type: :int)
42
+ expect(registry.get(:age)).to be_a(Fixnum)
43
+ end
44
+ end
45
+
46
+ context "given a non-integer value" do
47
+ it "blows up" do
48
+ expect { registry.load(:name, type: :int) }.
49
+ to raise_error(Prius::TypeMismatchError)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "when specifying :bool as the type" do
55
+ context "given a boolean value" do
56
+ it "doesn't blow up" do
57
+ expect { registry.load(:alive, type: :bool) }.to_not raise_error
58
+ end
59
+
60
+ it "stores an boolean" do
61
+ registry.load(:alive, type: :bool)
62
+ expect(registry.get(:alive)).to be_a(TrueClass)
63
+ end
64
+ end
65
+
66
+ context "given a non-boolean value" do
67
+ it "blows up" do
68
+ expect { registry.load(:name, type: :bool) }.
69
+ to raise_error(Prius::TypeMismatchError)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#get" do
76
+ context "given a name that has been loaded" do
77
+ before { registry.load(:name) }
78
+ it "returns the value" do
79
+ expect(registry.get(:name)).to eq("Harry")
80
+ end
81
+ end
82
+
83
+ context "given a name that hasn't been loaded" do
84
+ it "blows up" do
85
+ expect { registry.get(:name) }.
86
+ to raise_error(Prius::UndeclaredNameError)
87
+ end
88
+ end
89
+
90
+ context "given a nillable name that has been loaded" do
91
+ before { registry.load(:lightsabre, allow_nil: true) }
92
+ it "returns nil" do
93
+ expect(registry.get(:lightsabre)).to be_nil
94
+ end
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prius
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Harry Marr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ description: Environmentally-friendly config
28
+ email:
29
+ - engineering@gocardless.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - README.md
37
+ - lib/prius.rb
38
+ - lib/prius/errors.rb
39
+ - lib/prius/railtie.rb
40
+ - lib/prius/registry.rb
41
+ - lib/prius/version.rb
42
+ - prius.gemspec
43
+ - spec/prius/registry_spec.rb
44
+ homepage: https://github.com/gocardless/prius
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Environmentally-friendly config
68
+ test_files:
69
+ - spec/prius/registry_spec.rb