Sha256: a86ade9aa89baca01a49df6d82752aec7b099726529ed27e8d7b3856e26d28b7

Contents?: true

Size: 944 Bytes

Versions: 2

Compression:

Stored size: 944 Bytes

Contents

# encoding: utf-8

module RujitsuString
  # Return a string that can be used as part of a url
  # replaces basic "bad" characters with "-"
  def to_url
    self.downcase.gsub(/[^\-0-9a-z ]/, '').split.join('-')
  end

  # Truncates a string to the specified length,
  # and appends suffix if required
  #
  # Options:
  # * +length+ length to truncate string to. Includes the suffix in the length. Default is 50.
  # * +suffix+ suffix to append to truncated string. Pass "" or false for no suffix. Default is "...".
  #
  def truncate opts = {}
    opts[:length] ||= 50
    opts[:suffix] = opts.has_key?(:suffix) ? opts[:suffix] : "..."
    opts[:suffix] ||= ""
    opts[:length] -= (opts[:suffix].length+1)
    if opts[:length] > 0
      self.length > opts[:length] ? self[0..opts[:length]] + opts[:suffix] : self
    else
      opts[:suffix][0..(opts[:length] += opts[:suffix].length)]
    end
  end
end

String.send(:include, RujitsuString)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rujitsu-0.5.0 lib/rujitsu/string.rb
rujitsu-0.4.0 lib/rujitsu/string.rb