Skip to content

Commit

Permalink
Merge pull request #48 from gocardless/require-load-args
Browse files Browse the repository at this point in the history
Ensure `Prius#load` raises an error when unexpected options are passed
  • Loading branch information
Sannim1 authored Oct 24, 2022
2 parents 02d749e + f38bdab commit 71f1a2b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.0.0

* Ensure Prius#load raises an error when unexpected options are passed

# 4.1.0

* Add support for coercing to a date.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Environment variables need to be loaded into the Prius registry before being
used. Typically this is done in an initialiser.

```ruby
Prius.load(name, options = {})
Prius.load(name, **options)
```

If an environment variable can't be loaded, Prius will raise one of:
Expand Down
4 changes: 2 additions & 2 deletions lib/prius.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module Prius
# Raises a MissingValueError for required values that are missing.
# Raises a TypeMismatchError if a value can't be coerced to the given `type`.
# Raises an ArgumentError if an invalid `type` is provided.
def self.load(name, options = {})
registry.load(name, options)
def self.load(name, **options)
registry.load(name, **options)
end

# Fetch a value from the registry.
Expand Down
6 changes: 2 additions & 4 deletions lib/prius/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ def initialize(env)
end

# See Prius.load for documentation.
def load(name, options = {})
env_var = options.fetch(:env_var, name.to_s.upcase)
type = options.fetch(:type, :string)
required = options.fetch(:required, true)
def load(name, env_var: nil, type: :string, required: true)
env_var = env_var.nil? ? name.to_s.upcase : env_var
@registry[name] = case type
when :string then load_string(env_var, required)
when :int then load_int(env_var, required)
Expand Down
2 changes: 1 addition & 1 deletion lib/prius/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Prius
VERSION = "4.1.0"
VERSION = "5.0.0"
end
7 changes: 7 additions & 0 deletions spec/prius/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
let(:registry) { Prius::Registry.new(env) }

describe "#load" do
context "given an invalid option" do
it "raises an error" do
expect { registry.load(:age, unsupported_option: :foo) }.
to raise_error(ArgumentError)
end
end

context "given a name that's present in the environment" do
it "doesn't blow up" do
expect { registry.load(:name) }.to_not raise_error
Expand Down

0 comments on commit 71f1a2b

Please sign in to comment.