Module: Utilities::Strings

Defined in:
lib/utilities/strings.rb

Overview

Methods that recieve or generate a String. This methods in this library should be completely independant (i.e. ultimately gemifiable) from TaxonWorks.

Class Method Summary (collapse)

Class Method Details

+ (Array) alphabetic_strings(string)

Returns array of whitespace split strings, with any string containing a digit eliminated

Parameters:

  • string (String)

Returns:

  • (Array)

    array of whitespace split strings, with any string containing a digit eliminated



98
99
100
101
# File 'lib/utilities/strings.rb', line 98

def self.alphabetic_strings(string)
  a = string.gsub(/[^a-zA-Z]/, ' ').split(/\s+/)
  a.empty? ? [] : a
end

+ (String?) authorship_sentence(last_names = [])

TODO: DEPRECATE

Parameters:

  • last_names (Array) (defaults to: [])

Returns:

  • (String, nil)


90
91
92
93
# File 'lib/utilities/strings.rb', line 90

def self.authorship_sentence(last_names = [])
  return nil if last_names.empty?
  last_names.to_sentence(two_words_connector: ' & ', last_word_connector: ' & ')
end

+ (String) escape_single_quote(string)

Adds a second single quote to escape apostrophe in SQL query strings

Parameters:

  • string (String)

Returns:

  • (String)


54
55
56
57
# File 'lib/utilities/strings.rb', line 54

def self.escape_single_quote(string)
  return nil if string.blank?
  string.gsub("'", "''")
end

+ (Digest::MD5) generate_md5(text)

Parameters:

  • text (String)

Returns:

  • (Digest::MD5)


36
37
38
39
40
# File 'lib/utilities/strings.rb', line 36

def self.generate_md5(text)
  return nil if text.blank?
  text = text.downcase.gsub(/[\s\.,;:\?!]*/, '')
  Digest::MD5.hexdigest(text)
end

+ (String) increment_contained_integer(string)

Parameters:

  • string (String)

Returns:

  • (String)


44
45
46
47
48
49
# File 'lib/utilities/strings.rb', line 44

def self.increment_contained_integer(string)
  string =~ /([^\d]*)(\d+)([^\d]*)/
  a, b, c = $1, $2, $3
  return false if b.nil?
  [a,(b.to_i + 1), c].compact.join
end

+ (Boolean) is_i?(string)

see stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby Note: Might checkout CSV::Converters constants to see how they handle this

Parameters:

  • string (String)

Returns:

  • (Boolean)

    whether the string is an integer (positive or negative)



64
65
66
# File 'lib/utilities/strings.rb', line 64

def self.is_i?(string)
  /\A[-+]?\d+\z/ === string
end

+ (String?) nil_squish_strip(string)

Returns strips pre/post fixed space and condenses internal spaces, but returns nil (not empty string) if nothing is left

Parameters:

  • string (String)

Returns:

  • (String, nil)

    strips pre/post fixed space and condenses internal spaces, but returns nil (not empty string) if nothing is left



26
27
28
29
30
31
32
# File 'lib/utilities/strings.rb', line 26

def self.nil_squish_strip(string)
  if !string.nil?
    string.squish!
    string = nil if string == ''
  end
  string
end

+ (String?) nil_strip(string)

Returns strips space, leaves internal whitespace as is, returns nil if nothing is left

Parameters:

  • string (String)

Returns:

  • (String, nil)

    strips space, leaves internal whitespace as is, returns nil if nothing is left



15
16
17
18
19
20
21
# File 'lib/utilities/strings.rb', line 15

def self.nil_strip(string) # string should have content or be empty
  if !string.nil?
    string.strip!
    string = nil if string == ''
  end
  string
end

+ (String) nil_wrap(pre = nil, content = nil, post = nil)

return nil if content.nil?, else wrap and return string if provided

Parameters:

  • pre (String) (defaults to: nil)
  • content (String) (defaults to: nil)
  • post (String) (defaults to: nil)

Returns:

  • (String)


82
83
84
85
# File 'lib/utilities/strings.rb', line 82

def self.nil_wrap(pre = nil, content = nil, post = nil)
  return nil if content.blank?
  [pre, content, post].compact.join.html_safe
end

+ (String?) random_string(string_length)

Returns stub a string of a certain length

Parameters:

  • string_length (Integer)

Returns:

  • (String, nil)

    stub a string of a certain length



7
8
9
10
# File 'lib/utilities/strings.rb', line 7

def self.random_string(string_length)
  return nil if string_length.to_i == 0
  ('a'..'z').to_a.shuffle[0, string_length].join
end

+ (String, param) sanitize_for_csv(string)

Returns the goal is to sanitizie an individual string such that it is usable in TAB delimited, UTF-8, column. See Download TODO: Likely need to handle quotes

Parameters:

  • string (String)

Returns:

  • (String, param)

    the goal is to sanitizie an individual string such that it is usable in TAB delimited, UTF-8, column. See Download TODO: Likely need to handle quotes



72
73
74
75
# File 'lib/utilities/strings.rb', line 72

def self.sanitize_for_csv(string)
  return string if string.blank?
  string.to_s.gsub(/\n|\t/, ' ')
end