diff --git a/README.md b/README.md index 84e6cff..5c8bb03 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,13 @@ methods onto `ENV` for your use. ## Usage -``` +```shell ENV.string("APP_NAME", default: "local") ENV.symbol("BUSINESS_DOMAIN", default: :engineering, required: true) ENV.boolean("ENABLE_FEATURE_FOO", default: false) ENV.integer_range("ID_RANGE", default: (500..6000)) ENV.integer("MAX_THREAD_COUNT", default: 5) +ENV.file_path("FILE_PATH", default: "/some/path", required: true) ENV.date("SCHEDULED_DATE", required: true, format: "%Y-%m-%d") ``` @@ -46,6 +47,7 @@ isn't present in the environment. If `required` is set to a truthy value, then i present in the environment, an `EnvironmentHelpers::MissingVariableError` is raised. The available methods added to `ENV`: + * `string` - environment values are already strings, so this is the simplest of the methods. * `symbol` - produces a symbol, and enforces that the default value is either `nil` or a Symbol. * `boolean` - produces `nil`, `true`, or `false` (and only allows those as defaults). Supports.. @@ -57,6 +59,7 @@ The available methods added to `ENV`: * `integer` - produces an integer from the environment variable, by calling `to_i` on it (if it's present). Note that this means that providing a value like "hello" means you'll get `0`, since that's what ruby does when you call `"hello".to_i`. +* `file_path` - produces a `Pathname` initialized with the path specified by the environment variable. * `date` - produces a `Date` object, using `Date.strptime`. The default format string is `%Y-%m-%d`, which would parse a date like `2023-12-25`. It will handle invalid values (or format strings) like the variable not being present, though if it's specified as `required`, you will see a different diff --git a/lib/environment_helpers.rb b/lib/environment_helpers.rb index 1a9c10d..81a712e 100644 --- a/lib/environment_helpers.rb +++ b/lib/environment_helpers.rb @@ -3,6 +3,7 @@ require_relative "./environment_helpers/boolean_helpers" require_relative "./environment_helpers/range_helpers" require_relative "./environment_helpers/numeric_helpers" +require_relative "./environment_helpers/file_helpers" require_relative "./environment_helpers/datetime_helpers" module EnvironmentHelpers @@ -21,6 +22,7 @@ module EnvironmentHelpers include BooleanHelpers include RangeHelpers include NumericHelpers + include FileHelpers include DatetimeHelpers end diff --git a/lib/environment_helpers/file_helpers.rb b/lib/environment_helpers/file_helpers.rb new file mode 100644 index 0000000..d9c46bb --- /dev/null +++ b/lib/environment_helpers/file_helpers.rb @@ -0,0 +1,12 @@ +require "pathname" + +module EnvironmentHelpers + module FileHelpers + def file_path(name, default: nil, required: false) + check_default_type(:file_path, default, String) + path = fetch_value(name, required: required) || default + return nil if path.nil? + Pathname.new(path) + end + end +end diff --git a/lib/environment_helpers/version.rb b/lib/environment_helpers/version.rb index 5d887eb..787cae4 100644 --- a/lib/environment_helpers/version.rb +++ b/lib/environment_helpers/version.rb @@ -1,3 +1,3 @@ module EnvironmentHelpers - VERSION = "1.1.0" + VERSION = "1.2.0" end diff --git a/spec/environment_helpers/file_helpers_spec.rb b/spec/environment_helpers/file_helpers_spec.rb new file mode 100644 index 0000000..381bd5c --- /dev/null +++ b/spec/environment_helpers/file_helpers_spec.rb @@ -0,0 +1,51 @@ +RSpec.describe EnvironmentHelpers::FileHelpers do + subject(:env) { ENV } + + describe "#file_path" do + let(:name) { "FOO" } + + context "with required: true" do + subject(:file_path) { ENV.file_path(name, required: true) } + + context "when the environment value is not set" do + before { expect(ENV["FOO"]).to be_nil } + + it "raises a MissingVariableError" do + expect { file_path }.to raise_error(EnvironmentHelpers::MissingVariableError, /not supplied/) + end + end + + context "when the environment value is set" do + with_env("FOO" => "bar") + it { is_expected.to eq Pathname.new("bar") } + end + end + + context "without a default specified" do + subject(:file_path) { ENV.file_path(name) } + + context "when the environment value is not set" do + before { expect(ENV["FOO"]).to be_nil } + it { is_expected.to be_nil } + end + + context "when the environment value is set" do + with_env("FOO" => "bar") + it { is_expected.to eq Pathname.new("bar") } + end + end + + context "with a default specified" do + subject(:file_path) { ENV.file_path(name, default: "foo") } + + context "when the environment value is not set" do + it { is_expected.to eq Pathname.new("foo") } + end + + context "when the environment value is set" do + with_env("FOO" => "bar") + it { is_expected.to eq Pathname.new("bar") } + end + end + end +end