Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and cleanup option handling #69

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,19 @@ def self.strip_string(value, options = {})
# Necessary because Rails has removed the narrowing of attributes using :only
# and :except on Base#attributes
def self.narrow(attributes, options = {})
if except = options[:except]
except = Array(except).collect { |attribute| attribute.to_s }
if options[:except]
except = Array(options[:except]).map(&:to_s)
attributes.except(*except)
elsif only = options[:only]
only = Array(only).collect { |attribute| attribute.to_s }
elsif options[:only]
only = Array(options[:only]).map(&:to_s)
attributes.slice(*only)
else
attributes
end
end

def self.validate_options(options)
if keys = options.keys
unless (keys - VALID_OPTIONS).empty?
raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
end
end
return if (options.keys - VALID_OPTIONS).empty?
raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
end
end