Skip to content

Commit

Permalink
Merge pull request #16 from nevinera/support-file-path
Browse files Browse the repository at this point in the history
Add support for file paths. Returns a Pathname object
  • Loading branch information
cdimartino authored Apr 25, 2023
2 parents 62511b6 + 4851563 commit 8af8328
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```

Expand All @@ -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..
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/environment_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,7 @@ module EnvironmentHelpers
include BooleanHelpers
include RangeHelpers
include NumericHelpers
include FileHelpers
include DatetimeHelpers
end

Expand Down
12 changes: 12 additions & 0 deletions lib/environment_helpers/file_helpers.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/environment_helpers/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EnvironmentHelpers
VERSION = "1.1.0"
VERSION = "1.2.0"
end
51 changes: 51 additions & 0 deletions spec/environment_helpers/file_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8af8328

Please sign in to comment.