Method: Hoodoo::Errors#unjoin_and_unescape_commas

Defined in:
lib/hoodoo/errors/errors.rb

#unjoin_and_unescape_commas(str) ⇒ Object

When reference data is specified for errors, the reference values are concatenated together into a comma-separated string. Since reference values can themselves contain commas, comma is escaped with “\,” and “\” escaped with “\\”.

Call here with such a string; return an array of ‘unescaped’ values.

str

Value-escaped (“\\” / “\,”) comma-separated string. Unescaped commas separate individual values.



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/hoodoo/errors/errors.rb', line 278

def unjoin_and_unescape_commas( str )

  # In Ruby regular expressions, '(?<!pat)' is a negative lookbehind
  # assertion, making sure that the preceding characters do not match
  # 'pat'. To split the string joined on ',' to an array but not splitting
  # any escaped '\,', then, we can use this rather opaque split regexp:
  #
  #   error[ 'reference' ].split( /(?<!\\),/ )
  #
  # I.e. split on ',', provided it is not preceded by a '\' (escaped in the
  # regexp to '\\').

  ary = str.split( /(?<!\\),/ )
  ary.map { | entry | unescape_commas( entry ) }
end