Class: String

Inherits:
Object show all
Defined in:
lib/commonthread/monkey_patches.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rand(length = 8) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/commonthread/monkey_patches.rb', line 40

def self.rand(length = 8)
  alphabet = ('a' .. 'z').to_a + ('0' .. '9').to_a
  rand_string = ''
  length.times do
    rand_string << alphabet.rand
  end
  rand_string
end

.rand_hex(length = 12) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/commonthread/monkey_patches.rb', line 49

def self.rand_hex(length = 12)
  alphabet = ('a' .. 'f').to_a + ('0' .. '9').to_a
  rand_string = ''
  length.times do
    rand_string << alphabet.rand
  end
  rand_string
end

Instance Method Details

#convert(pattern, template) ⇒ Object



2
3
4
5
6
# File 'lib/commonthread/monkey_patches.rb', line 2

def convert(pattern, template)
  output = String.new(template)
  self.match(pattern).to_a.each_with_index{|group, index| output.gsub!("$#{index}", group.to_s)}
  output
end

#date?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/commonthread/monkey_patches.rb', line 71

def date?
  Date.parse(self)
  true
rescue
  false
end

#email?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/commonthread/monkey_patches.rb', line 58

def email?
  if self =~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
    return true
  else
    return false
  end
end

#escapeObject



8
9
10
# File 'lib/commonthread/monkey_patches.rb', line 8

def escape
  CGI::escape(self)
end

#phone?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/commonthread/monkey_patches.rb', line 66

def phone?
  return false if self.nil?
  self.gsub(/[^0123456789]/, '').length > 9
end

#to_md5Object



20
21
22
# File 'lib/commonthread/monkey_patches.rb', line 20

def to_md5
  Digest::MD5.hexdigest(self)
end

#to_possesiveObject



24
25
26
# File 'lib/commonthread/monkey_patches.rb', line 24

def to_possesive
  self[-1].chr.downcase == 's' ? "#{self}'" : "#{self}'s"
end

#to_pretty_urlObject



36
37
38
# File 'lib/commonthread/monkey_patches.rb', line 36

def to_pretty_url
  self.strip.downcase.gsub(/\s+/, '_').gsub(/[^\w_-]/, '')
end

#to_shaObject



16
17
18
# File 'lib/commonthread/monkey_patches.rb', line 16

def to_sha
  Digest::SHA1.hexdigest(self)
end

#unescapeObject



12
13
14
# File 'lib/commonthread/monkey_patches.rb', line 12

def unescape
  CGI::unescape(self)
end

#wrap(before, after = nil) ⇒ Object



32
33
34
# File 'lib/commonthread/monkey_patches.rb', line 32

def wrap(before, after = nil)
  before + self + (after || before)
end

#xml_stripObject



28
29
30
# File 'lib/commonthread/monkey_patches.rb', line 28

def xml_strip
  self.gsub(/<.*?>/, '')
end