Sha256: 9ce2b7c559ee55478671a21dc41c584fe92232fb0bf5ab0ea99db018225177cd

Contents?: true

Size: 807 Bytes

Versions: 1

Compression:

Stored size: 807 Bytes

Contents

# frozen_string_literal: true

require 'magic/core_ext'

Magic::CoreExt.kernel do
	# Yields self to the block and returns the result of the block if it’s
	# truthy, and self otherwise.
	#
	#   rand(6)                              # returns 0–5
	#     .optional { it + 1 if one_based? } # returns 1–6 if 1-based, or
	#                                        # the original 0–5 otherwise
	#
	# It can be considered as a conditional +then+.
	#
	# Good usage for +optional+ is value piping in method chains with
	# conditional processing:
	#
	#   @people = Person
	#     .optional { it.where created_at: (1.hour.ago...) if new? }
	#     .optional { anonymize it if gdpr? }
	#     .optional { try :decorate }
	#
	def optional
		tap do
			result = yield(self) or
					next

			break result
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magic-support-0.1.0 lib/magic/core_ext/kernel/optional.rb