Skip to content

Instantly share code, notes, and snippets.

@ManickYoj
Last active September 16, 2018 22:20
Show Gist options
  • Save ManickYoj/115a73ee54cfd9fa42ff70332f2fd7b8 to your computer and use it in GitHub Desktop.
Save ManickYoj/115a73ee54cfd9fa42ff70332f2fd7b8 to your computer and use it in GitHub Desktop.
Check for nil arguments automatically
#
# Hey folks! This Gist details how to use the `check_for_nil_arguments!` method,
# which is available to you via including NilArgsCheckHelper into the class of
# your choice.
#
# Briefly, it's a shorthand for checking if any of your arguments are nil without
# explicitly writing out checks for each one you care about. This is especially
# helpful with methods that use a long list of keyword required arguments.
#
# For real usage examples, check out `bulk_label_data.rb`
#
# Also, this code is defined in `nil_args_check_helper.rb`, so look there if you
# want to see the implementation. Feel free to ask me questions as well.
#
# -Nick (04.02.18)
#
# -- BRIEF EXAMPLES
# Example 1: Check required and required keyword arguments (common use case)
class Foo
include NilArgsCheckHelper
def foo(bar, bat=nil, baz:, bizzle: nil)
check_for_nil_arguments!(binding)
# ...
end
end
f = Foo.new
f.foo(1, 2, baz: 3, bizzle: 4) # => ... (no error)
f.foo(1, nil, baz: 3, bizzle: nil) # => ... (no error)
f.foo(1, baz: 2) # => ... (no error)
f.foo(nil, baz: 2) # => NilArgsCheckHelper::NilArgumentError: ... bar ...
f.foo(1, baz: nil) # => NilArgsCheckHelper::NilArgumentError: ... baz ...
# Example 2: Check only optional and keyword (non-required) arguments (because you're an odd duck)
class Foo
include NilArgsCheckHelper
def foo(bar, bat=nil, baz:, bizzle: nil)
check_for_nil_arguments!(binding, [:opt, :key])
end
end
f = Foo.new
f.foo(1, 2, baz: 3, bizzle: 4) # => ... (no error)
f.foo(1, nil, baz: 3, bizzle: nil) # => ... NilArgumentError: ... bat ...
f.foo(nil, 2, baz: nil, bizzle: 4) # => ... (no error)
f.foo(nil, 2, baz: nil) # => ... NilArgumentError: ... bizzle ...
# -- METHOD DOCUMENTATION
#
# Check the calling method to see if any of its required arguments or
# required keyword arguments are nil.
#
# @param [Binding] client_binding The Binding context of the method calling
# this one. Get this using the `binding` method, which will be defined
# wherever you call it
# @param [Array<Symbol>] types_to_check: The parameter types that, if
# their argument is nil, you would like to have an error thrown for.
# defaults to types defined in `TYPES_TO_CHECK`
#
# @note Checking arguments for nil using this method is ~ an order of
# magnitude slower than doing it manually. For checking 2 required args
# out of 4 total args, 1000 runs of check_for_nil_arguments! took 0.0326
# seconds, whereas doing the same checks 1000 times with `if arg.nil?` for
# the required args took 0.0057 seconds.
#
# @raise [NilArgsCheckHelper::NilArgumentError] If any of the passed
# arguments are nil and should not be.
#
# @return [void]
#
# Useful reference for what options you have available and which are the default
TYPES_TO_CHECK = [
:req, # eg. foo(bar)
# :opt, # eg. foo(bar = 5)
:keyreq, # eg. foo(bar:)
# :key, # eg. foo(bar: 5)
].freeze
# Method signature
def check_for_nil_arguments!(
client_binding,
types_to_check = TYPES_TO_CHECK
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment