Skip to content

Commit

Permalink
Refactor loading logic
Browse files Browse the repository at this point in the history
Split the logic for loading a value from parsing the value, that way we
only need to check if the env var is required in one place.
  • Loading branch information
ameykusurkar committed Jul 12, 2024
1 parent 3cc4af0 commit d88c117
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lib/prius/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def initialize(env)
# See Prius.load for documentation.
def load(name, env_var: nil, type: :string, required: true)
env_var = name.to_s.upcase if env_var.nil?
value = load_value(env_var, required)
@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 value
when :int then parse_int(env_var, value)
when :bool then parse_bool(env_var, value)
when :date then parse_date(env_var, value)
else raise ArgumentError, "Invalid type #{type}"
end
end
Expand All @@ -35,40 +36,31 @@ def get(name)

private

def load_string(name, required)
def load_value(name, required)
@env.fetch(name)
rescue KeyError
return nil unless required

raise MissingValueError, "config value '#{name}' not present"
end

def load_int(name, required)
value = load_string(name, required)
return value if value.nil?

def parse_int(name, value)
unless /\A[0-9]+\z/.match?(value)
raise TypeMismatchError, "'#{name}' value '#{value}' is not an integer"
end

value.to_i
end

def load_bool(name, required)
value = load_string(name, required)
return nil if value.nil?

def parse_bool(name, value)
value = value.downcase
return true if %w[yes y true t 1].include?(value)
return false if %w[no n false f 0].include?(value)

raise TypeMismatchError, "'#{name}' value '#{value}' is not a boolean"
end

def load_date(name, required)
value = load_string(name, required)
return nil if value.nil?

def parse_date(name, value)
Date.parse(value)
rescue ArgumentError
raise TypeMismatchError, "'#{name}' value '#{value}' is not a date"
Expand Down

0 comments on commit d88c117

Please sign in to comment.