Class: String

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

Overview

This file is part of the “Utopia Framework” project, and is licensed under the GNU AGPLv3. Copyright 2010 Samuel Williams. All rights reserved. See <utopia.rb> for licensing details.

Constant Summary collapse

HTML_ESCAPE =
{"&" => "&amp;", "<" => "&lt;", ">" => "&gt;", "\"" => "&quot;"}
HTML_ESCAPE_PATTERN =
Regexp.new("[" + Regexp.quote(HTML_ESCAPE.keys.join) + "]")

Instance Method Summary collapse

Instance Method Details

#to_htmlObject



9
10
11
# File 'lib/utopia/extensions/string.rb', line 9

def to_html
	gsub(HTML_ESCAPE_PATTERN){|c| HTML_ESCAPE[c]}
end

#to_quoted_stringObject



13
14
15
# File 'lib/utopia/extensions/string.rb', line 13

def to_quoted_string
	'"' + self.gsub('"', '\\"').gsub(/\r/, "\\r").gsub(/\n/, "\\n") + '"'
end

#to_snakeObject



21
22
23
# File 'lib/utopia/extensions/string.rb', line 21

def to_snake
	self.gsub("::", "").gsub(/([A-Z]+)/){"_" + $1.downcase}.sub(/^_+/, "")
end

#to_titleObject



17
18
19
# File 'lib/utopia/extensions/string.rb', line 17

def to_title
	(" " + self).gsub(/[ \-_](.)/){" " + $1.upcase}.strip
end