Skip to content

Commit

Permalink
Refactor methods
Browse files Browse the repository at this point in the history
Ensures `ENVY_LOADED` env var is set regardless of how we leave
the block scope.
  • Loading branch information
akadusei committed Jun 2, 2024
1 parent 578d97c commit 91c1a34
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/envy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def from_file(*files, perm: nil)
set_perms(files, perm: perm)

if found = files.find { |file| File.readable?(file) }
next load_yaml(found, force: false)
return load_yaml(found, force: false)
end

raise Error, "Env file(s) not found or not readable"
Expand All @@ -26,7 +26,7 @@ def from_file!(*files, perm: nil)
set_perms(files, perm: perm)

if found = files.find { |file| File.readable?(file) }
next load_yaml(found, force: true)
return load_yaml(found, force: true)
end

raise Error, "Env file(s) not found or not readable"
Expand All @@ -37,6 +37,7 @@ def from_file!(*files, perm: nil)

def load_yaml(path, force:)
File.open(path) { |file| set_envs YAML.safe_load(file), force: force }
nil
end

def set_perms(files, perm: nil)
Expand Down Expand Up @@ -70,9 +71,10 @@ def load_wrap
var = "ENVY_LOADED"
return if ENV[var] == "yes"

yield

ENV[var] = "yes"
nil
begin
yield
ensure
ENV[var] = "yes"
end
end
end

0 comments on commit 91c1a34

Please sign in to comment.