Class: DataMapper::Types::Slug
- Inherits:
-
DataMapper::Type
- Object
- DataMapper::Type
- DataMapper::Types::Slug
- Defined in:
- lib/gems/dm-types-0.9.9/lib/dm-types/slug.rb
Constant Summary
Constants inherited from DataMapper::Type
DataMapper::Type::PROPERTY_OPTIONS, DataMapper::Type::PROPERTY_OPTION_ALIASES
Class Method Summary collapse
- .dump(value, property) ⇒ Object
-
.escape(string) ⇒ Object
Hugs and kisses to Rick Olsons permalink_fu for the escape method.
- .load(value, property) ⇒ Object
Methods inherited from DataMapper::Type
bind, configure, inherited, options, primitive
Class Method Details
.dump(value, property) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/slug.rb', line 13 def self.dump(value, property) if value.nil? nil elsif value.is_a?(String) #Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv(value.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase).to_s escape(value) else raise ArgumentError.new("+value+ must be nil or a String") end end |
.escape(string) ⇒ Object
Hugs and kisses to Rick Olsons permalink_fu for the escape method
25 26 27 28 29 30 31 32 33 |
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/slug.rb', line 25 def self.escape(string) result = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars. result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row. result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator. result.downcase! result end |
.load(value, property) ⇒ Object
9 10 11 |
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/slug.rb', line 9 def self.load(value, property) value end |