diff --git a/CHANGELOG.md b/CHANGELOG.md index ce666e5..e8d1f17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.1.0 - 2024-06-27 + +- Add support for loading a datetime value. + # 6.0.0 - 2024-01-09 - Drop support for Ruby 2.6 and 2.7. diff --git a/Gemfile b/Gemfile index e49facd..865a39a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source "https://rubygems.org" gemspec group :development do - gem "gc_ruboconfig", "~> 4.4" + gem "gc_ruboconfig", "~> 5.0" gem 'pry' gem "rspec", "~> 3.12" gem "rspec-github", "~> 2.4.0" diff --git a/lib/prius/registry.rb b/lib/prius/registry.rb index abe78bf..f76eba7 100644 --- a/lib/prius/registry.rb +++ b/lib/prius/registry.rb @@ -18,10 +18,11 @@ def initialize(env) def load(name, env_var: nil, type: :string, required: true) env_var = name.to_s.upcase if env_var.nil? @registry[name] = case type - when :string then load_string(env_var, required) - when :int then load_int(env_var, required) - when :bool then load_bool(env_var, required) - when :date then load_date(env_var, required) + when :string then load_string(env_var, required) + when :int then load_int(env_var, required) + when :bool then load_bool(env_var, required) + when :date then load_date(env_var, required) + when :datetime then load_datetime(env_var, required) else raise ArgumentError, "Invalid type #{type}" end end @@ -73,5 +74,14 @@ def load_date(name, required) rescue ArgumentError raise TypeMismatchError, "'#{name}' value '#{value}' is not a date" end + + def load_datetime(name, required) + value = load_string(name, required) + return nil if value.nil? + + DateTime.parse(value) + rescue ArgumentError + raise TypeMismatchError, "'#{name}' value '#{value}' is not a datetime" + end end end diff --git a/lib/prius/version.rb b/lib/prius/version.rb index 4b616da..3e91336 100644 --- a/lib/prius/version.rb +++ b/lib/prius/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Prius - VERSION = "6.0.0" + VERSION = "6.1.0" end diff --git a/spec/prius/registry_spec.rb b/spec/prius/registry_spec.rb index 8685e60..eece2fb 100644 --- a/spec/prius/registry_spec.rb +++ b/spec/prius/registry_spec.rb @@ -10,6 +10,8 @@ "ALIVE" => "Yes", "BORN" => "2022-09-02", "INVALID_DATE" => "2022-02-99", + "HELIUM_RELEASE_DATETIME" => "2024-01-31 00:00 UTC", + "INVALID_RELEASE_DATETIME" => "2024-01-31 00:99 UTC", } end let(:registry) { described_class.new(env) } @@ -115,6 +117,34 @@ end end end + + context "when specifying :datetime as the type" do + context "given a datetime value" do + it "doesn't blow up" do + expect { registry.load(:helium_release_datetime, type: :datetime) }.to_not raise_error + end + + it "stores a datetime" do + registry.load(:helium_release_datetime, type: :datetime) + expect(registry.get(:helium_release_datetime)).to be_a(DateTime) + expect(registry.get(:helium_release_datetime)).to eq(DateTime.parse(env["HELIUM_RELEASE_DATETIME"])) + end + end + + context "given an invalid date value" do + it "blows up" do + expect { registry.load(:invalid_release_datetime, type: :datetime) }. + to raise_error(Prius::TypeMismatchError) + end + end + + context "given a non-date value" do + it "blows up" do + expect { registry.load(:name, type: :datetime) }. + to raise_error(Prius::TypeMismatchError) + end + end + end end describe "#get" do