Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetual/string.rb,
lib/perpetual/string/crypt.rb

Overview

– This file is part of Perpetual.

Copyright© 2012 Giovanni Capuano <[email protected]>

Perpetual is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Perpetual is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with Perpetual. If not, see <www.gnu.org/licenses/>. ++

Instance Method Summary collapse

Instance Method Details

#at(position) ⇒ Object

Returns the character at the position.



22
23
24
# File 'lib/perpetual/string.rb', line 22

def at(position)
  self[position, 1].to_s
end

#base64_decodeObject

Returns the base64 decoded string of the hash.



63
64
65
# File 'lib/perpetual/string/crypt.rb', line 63

def base64_decode
  Crypt::base64_decode self
end

#base64_encodeObject Also known as: to_base64, base64

Returns the base64 encoded hash of the string.



56
57
58
# File 'lib/perpetual/string/crypt.rb', line 56

def base64_encode
  Crypt::base64_encode self
end

#blank?Boolean

Returns true if the string contains only whitespaces or is empty.

Returns:

  • (Boolean)


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

def blank?
  self !~ %r![^\s#{[0x3000].pack("U")}]!
end

#cut(limit) ⇒ Object Also known as: truncate

Truncates the string after a given length if text is longer than length.



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

def cut(limit)
  self.match(%r{^(.{0,#{limit}})})[1]
end

#first(limit = 1) ⇒ Object

Returns the first character of the string or the first limit characters.



38
39
40
41
42
43
44
45
46
# File 'lib/perpetual/string.rb', line 38

def first(limit = 1)
  if limit == 0
    ''
  elsif limit >= self.length
    self
  else
    self[0...limit]
  end
end

#from(position) ⇒ Object

Returns the remaining of the string from the position.



49
50
51
# File 'lib/perpetual/string.rb', line 49

def from(position)
  self[position, -1].to_s
end

#last(limit = 1) ⇒ Object

Returns the last character of the string or the last limit characters.



54
55
56
57
58
59
60
61
62
# File 'lib/perpetual/string.rb', line 54

def last(limit = 1)
  if limit == 0
    ''
  elsif limit >= self.length
    self
  else
    self[(-limit)..-1]
  end
end

#md5Object Also known as: to_md5

Returns the MD5 hash of the string.



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

def md5
  Crypt::md5 self
end

#numeric?Boolean

Returns true if the string is a valid integer.

Returns:

  • (Boolean)


65
66
67
# File 'lib/perpetual/string.rb', line 65

def numeric?
  self.to_i.to_s == self || self.to_f.to_s == self
end

#sha1Object Also known as: to_sha1

Returns the SHA1 hash of the string.



74
75
76
# File 'lib/perpetual/string/crypt.rb', line 74

def sha1
  Crypt::sha1 self
end

#strip_html_tagsObject Also known as: strip_tags

Removes from the string all the HTML(-like) tags.



70
71
72
# File 'lib/perpetual/string.rb', line 70

def strip_html_tags
  self.gsub(/<("[^"]*"|'[^']*'|[^'">])*>/, '')
end

#strip_html_tags!Object Also known as: strip_tags!



75
76
77
# File 'lib/perpetual/string.rb', line 75

def strip_html_tags!
  self.gsub!(/<("[^"]*"|'[^']*'|[^'">])*>/, '')
end

#titlecaseObject Also known as: titleize

Capitalizes all the single words in the string.



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

def titlecase
  self.gsub(/\b('?[a-z])/) { $1.capitalize }
end

#titlecase!Object Also known as: titleize!



86
87
88
# File 'lib/perpetual/string.rb', line 86

def titlecase!
  self.gsub!(/\b('?[a-z])/) { $1.capitalize }
end

#to(position) ⇒ Object

Returns the beginning of the string up to the position



92
93
94
# File 'lib/perpetual/string.rb', line 92

def to(position)
  self[0..position].to_s
end