Module: Cuba::TextHelpers

Defined in:
lib/cuba/contrib/text_helpers.rb

Instance Method Summary collapse

Instance Method Details

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



19
20
21
# File 'lib/cuba/contrib/text_helpers.rb', line 19

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

#humanize(str) ⇒ Object



27
28
29
# File 'lib/cuba/contrib/text_helpers.rb', line 27

def humanize(str)
  titlecase(str.to_s.tr("_", " ").gsub(/_id$/, ""))
end

#markdown(str) ⇒ Object



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

def markdown(str)
  require "bluecloth"

  BlueCloth.new(str).to_html
end

#nl2br(str) ⇒ Object



15
16
17
# File 'lib/cuba/contrib/text_helpers.rb', line 15

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

#titlecase(str) ⇒ Object



23
24
25
# File 'lib/cuba/contrib/text_helpers.rb', line 23

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

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



9
10
11
12
13
# File 'lib/cuba/contrib/text_helpers.rb', line 9

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

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

#underscore(str) ⇒ Object



31
32
33
# File 'lib/cuba/contrib/text_helpers.rb', line 31

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