-
-
Save Pserg/8a979ce28c2e67721a04a13a32928a93 to your computer and use it in GitHub Desktop.
This file contains hidden or 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