Last active
July 17, 2021 00:02
-
-
Save apneadiving/076ad5b412e5b1f063a13d34e59a35a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParamsSlicer | |
def initialize(params, *whitelist) | |
@params = params | |
@nested_whitelist = whitelist.extract_options! | |
@whitelist = whitelist | |
end | |
def call | |
params.slice(*whitelist).tap do |result| | |
nested_whitelist.each do |k, v| | |
result[k] = params[k].slice(*v) | |
end | |
end | |
end | |
private | |
attr_reader :params, :nested_whitelist, :whitelist | |
end | |
h = {name: 'Mike', address_attributes: {city: 'Berlin', street: 'lisenhof'}} | |
ParamsSlicer.new(h, :name, address_attributes: %i[city]).call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment