Module: Cuba::TextHelpers

Defined in:
lib/cuba/text_helpers.rb

Instance Method Summary collapse

Instance Method Details

#currency(amount, unit = "$") ⇒ Object



17
18
19
# File 'lib/cuba/text_helpers.rb', line 17

def currency(amount, unit = "$")
  "#{unit} %.2f" % amount
end

#delimit(number, delimiter = ",") ⇒ Object



21
22
23
# File 'lib/cuba/text_helpers.rb', line 21

def delimit(number, delimiter = ",")
  number.to_s.gsub(%r{(\d)(?=(\d\d\d)+(?!\d))}, "\\1#{delimiter}")
end

#markdown(str) ⇒ Object



3
4
5
# File 'lib/cuba/text_helpers.rb', line 3

def markdown(str)
  Markdown.new(str).to_html
end

#nl2br(str) ⇒ Object



13
14
15
# File 'lib/cuba/text_helpers.rb', line 13

def nl2br(str)
  str.to_s.gsub(/\n|\r\n/, "<br>")
end

#titlecase(str) ⇒ Object



25
26
27
28
29
30
# File 'lib/cuba/text_helpers.rb', line 25

def titlecase(str)
  res = str.to_s.dup
  res.tr!("_", " ")
  res.gsub!(/(^|\s)([a-z])/) { |char| char.upcase }
  res
end

#truncate(str, length, ellipses = "...") ⇒ Object



7
8
9
10
11
# File 'lib/cuba/text_helpers.rb', line 7

def truncate(str, length, ellipses = "...")
  return str if !str || str.length <= length

  sprintf("%.#{length}s#{ellipses}", str)
end

#underscore(str) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cuba/text_helpers.rb', line 32

def underscore(str)
  res = str.to_s.dup
  res.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  res.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  res.downcase!
  res
end