Skip to content

Instantly share code, notes, and snippets.

@hassox
Created March 6, 2010 11:07
Show Gist options
  • Select an option

  • Save hassox/323633 to your computer and use it in GitHub Desktop.

Select an option

Save hassox/323633 to your computer and use it in GitHub Desktop.
require 'stringex'
module DataMapper
module Types
class Slug < DataMapper::Type
primitive String
length 2000
# Maximum length chosen because URI type is limited to 2000
# characters, and a slug is a component of a URI, so it should
# not exceed the maximum URI length either.
def self.typecast(val, property)
if val && val.respond_to?(:to_str)
escape(val.to_str)
end
end
def self.escape(string)
string.to_url
end
end # class Slug
end # module Types
end # module DataMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment