From 76e8d30cb6066c9e9099e80b6265a569da0b183c Mon Sep 17 00:00:00 2001 From: Sanni Abdulmusawwir Date: Mon, 24 Oct 2022 10:16:20 +0100 Subject: [PATCH 1/2] Ensure `Prius#load` raises an error when unexpected options are passed Use keyword arguments in Prius::Registry#load so that extraneous options passed in to the #load method raise an ArgumentError rather than getting silently ignored. --- README.md | 2 +- lib/prius.rb | 4 ++-- lib/prius/registry.rb | 6 ++---- spec/prius/registry_spec.rb | 7 +++++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5680963..e3f8aa6 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/prius.rb b/lib/prius.rb index 4e4f230..8cd9c97 100644 --- a/lib/prius.rb +++ b/lib/prius.rb @@ -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. diff --git a/lib/prius/registry.rb b/lib/prius/registry.rb index 6824e3c..6b29ccd 100644 --- a/lib/prius/registry.rb +++ b/lib/prius/registry.rb @@ -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) diff --git a/spec/prius/registry_spec.rb b/spec/prius/registry_spec.rb index fc38b04..95adcfc 100644 --- a/spec/prius/registry_spec.rb +++ b/spec/prius/registry_spec.rb @@ -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 From f38bdab0689564ec03d552fe801be0d31c50e7e9 Mon Sep 17 00:00:00 2001 From: Sanni Abdulmusawwir Date: Mon, 24 Oct 2022 10:22:51 +0100 Subject: [PATCH 2/2] Bump to v5.0.0 --- CHANGELOG.md | 4 ++++ lib/prius/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f22f3b7..c85d52f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/prius/version.rb b/lib/prius/version.rb index d5319df..2155d22 100644 --- a/lib/prius/version.rb +++ b/lib/prius/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Prius - VERSION = "4.1.0" + VERSION = "5.0.0" end