Reputation: 8185
How can I clean up this ruby method signature?
def card(title: nil, textured: nil, threed: true,
borderless: false, bodyless: false, title_classes: ['card-header'])
The problem is that I get a linting/rubocop warning:
Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [6/5]
The reason I have so many keyword arguments for my method is, I made the method really flexible. It's powerful.
Upvotes: 1
Views: 2168
Reputation: 8185
Just do what rails does
# Creates a number field.
#
# ==== Options
# * <tt>:min</tt> - The minimum acceptable value.
# * <tt>:max</tt> - The maximum acceptable value.
# * <tt>:in</tt> - A range specifying the <tt>:min</tt> and
# <tt>:max</tt> values.
# * <tt>:within</tt> - Same as <tt>:in</tt>.
# * <tt>:step</tt> - The acceptable value granularity.
# * Otherwise accepts the same options as text_field_tag.
#
# ==== Examples
# number_field_tag 'quantity'
# # => <input id="quantity" name="quantity" type="number" />
#
# number_field_tag 'quantity', '1'
# # => <input id="quantity" name="quantity" type="number" value="1" />
#
# number_field_tag 'quantity', nil, class: 'special_input'
# # => <input class="special_input" id="quantity" name="quantity" type="number" />
#
# number_field_tag 'quantity', nil, min: 1
# # => <input id="quantity" name="quantity" min="1" type="number" />
#
# number_field_tag 'quantity', nil, max: 9
# # => <input id="quantity" name="quantity" max="9" type="number" />
#
# number_field_tag 'quantity', nil, in: 1...10
# # => <input id="quantity" name="quantity" min="1" max="9" type="number" />
#
# number_field_tag 'quantity', nil, within: 1...10
# # => <input id="quantity" name="quantity" min="1" max="9" type="number" />
#
# number_field_tag 'quantity', nil, min: 1, max: 10
# # => <input id="quantity" name="quantity" min="1" max="10" type="number" />
#
# number_field_tag 'quantity', nil, min: 1, max: 10, step: 2
# # => <input id="quantity" name="quantity" min="1" max="10" step="2" type="number" />
#
# number_field_tag 'quantity', '1', class: 'special_input', disabled: true
# # => <input disabled="disabled" class="special_input" id="quantity" name="quantity" type="number" value="1" />
def number_field_tag(name, value = nil, options = {})
options = options.stringify_keys
options["type"] ||= "number"
if range = options.delete("in") || options.delete("within")
options.update("min" => range.min, "max" => range.max)
end
text_field_tag(name, value, options)
end
Upvotes: 1
Reputation: 121000
Well, in theory you might use a keyword argument and Hash#fetch
to handle default values:
def card(**params)
title = params.fetch(:title, nil)
textured = params.fetch(:textured, nil)
threed = params.fetch(:threed, true)
borderless = params.fetch(:borderless, false)
bodyless = params.fetch(:bodyless, false)
title_classes = params.fetch(:title_classes, ['card-header'])
...
But my personal advice would be to just shut up rubocop with:
# rubocop:disable Metrics/ParameterLists
def card(...)
...
end
# rubocop:enable Metrics/ParameterLists
Upvotes: 4
Reputation: 22926
Try creating a case class/data class with those attributes.
class CardProperties
attr_accessor :title, :textured, :threed, :borderless, :bodyless, :title_classes
end
Create a new CardProperties
and pass it to the card method:
card_properties = CardProperties.new
card_properties.title = ''
....
card(card_properties)
Upvotes: 3