dm-types 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -13,8 +13,11 @@ lib/dm-types/file_path.rb
13
13
  lib/dm-types/flag.rb
14
14
  lib/dm-types/ip_address.rb
15
15
  lib/dm-types/json.rb
16
+ lib/dm-types/slug.rb
17
+ lib/dm-types/regexp.rb
16
18
  lib/dm-types/serial.rb
17
19
  lib/dm-types/uri.rb
20
+ lib/dm-types/uuid.rb
18
21
  lib/dm-types/version.rb
19
22
  lib/dm-types/yaml.rb
20
23
  spec/integration/bcrypt_hash_spec.rb
@@ -23,6 +26,7 @@ spec/integration/file_path_spec.rb
23
26
  spec/integration/flag_spec.rb
24
27
  spec/integration/ip_address_spec.rb
25
28
  spec/integration/json_spec.rb
29
+ spec/integration/slug_spec.rb
26
30
  spec/integration/uri_spec.rb
27
31
  spec/integration/yaml_spec.rb
28
32
  spec/spec.opts
@@ -35,5 +39,6 @@ spec/unit/file_path_spec.rb
35
39
  spec/unit/flag_spec.rb
36
40
  spec/unit/ip_address_spec.rb
37
41
  spec/unit/json_spec.rb
42
+ spec/unit/regexp_spec.rb
38
43
  spec/unit/uri_spec.rb
39
44
  spec/unit/yaml_spec.rb
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ AUTHOR = "Sam Smoot"
10
10
  EMAIL = "ssmoot@gmail.com"
11
11
  GEM_NAME = "dm-types"
12
12
  GEM_VERSION = DataMapper::Types::VERSION
13
- GEM_DEPENDENCIES = [["dm-core", GEM_VERSION]]
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION], "uuidtools"]
14
14
  GEM_CLEAN = ["log", "pkg", "coverage"]
15
15
  GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
16
 
@@ -45,7 +45,7 @@ end
45
45
  desc 'Run specifications'
46
46
  Spec::Rake::SpecTask.new(:spec) do |t|
47
47
  t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
48
- t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
48
+ t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
49
49
 
50
50
  begin
51
51
  t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
data/lib/dm-types.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'pathname'
3
3
 
4
- gem 'dm-core', '=0.9.5'
4
+ gem 'dm-core', '=0.9.6'
5
5
  require 'dm-core'
6
6
 
7
7
  dir = Pathname(__FILE__).dirname.expand_path / 'dm-types'
@@ -14,8 +14,11 @@ require dir / 'flag'
14
14
  require dir / 'ip_address'
15
15
  require dir / "json"""
16
16
  require dir / 'uri'
17
+ require dir / 'uuid'
17
18
  require dir / 'yaml'
18
19
  require dir / 'serial'
20
+ require dir / 'regexp'
21
+ require dir / 'slug'
19
22
 
20
23
  # this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
21
24
  module DataMapper
@@ -0,0 +1,20 @@
1
+ module DataMapper
2
+ module Types
3
+ class Regexp < DataMapper::Type
4
+ primitive String
5
+
6
+ def self.load(value, property)
7
+ ::Regexp.new(value) unless value.nil?
8
+ end
9
+
10
+ def self.dump(value, property)
11
+ return nil if value.nil?
12
+ value.source
13
+ end
14
+
15
+ def self.typecast(value, property)
16
+ value.kind_of?(::Regexp) ? value : load(value, property)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ require 'iconv'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Slug < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+
9
+ def self.load(value, property)
10
+ value
11
+ end
12
+
13
+ def self.dump(value, property)
14
+ if value.nil?
15
+ nil
16
+ elsif value.is_a?(String)
17
+ #Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv(value.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase).to_s
18
+ escape(value)
19
+ else
20
+ raise ArgumentError.new("+value+ must be nil or a String")
21
+ end
22
+ end
23
+
24
+ # Hugs and kisses to Rick Olsons permalink_fu for the escape method
25
+ def self.escape(string)
26
+ result = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s
27
+ result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
28
+ result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
29
+ result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
30
+ result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
31
+ result.downcase!
32
+ result
33
+ end
34
+
35
+ end # class Slug
36
+ end # module Types
37
+ end # module DataMapper
data/lib/dm-types/uri.rb CHANGED
@@ -16,7 +16,13 @@ module DataMapper
16
16
  end
17
17
 
18
18
  def self.typecast(value, property)
19
- value.kind_of?(Addressable::URI) ? value : load(value.to_s, property)
19
+ if value.kind_of?(Addressable::URI)
20
+ value
21
+ elsif value.nil?
22
+ load(nil, property)
23
+ else
24
+ load(value.to_s, property)
25
+ end
20
26
  end
21
27
  end # class URI
22
28
  end # module Types
@@ -0,0 +1,43 @@
1
+ require 'uuidtools'
2
+
3
+ module DataMapper
4
+ module Types
5
+ # UUID Type
6
+ # First run at this, because I need it. A few caveats:
7
+ # * Only works on postgres, using the built-in native uuid type.
8
+ # To make it work in mysql, you'll have to add a typemap entry to
9
+ # the mysql_adapter. I think. I don't have mysql handy, so I'm
10
+ # not going to try. For SQLite, this will have to inherit from the
11
+ # String primitive
12
+ # * Won't accept a random default, because of the namespace clash
13
+ # between this and the UUIDtools gem. Also can't set the default
14
+ # type to UUID() (postgres-contrib's native generator) and
15
+ # automigrate, because auto_migrate! tries to make it a string "UUID()"
16
+ # Feel free to enchance this, and delete these caveats when they're fixed.
17
+ #
18
+ # -- Rando Sept 25, 08
19
+ #
20
+ class UUID < DataMapper::Type
21
+ primitive 'UUID'
22
+
23
+ def self.load(value, property)
24
+ return nil if value.nil?
25
+ ::UUID.parse(value)
26
+ end
27
+
28
+ def self.dump(value, property)
29
+ return nil if value.nil?
30
+ value.to_s
31
+ end
32
+
33
+ def self.typecast(value, property)
34
+ value.kind_of?(::UUID) ? value : load(value, property)
35
+ end
36
+
37
+ ::DataMapper::Property::TYPES << self
38
+ if defined? DataMapper::Adapters::PostgresAdapter
39
+ DataMapper::Adapters::PostgresAdapter.type_map.map(self).to('UUID')
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Types
3
- VERSION = "0.9.5"
3
+ VERSION = "0.9.6"
4
4
  end
5
5
  end
@@ -0,0 +1,54 @@
1
+ require 'pathname'
2
+ require 'iconv'
3
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
4
+
5
+ describe DataMapper::Types::Slug do
6
+
7
+ before(:all) do
8
+ class SlugTest
9
+ include DataMapper::Resource
10
+
11
+ property :id, Serial
12
+ property :name, Slug
13
+
14
+ end
15
+ SlugTest.auto_migrate!
16
+ end
17
+
18
+ it "should create the permalink" do
19
+ repository(:default) do
20
+ SlugTest.create(:name => 'New DataMapper Type')
21
+ end
22
+
23
+ SlugTest.first.name.should == "new-datamapper-type"
24
+ end
25
+
26
+ it "should find by a slug" do
27
+ repository(:default) do
28
+ SlugTest.create(:name => "This Should Be a Slug")
29
+ end
30
+ slug = "this-should-be-a-slug"
31
+
32
+ slugged = SlugTest.first(:name => slug)
33
+ slugged.should_not be_nil
34
+ slugged.name.should == slug
35
+ end
36
+
37
+ [
38
+ ["Iñtërnâtiônàlizætiøn", "internationalizaetion" ],
39
+ ["Hello World", "hello-world"],
40
+ ["This is Dan's Blog", "this-is-dans-blog"],
41
+ ["This is My Site, and Blog", "this-is-my-site-and-blog"]
42
+ ].each do |name, slug|
43
+
44
+ it "should sluggify #{name}" do
45
+ repository(:default) do
46
+ SlugTest.create(:name => name)
47
+ end
48
+ SlugTest.first(:name => slug).should_not be_nil
49
+ end
50
+ end
51
+
52
+
53
+
54
+ end
@@ -23,4 +23,9 @@ describe DataMapper::Types::URI do
23
23
  it 'should immediately typecast supplied values' do
24
24
  URITest.new(:uri => 'http://localhost').uri.should == Addressable::URI.parse('http://localhost')
25
25
  end
26
+
27
+ it "should correctly typecast nil values" do
28
+ URITest.new(:uri => nil).uri.should == nil
29
+ end
30
+
26
31
  end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,8 @@ ENV["SQLITE3_SPEC_URI"] ||= 'sqlite3::memory:'
8
8
  ENV["MYSQL_SPEC_URI"] ||= 'mysql://localhost/dm_core_test'
9
9
  ENV["POSTGRES_SPEC_URI"] ||= 'postgres://postgres@localhost/dm_more_test'
10
10
 
11
+ # DataMapper::Logger.new(STDOUT, :debug)
12
+
11
13
  def setup_adapter(name, default_uri = nil)
12
14
  begin
13
15
  DataMapper.setup(name, ENV["#{ENV['ADAPTER'].to_s.upcase}_SPEC_URI"] || default_uri)
@@ -0,0 +1,23 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Regexp, ".load" do
5
+ it 'should make a Regexp from the value if a string is provided' do
6
+ Regexp.should_receive(:new).with('regexp_string').once
7
+ DataMapper::Types::Regexp.load('regexp_string', :property)
8
+ end
9
+
10
+ it 'should return nil otherwise' do
11
+ DataMapper::Types::Regexp.load(nil, :property).should == nil
12
+ end
13
+ end
14
+
15
+ describe DataMapper::Types::Regexp, ".dump" do
16
+ it 'should dump to a string' do
17
+ DataMapper::Types::Regexp.dump(/\d+/, :property).should == "\\d+"
18
+ end
19
+
20
+ it 'should return nil if the value is nil' do
21
+ DataMapper::Types::Regexp.dump(nil, :property).should == nil
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Smoot
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-26 00:00:00 -05:00
12
+ date: 2008-10-12 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,17 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.5
23
+ version: 0.9.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: uuidtools
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
24
34
  version:
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: hoe
@@ -59,8 +69,11 @@ files:
59
69
  - lib/dm-types/flag.rb
60
70
  - lib/dm-types/ip_address.rb
61
71
  - lib/dm-types/json.rb
72
+ - lib/dm-types/slug.rb
73
+ - lib/dm-types/regexp.rb
62
74
  - lib/dm-types/serial.rb
63
75
  - lib/dm-types/uri.rb
76
+ - lib/dm-types/uuid.rb
64
77
  - lib/dm-types/version.rb
65
78
  - lib/dm-types/yaml.rb
66
79
  - spec/integration/bcrypt_hash_spec.rb
@@ -69,6 +82,7 @@ files:
69
82
  - spec/integration/flag_spec.rb
70
83
  - spec/integration/ip_address_spec.rb
71
84
  - spec/integration/json_spec.rb
85
+ - spec/integration/slug_spec.rb
72
86
  - spec/integration/uri_spec.rb
73
87
  - spec/integration/yaml_spec.rb
74
88
  - spec/spec.opts
@@ -81,6 +95,7 @@ files:
81
95
  - spec/unit/flag_spec.rb
82
96
  - spec/unit/ip_address_spec.rb
83
97
  - spec/unit/json_spec.rb
98
+ - spec/unit/regexp_spec.rb
84
99
  - spec/unit/uri_spec.rb
85
100
  - spec/unit/yaml_spec.rb
86
101
  has_rdoc: true