Created
March 6, 2010 11:07
-
-
Save hassox/323633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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