module Ohm::Slug

Public Class Methods

included(model) click to toggle source
# File lib/ohm/slug.rb, line 3
def self.included(model)
  model.extend ClassMethods
end
slug(str = to_s) click to toggle source
# File lib/ohm/slug.rb, line 13
def slug(str = to_s)
  ret = transcode(str.dup)
  ret.gsub!("'", "")
  ret.gsub!(/[^0-9A-Za-z]/u, " ")
  ret.strip!
  ret.gsub!(/\s+/, "-")
  ret.downcase!
  return ret
end
transcode(str) click to toggle source
# File lib/ohm/slug.rb, line 24
def transcode(str)
  begin
    # TODO: replace with a String#encode version which will
    # contain proper transliteration tables. For now, Iconv
    # still wins because we get that for free.
    v, $VERBOSE = $VERBOSE, nil
    require "iconv"
    $VERBOSE = v

    Iconv.iconv("ascii//translit//ignore", "utf-8", str)[0]
  rescue LoadError
    return str
  end
end

Public Instance Methods

to_param() click to toggle source
# File lib/ohm/slug.rb, line 40
def to_param
  "#{ id }-#{ slug }"
end

Private Instance Methods

slug(str = to_s) click to toggle source
# File lib/ohm/slug.rb, line 13
def slug(str = to_s)
  ret = transcode(str.dup)
  ret.gsub!("'", "")
  ret.gsub!(/[^0-9A-Za-z]/u, " ")
  ret.strip!
  ret.gsub!(/\s+/, "-")
  ret.downcase!
  return ret
end
transcode(str) click to toggle source
# File lib/ohm/slug.rb, line 24
def transcode(str)
  begin
    # TODO: replace with a String#encode version which will
    # contain proper transliteration tables. For now, Iconv
    # still wins because we get that for free.
    v, $VERBOSE = $VERBOSE, nil
    require "iconv"
    $VERBOSE = v

    Iconv.iconv("ascii//translit//ignore", "utf-8", str)[0]
  rescue LoadError
    return str
  end
end