Skip to content

Commit

Permalink
Refactoring method to leverage safe_constantize
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyf committed Dec 7, 2022
1 parent 300fb6e commit f359447
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/qa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,12 @@ class DataNormalizationError < StandardError; end
# @return [#search, #find, #fetch]
def self.authority_for(vocab:, subauthority: nil)
authority_constant_name = "Qa::Authorities::#{vocab.to_s.camelcase}"
begin
authority_constant = authority_constant_name.constantize
rescue NameError
raise Qa::InvalidAuthorityError, authority_constant_name
end
authority_constant = authority_constant_name.safe_constantize
raise Qa::InvalidAuthorityError, authority_constant_name if authority_constant.nil?

if authority_constant.is_a?(Class)
authority_constant.new
elsif subauthority.present?
authority_constant.subauthority_for(subauthority)
else
raise Qa::MissingSubAuthority, "No sub-authority provided"
end
return authority_constant.new if authority_constant.is_a?(Class)
return authority_constant.subauthority_for(subauthority) if subauthority.present?

raise Qa::MissingSubAuthority, "No sub-authority provided"
end
end

0 comments on commit f359447

Please sign in to comment.