Module: Xqsr3::ArrayUtilities::JoinWithOr

Extended by:
JoinWithOr
Included in:
JoinWithOr
Defined in:
lib/xqsr3/array_utilities/join_with_or.rb

Overview

include-able module that provides sequence-joining functionality

Instance Method Summary collapse

Instance Method Details

#join_with_or(ar, **options) ⇒ Object

Joins an array with grammatical appropriateness (with an ‘or’)

Signature

  • Parameters:

    • ar (Array) The array whose contents are to be joined

    • options (Hash) Options that control the behaviour of the method

  • Options:

    • :or (String) A string that is used instead of ‘or’

    • :oxford_comma (boolean) Determines whether an Oxford comma will be used. Default is true

    • :quote_char (String) The quote character. Default is empty string ”

    • :separator (String) The separator character. Default is ‘,’



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xqsr3/array_utilities/join_with_or.rb', line 77

def join_with_or ar, **options

	::Xqsr3::Quality::ParameterChecking.check_parameter ar, 'ar', type: ::Array, allow_nil: true
	::Xqsr3::Quality::ParameterChecking.check_parameter options, 'options', type: ::Hash, allow_nil: false

	::Xqsr3::Quality::ParameterChecking.check_parameter options[:or], ':or', type: ::String, option: true, allow_nil: true
	::Xqsr3::Quality::ParameterChecking.check_parameter options[:oxford_comma], ':oxford_comma', types: [ ::FalseClass, ::TrueClass ], option: true, allow_nil: true
	::Xqsr3::Quality::ParameterChecking.check_parameter options[:quote_char], ':quote_char', type: ::String, option: true, allow_nil: true
	::Xqsr3::Quality::ParameterChecking.check_parameter options[:separator], ':separator', type: ::String, option: true, allow_nil: true

	return '' if ar.nil?
	return '' if ar.empty?

	separator	=	options[:separator] || ','
	or_word		=	options[:or] || 'or'
	ox_comma	=	(options.has_key?(:oxford_comma) && !options[:oxford_comma]) ? '' : separator
	quote_char	=	options[:quote_char]

	ar			=	ar.map { |v| "#{quote_char}#{v}#{quote_char}" } if quote_char

	case ar.size
	when 1
		ar[0]
	when 2
		"#{ar[0]} #{or_word} #{ar[1]}"
	else
		"#{ar[0...-1].join(separator + ' ')}#{ox_comma} #{or_word} #{ar[-1]}"
	end
end