prius 4.0.0 → 5.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2e2b95c7456b01442cf90ba9ad16d4a4ca872bbcf765f15aefeb0b5ff132003
4
- data.tar.gz: 1f3cf76e9746fc2433d297e0090a1f4ab166ceda25de176ccee0826e02b99c50
3
+ metadata.gz: f3a8c8693c57b1528dc91a93f3cc4343315db4beb5e5f4eeea6d11be4bbb6fa8
4
+ data.tar.gz: 3784c94e35340ef5dcab6d6ca380204f7d5beac63937b4df3baa4e9352da995b
5
5
  SHA512:
6
- metadata.gz: cb70cd3bc19f29b24ed2a980d713a45476a8ff1006f853b6dbc8ab2091f743208d819eaffdf098234114c847420729bf7c7d119c8726f8ca8a02fabc0c95c68f
7
- data.tar.gz: 53df6a3b348e74b69c193a36ad56b3d5e4c5dc0360347eadd7f570a06b820ebdef8344d921467d7cd6e5221f61499d8890fa7bea5264e1b563a6d760397210f4
6
+ metadata.gz: 1f06a5b14da5781a04009b414943f0485b5f7729c87ca2585756d65d94fbea599c56c07aef4278fc28873752652591cf346fa2ffd678aed155d6570ce3ed5e68
7
+ data.tar.gz: 48211d8f7ae7c20e59d411f08ba8b82cb366242510c4dabe2dbea94d8b3eb010a88d5f885dc0fd2f5e8b21c5199932eee43a9ebc59dc05fb8c0a5d1c7191f10c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 5.0.0
2
+
3
+ * Ensure Prius#load raises an error when unexpected options are passed
4
+
5
+ # 4.1.0
6
+
7
+ * Add support for coercing to a date.
8
+
1
9
  # 4.0.0
2
10
 
3
11
  * Add support for Ruby 3.0
data/README.md CHANGED
@@ -9,7 +9,7 @@ Prius helps you guarantee that your environment variables are:
9
9
  - **Present** - an exception is raised if an environment variable is missing,
10
10
  so you can hear about it as soon as your app boots.
11
11
  - **Valid** - an environment variable can be coerced to a desired type
12
- (integer, boolean or string), and an exception will be raised if the value
12
+ (integer, boolean, string, or date), and an exception will be raised if the value
13
13
  doesn't match the type.
14
14
 
15
15
  ## Usage
@@ -48,7 +48,7 @@ Environment variables need to be loaded into the Prius registry before being
48
48
  used. Typically this is done in an initialiser.
49
49
 
50
50
  ```ruby
51
- Prius.load(name, options = {})
51
+ Prius.load(name, **options)
52
52
  ```
53
53
 
54
54
  If an environment variable can't be loaded, Prius will raise one of:
@@ -60,7 +60,7 @@ If an environment variable can't be loaded, Prius will raise one of:
60
60
  | Param | Default | Description |
61
61
  |-------------------|---------------|-------------------------------------------------------------------------------------------|
62
62
  | `required` | `true` | Flag to require the environment variable to have been set. |
63
- | `type` | `:string` | Type to coerce the environment variable to. Allowed values are `:string`, `:int` and `:bool`. |
63
+ | `type` | `:string` | Type to coerce the environment variable to. Allowed values are `:string`, `:int`, `:bool`, and `:date`. |
64
64
  | `env_var` | `name.upcase` | Name of the environment variable name (if different from the upcased `name`). |
65
65
 
66
66
  #### Reading Environment Variables
@@ -14,14 +14,13 @@ module Prius
14
14
  end
15
15
 
16
16
  # See Prius.load for documentation.
17
- def load(name, options = {})
18
- env_var = options.fetch(:env_var, name.to_s.upcase)
19
- type = options.fetch(:type, :string)
20
- required = options.fetch(:required, true)
17
+ def load(name, env_var: nil, type: :string, required: true)
18
+ env_var = env_var.nil? ? name.to_s.upcase : env_var
21
19
  @registry[name] = case type
22
20
  when :string then load_string(env_var, required)
23
21
  when :int then load_int(env_var, required)
24
22
  when :bool then load_bool(env_var, required)
23
+ when :date then load_date(env_var, required)
25
24
  else raise ArgumentError, "Invalid type #{type}"
26
25
  end
27
26
  end
@@ -62,5 +61,14 @@ module Prius
62
61
 
63
62
  raise TypeMismatchError, "'#{name}' value '#{value}' is not a boolean"
64
63
  end
64
+
65
+ def load_date(name, required)
66
+ value = load_string(name, required)
67
+ return nil if value.nil?
68
+
69
+ Date.parse(value)
70
+ rescue ArgumentError
71
+ raise TypeMismatchError, "'#{name}' value '#{value}' is not a date"
72
+ end
65
73
  end
66
74
  end
data/lib/prius/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prius
4
- VERSION = "4.0.0"
4
+ VERSION = "5.0.0"
5
5
  end
data/lib/prius.rb CHANGED
@@ -14,7 +14,7 @@ module Prius
14
14
  # omitted the uppercased form of `name` will be used.
15
15
  # :type - The Symbol type of the environment variable's value.
16
16
  # The value will be coerced to this type. Must be one
17
- # of :string, :int, or :bool (default :string).
17
+ # of :string, :int, :bool, or :date (default :string).
18
18
  # :required - A Boolean indicating whether the value must be
19
19
  # present in the environment. If true, a
20
20
  # MissingValueError exception will be raised if the
@@ -25,8 +25,8 @@ module Prius
25
25
  # Raises a MissingValueError for required values that are missing.
26
26
  # Raises a TypeMismatchError if a value can't be coerced to the given `type`.
27
27
  # Raises an ArgumentError if an invalid `type` is provided.
28
- def self.load(name, options = {})
29
- registry.load(name, options)
28
+ def self.load(name, **options)
29
+ registry.load(name, **options)
30
30
  end
31
31
 
32
32
  # Fetch a value from the registry.
@@ -3,10 +3,25 @@
3
3
  require "prius/registry"
4
4
 
5
5
  describe Prius::Registry do
6
- let(:env) { { "NAME" => "Harry", "AGE" => "25", "ALIVE" => "yes" } }
6
+ let(:env) do
7
+ {
8
+ "NAME" => "Harry",
9
+ "AGE" => "25",
10
+ "ALIVE" => "yes",
11
+ "BORN" => "2022-09-02",
12
+ "INVALID_DATE" => "2022-02-99"
13
+ }
14
+ end
7
15
  let(:registry) { Prius::Registry.new(env) }
8
16
 
9
17
  describe "#load" do
18
+ context "given an invalid option" do
19
+ it "raises an error" do
20
+ expect { registry.load(:age, unsupported_option: :foo) }.
21
+ to raise_error(ArgumentError)
22
+ end
23
+ end
24
+
10
25
  context "given a name that's present in the environment" do
11
26
  it "doesn't blow up" do
12
27
  expect { registry.load(:name) }.to_not raise_error
@@ -72,6 +87,34 @@ describe Prius::Registry do
72
87
  end
73
88
  end
74
89
  end
90
+
91
+ context "when specifying :date as the type" do
92
+ context "given a date value" do
93
+ it "doesn't blow up" do
94
+ expect { registry.load(:born, type: :date) }.to_not raise_error
95
+ end
96
+
97
+ it "stores a date" do
98
+ registry.load(:born, type: :date)
99
+ expect(registry.get(:born)).to be_a(Date)
100
+ expect(registry.get(:born)).to eq(Date.parse(env["BORN"]))
101
+ end
102
+ end
103
+
104
+ context "given an invalid date value" do
105
+ it "blows up" do
106
+ expect { registry.load(:invalid_date, type: :date) }.
107
+ to raise_error(Prius::TypeMismatchError)
108
+ end
109
+ end
110
+
111
+ context "given a non-date value" do
112
+ it "blows up" do
113
+ expect { registry.load(:name, type: :date) }.
114
+ to raise_error(Prius::TypeMismatchError)
115
+ end
116
+ end
117
+ end
75
118
  end
76
119
 
77
120
  describe "#get" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prius
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GoCardless Engineering
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-23 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -92,7 +92,7 @@ homepage: https://github.com/gocardless/prius
92
92
  licenses:
93
93
  - MIT
94
94
  metadata: {}
95
- post_install_message:
95
+ post_install_message:
96
96
  rdoc_options: []
97
97
  require_paths:
98
98
  - lib
@@ -107,8 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubygems_version: 3.2.15
111
- signing_key:
110
+ rubygems_version: 3.0.3.1
111
+ signing_key:
112
112
  specification_version: 4
113
113
  summary: Environmentally-friendly config
114
114
  test_files: