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

+ (Object) escape_single_quote(string)

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



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

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

+ (Object) generate_md5(text)



31
32
33
34
35
# File 'lib/utilities/strings.rb', line 31

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

+ (Object) increment_contained_integer(string)



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

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

Returns:

  • (Boolean)

    whether the string is an integer (positive or negative)



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

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

Returns:

  • (String, nil)

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



23
24
25
26
27
28
29
# File 'lib/utilities/strings.rb', line 23

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

Returns:

  • (String, nil)

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



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

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

Returns:

  • (String)

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



68
69
70
71
# File 'lib/utilities/strings.rb', line 68

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

+ (Object) random_string(string_length)

return [String, nil]

stub a string of a certain length


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

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

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



61
62
63
64
# File 'lib/utilities/strings.rb', line 61

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