module MySQLExpectations::ArrayRefinements
Monkey patch Array to have to_sentence
. to_sentence
converts the array to a delimited string. you pick the delimiter, you pick the coordinating conjunction, you pick serial delimiter or not.
Serial delimiter (or serial comma in the case the delimiter is a comma), means to include the comma between the last two items in the list.
Public Instance Methods
delimited_conjunction(conjunction, delimiter, serial_delimiter)
click to toggle source
# File lib/mysql_expectations/array_refinements.rb, line 21 def delimited_conjunction(conjunction, delimiter, serial_delimiter) delimited_conjunction = conjunction delimited_conjunction += ' ' unless conjunction.empty? if serial_delimiter delimited_conjunction = delimiter + delimited_conjunction else delimited_conjunction = ' ' + delimited_conjunction end delimited_conjunction end
quote_elements(quote_with)
click to toggle source
# File lib/mysql_expectations/array_refinements.rb, line 17 def quote_elements(quote_with) map { |e| "#{quote_with}#{e}#{quote_with}" } end
to_sentence( quote_with: "'", delimiter: ', ', conjunction: 'and', serial_delimiter: true )
click to toggle source
# File lib/mysql_expectations/array_refinements.rb, line 32 def to_sentence( quote_with: "'", delimiter: ', ', conjunction: 'and', serial_delimiter: true ) return '' if self.empty? words = quote_elements(quote_with) return words[0] if words.size == 1 return "#{words[0]} #{conjunction} #{words[1]}" if words.size == 2 delimited_conjunction = delimited_conjunction( conjunction, delimiter, serial_delimiter) [words[0...-1].join(delimiter), words.last].join(delimited_conjunction) end