Skip to content

Commit

Permalink
Merge branch 'revans'
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed Sep 22, 2008
2 parents 056b873 + b7cea31 commit 7ef6827
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions dm-types/lib/dm-types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require dir / 'yaml'
require dir / 'serial'
require dir / 'regexp'
require dir / 'permalink'

# this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
module DataMapper
Expand Down
26 changes: 26 additions & 0 deletions dm-types/lib/dm-types/permalink.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'iconv'

module DataMapper
module Types
class Permalink < DataMapper::Type
primitive String
size 65535

def self.load(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)
else
raise ArgumentError.new("+value+ must be nil or a String")
end
end

def self.dump(value, property)
return nil if value.nil?
value.to_s
end

end # class Permalink
end # module Types
end # module DataMapper
31 changes: 31 additions & 0 deletions dm-types/spec/integration/permalink_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'pathname'
require 'iconv'
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'

describe DataMapper::Types::Permalink do

before(:all) do
class PermalinkTest
include DataMapper::Resource

property :id, Serial
property :name, Permalink

end
PermalinkTest.auto_migrate!
end

it "should create the permalink" do
repository(:default) do
PermalinkTest.create(:name => 'New DataMapper Type')
end

PermalinkTest.first.name.should == create_permalink("New DataMapper Type")
end


def create_permalink(word)
Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv(word.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase)
end

end

0 comments on commit 7ef6827

Please sign in to comment.