Module: Rocketeer::Helpers::Text

Defined in:
lib/rocketeer/helpers/text.rb

Instance Method Summary collapse

Instance Method Details

#pluralize(count, singular, plural = nil) ⇒ Object

Pluralizes the string.

@example:

pluralize comments.count, 'comment'

Parameters:

  • count (Integer)

    The count

  • singular (String)

    The singular form of the string

  • plural (String) (defaults to: nil)

    The plural form of the word

Author:

  • Jack Polgar

Since:

  • 0.1



35
36
37
# File 'lib/rocketeer/helpers/text.rb', line 35

def pluralize(count, singular, plural = nil)
  "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
end

#shorten_string(text, length = 30, truncate_string = "...") ⇒ Object

Shortens the string to the specified length

@example:

shorten_string 'A very, very, long string', 5

Parameters:

  • text (String)

    The string to shorten

  • length (Integer) (defaults to: 30)

    The length to shorten to

  • truncate_string (String) (defaults to: "...")

    Text to place at the end of the shortened string

Author:

  • Jack Polgar

Since:

  • 0.1



51
52
53
54
55
# File 'lib/rocketeer/helpers/text.rb', line 51

def shorten_string(text, length = 30, truncate_string = "...")
  return if text.nil?
  l = length - truncate_string.length
  text.length > length ? text[/\A.{#{l}}\w*\;?/m][/.*[\w\;]/m] + truncate_string : text
end