Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/midwire_common/string.rb

Overview

A light-weight super String class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')]) ⇒ Object



4
5
6
7
# File 'lib/midwire_common/string.rb', line 4

def random(count = 6, ranges = [('a'..'z'), ('A'..'Z'), ('0'..'9')])
  coll = ranges.map(&:to_a).flatten
  (0..(count - 1)).map { coll[rand(coll.length)] }.join
end

Instance Method Details

#alpha_numeric?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/midwire_common/string.rb', line 57

def alpha_numeric?
  regex = /^[a-zA-Z0-9]+$/
  (self =~ regex) == 0 ? true : false
end

#camelizeObject

rubocop:disable Style/PerlBackrefs



125
126
127
# File 'lib/midwire_common/string.rb', line 125

def camelize
  gsub(/\/(.?)/) { '::' + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
end

#email_address?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/midwire_common/string.rb', line 62

def email_address?
  email_regex = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

  (self =~ email_regex) == 0 ? true : false
end

#escape_double_quotesObject



112
113
114
# File 'lib/midwire_common/string.rb', line 112

def escape_double_quotes
  gsub(/["]/, '\\\\\"')
end

#escape_single_quotesObject



108
109
110
# File 'lib/midwire_common/string.rb', line 108

def escape_single_quotes
  gsub(/[']/, '\\\\\'')
end

#format_phoneObject



80
81
82
83
# File 'lib/midwire_common/string.rb', line 80

def format_phone
  gsub!(/[a-z,! \-\(\)\:\;\.\&\$]+/i, '')
  '(' << slice(0..2) << ')' << slice(3..5) << '-' << slice(-4, 4)
end

#here_with_pipe(delimeter = ' ') ⇒ Object

html = <<-stop.here_with_pipe(delimeter=“n”)

|<!-- Begin: comment  -->
|<script type="text/javascript">

stop



50
51
52
53
54
55
# File 'lib/midwire_common/string.rb', line 50

def here_with_pipe(delimeter = ' ')
  lines = split("\n")
  lines.map! { |c| c.sub!(/\s*\|/, '') }
  new_string = lines.join(delimeter)
  replace(new_string)
end

#left_substr(count) ⇒ Object



10
11
12
# File 'lib/midwire_common/string.rb', line 10

def left_substr(count)
  slice(0, count)
end

#left_trimObject



18
19
20
21
# File 'lib/midwire_common/string.rb', line 18

def left_trim
  # remove leading whitespace
  gsub(/^[\t\s]+/, '')
end

#left_trim!Object



23
24
25
# File 'lib/midwire_common/string.rb', line 23

def left_trim!
  gsub!(/^[\t\s]+/, '') || ''
end

#numeric?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/midwire_common/string.rb', line 72

def numeric?
  Float(self)
rescue
  false # not numeric
else
  true # numeric
end

#right_substr(count) ⇒ Object



14
15
16
# File 'lib/midwire_common/string.rb', line 14

def right_substr(count)
  slice(-count, count)
end

#right_trimObject



27
28
29
30
# File 'lib/midwire_common/string.rb', line 27

def right_trim
  # remove trailing whitespace
  gsub(/[\t\s]+$/, '')
end

#right_trim!Object



32
33
34
# File 'lib/midwire_common/string.rb', line 32

def right_trim!
  gsub!(/[\t\s]+$/, '') || ''
end

#sanitizeObject



85
86
87
# File 'lib/midwire_common/string.rb', line 85

def sanitize
  gsub(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
end

#sanitize!Object



89
90
91
# File 'lib/midwire_common/string.rb', line 89

def sanitize!
  gsub!(/[^a-z0-9,! \-\(\)\:\;\.\&\$]+/i, '')
end

#shorten(maxcount = 30) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/midwire_common/string.rb', line 93

def shorten(maxcount = 30)
  if length >= maxcount
    shortened = self[0, maxcount]
    splitted = shortened.split(/\s/)
    if splitted.length > 1
      words = splitted.length
      splitted[0, words - 1].join(' ') + '...'
    else
      shortened[0, maxcount - 3] + '...'
    end
  else
    self
  end
end

#snakerizeObject



116
117
118
119
120
121
122
# File 'lib/midwire_common/string.rb', line 116

def snakerize
  gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('-', '_')
      .downcase
end

#trimObject



36
37
38
39
# File 'lib/midwire_common/string.rb', line 36

def trim
  # remove leading and trailing whitespace
  left_trim.right_trim
end

#trim!Object



41
42
43
44
# File 'lib/midwire_common/string.rb', line 41

def trim!
  # remove leading and trailing whitespace
  left_trim!.right_trim! || ''
end

#zipcode?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/midwire_common/string.rb', line 68

def zipcode?
  self =~ %r{^(\d{5})(-\d{4})?$}x ? true : false
end