dm-types 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/Manifest.txt ADDED
@@ -0,0 +1,39 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ lib/dm-types.rb
8
+ lib/dm-types/bcrypt_hash.rb
9
+ lib/dm-types/csv.rb
10
+ lib/dm-types/enum.rb
11
+ lib/dm-types/epoch_time.rb
12
+ lib/dm-types/file_path.rb
13
+ lib/dm-types/flag.rb
14
+ lib/dm-types/ip_address.rb
15
+ lib/dm-types/json.rb
16
+ lib/dm-types/serial.rb
17
+ lib/dm-types/uri.rb
18
+ lib/dm-types/version.rb
19
+ lib/dm-types/yaml.rb
20
+ spec/integration/bcrypt_hash_spec.rb
21
+ spec/integration/enum_spec.rb
22
+ spec/integration/file_path_spec.rb
23
+ spec/integration/flag_spec.rb
24
+ spec/integration/ip_address_spec.rb
25
+ spec/integration/json_spec.rb
26
+ spec/integration/uri_spec.rb
27
+ spec/integration/yaml_spec.rb
28
+ spec/spec.opts
29
+ spec/spec_helper.rb
30
+ spec/unit/bcrypt_hash_spec.rb
31
+ spec/unit/csv_spec.rb
32
+ spec/unit/enum_spec.rb
33
+ spec/unit/epoch_time_spec.rb
34
+ spec/unit/file_path_spec.rb
35
+ spec/unit/flag_spec.rb
36
+ spec/unit/ip_address_spec.rb
37
+ spec/unit/json_spec.rb
38
+ spec/unit/uri_spec.rb
39
+ spec/unit/yaml_spec.rb
@@ -1,4 +1,3 @@
1
- dm-types
2
- ========
1
+ = dm-types
3
2
 
4
3
  DataMapper plugin providing many extra types for use in data models.
data/Rakefile CHANGED
@@ -1,51 +1,44 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
- require 'rake/clean'
4
- require 'rake/gempackagetask'
5
3
  require 'spec/rake/spectask'
6
4
  require 'pathname'
7
5
 
8
- CLEAN.include '{log,pkg}/'
6
+ ROOT = Pathname(__FILE__).dirname.expand_path
7
+ require ROOT + 'lib/dm-types/version'
9
8
 
10
- spec = Gem::Specification.new do |s|
11
- s.name = 'dm-types'
12
- s.version = '0.9.2'
13
- s.platform = Gem::Platform::RUBY
14
- s.has_rdoc = true
15
- s.extra_rdoc_files = %w[ README LICENSE TODO ]
16
- s.summary = 'DataMapper plugin providing extra data types'
17
- s.description = s.summary
18
- s.author = 'Sam Smoot'
19
- s.email = 'ssmoot@gmail.com'
20
- s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-types'
21
- s.require_path = 'lib'
22
- s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
23
- s.add_dependency('dm-core', "=#{s.version}")
24
- end
9
+ AUTHOR = "Sam Smoot"
10
+ EMAIL = "ssmoot@gmail.com"
11
+ GEM_NAME = "dm-types"
12
+ GEM_VERSION = DataMapper::Types::VERSION
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION]]
14
+ GEM_CLEAN = ["log", "pkg", "coverage"]
15
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
+
17
+ PROJECT_NAME = "datamapper"
18
+ PROJECT_URL = "http://github.com/sam/dm-more/tree/master/dm-types"
19
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin providing extra data types"
20
+
21
+ require ROOT.parent + 'tasks/hoe'
25
22
 
26
23
  task :default => [ :spec ]
27
24
 
28
25
  WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
26
  SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
27
 
31
- Rake::GemPackageTask.new(spec) do |pkg|
32
- pkg.gem_spec = spec
33
- end
34
-
35
- desc "Install #{spec.name} #{spec.version} (default ruby)"
28
+ desc "Install #{GEM_NAME} #{GEM_VERSION} (default ruby)"
36
29
  task :install => [ :package ] do
37
- sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
30
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
38
31
  end
39
32
 
40
- desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
33
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
41
34
  task :uninstall => [ :clobber ] do
42
- sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
35
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
43
36
  end
44
37
 
45
38
  namespace :jruby do
46
- desc "Install #{spec.name} #{spec.version} with JRuby"
39
+ desc "Install #{GEM_NAME} #{GEM_VERSION} with JRuby"
47
40
  task :install => [ :package ] do
48
- sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
41
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}, :verbose => false
49
42
  end
50
43
  end
51
44
 
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.2'
4
+ gem 'dm-core', '=0.9.3'
5
5
  require 'dm-core'
6
6
 
7
7
  dir = Pathname(__FILE__).dirname.expand_path / 'dm-types'
@@ -16,3 +16,10 @@ require dir / "json"""
16
16
  require dir / 'uri'
17
17
  require dir / 'yaml'
18
18
  require dir / 'serial'
19
+
20
+ # this looks a little ugly, but everyone who uses dm-types shouldn't have to have ruby-bcrypt installed
21
+ module DataMapper
22
+ module Types
23
+ autoload(:BCryptHash, File.join(Pathname(__FILE__).dirname.expand_path, 'dm-types', 'bcrypt_hash'))
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+
2
+ require 'bcrypt'
3
+
4
+ module DataMapper
5
+ module Types
6
+ class BCryptHash < DataMapper::Type
7
+ primitive String
8
+ size 60
9
+
10
+ def self.load(value, property)
11
+ if value.nil?
12
+ nil
13
+ elsif value.is_a?(String)
14
+ BCrypt::Password.new(value)
15
+ else
16
+ raise ArgumentError.new("+value+ must be nil or a String")
17
+ end
18
+ end
19
+
20
+ def self.dump(value, property)
21
+ if value.nil?
22
+ nil
23
+ elsif value.is_a?(String)
24
+ BCrypt::Password.create(value, :cost => BCrypt::Engine::DEFAULT_COST)
25
+ else
26
+ raise ArgumentError.new("+value+ must be nil or a String")
27
+ end
28
+ end
29
+ end # class BCryptHash
30
+ end # module Types
31
+ end # module DataMapper
data/lib/dm-types/enum.rb CHANGED
@@ -35,6 +35,17 @@ module DataMapper
35
35
  def self.dump(value, property)
36
36
  self.flag_map.invert[value]
37
37
  end
38
+
39
+ def self.typecast(value, property)
40
+ # Attempt to typecast using the class of the first item in the map.
41
+ return value if value.nil?
42
+ case self.flag_map[1]
43
+ when Symbol then value.to_sym
44
+ when String then value.to_s
45
+ when Fixnum then value.to_i
46
+ else value
47
+ end
48
+ end
38
49
  end # class Enum
39
50
  end # module Types
40
51
  end # module DataMapper
@@ -17,6 +17,11 @@ module DataMapper
17
17
  return nil if value.nil?
18
18
  value.to_s
19
19
  end
20
+
21
+ def self.typecast(value, property)
22
+ # Leave alone if a Pathname is given.
23
+ value.kind_of?(Pathname) ? value : load(value, property)
24
+ end
20
25
  end # class FilePath
21
26
  end # module Types
22
27
  end # module DataMapper
data/lib/dm-types/flag.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  module DataMapper
2
2
  module Types
3
3
  class Flag < DataMapper::Type(Integer)
4
+ def self.inherited(target)
5
+ target.instance_variable_set("@primitive", self.primitive)
6
+ end
4
7
 
5
8
  def self.flag_map
6
9
  @flag_map
@@ -11,7 +14,7 @@ module DataMapper
11
14
  end
12
15
 
13
16
  def self.new(*flags)
14
- type = Flag.clone # cannot dup a Class
17
+ type = Class.new(Flag)
15
18
  type.flag_map = {}
16
19
 
17
20
  flags.each_with_index do |flag, i|
@@ -46,6 +49,13 @@ module DataMapper
46
49
  flags.map!{ |f| f.to_sym }
47
50
  flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
48
51
  end
52
+
53
+ def self.typecast(value, property)
54
+ case value
55
+ when Array then value.map {|v| v.to_sym}
56
+ else value.to_sym
57
+ end
58
+ end
49
59
  end # class Flag
50
60
  end # module Types
51
61
  end # module DataMapper
@@ -21,6 +21,10 @@ module DataMapper
21
21
  return nil if value.nil?
22
22
  value.to_s
23
23
  end
24
+
25
+ def self.typecast(value, property)
26
+ value.kind_of?(IPAddr) ? value : load(value, property)
27
+ end
24
28
  end # class IPAddress
25
29
  end # module Types
26
30
  end # module DataMapper
data/lib/dm-types/json.rb CHANGED
@@ -26,6 +26,15 @@ module DataMapper
26
26
  ::JSON.dump(value)
27
27
  end
28
28
  end
29
+
30
+ def self.typecast(value, property)
31
+ # Arrays and hashes are left alone, while strings are parsed as JSON.
32
+ if value.kind_of?(Array) || value.kind_of?(Hash)
33
+ value
34
+ else
35
+ ::JSON.load(value.to_s)
36
+ end
37
+ end
29
38
  end # class Json
30
39
  end # module Types
31
40
  end # module DataMapper
data/lib/dm-types/uri.rb CHANGED
@@ -14,6 +14,10 @@ module DataMapper
14
14
  return nil if value.nil?
15
15
  value.to_s
16
16
  end
17
+
18
+ def self.typecast(value, property)
19
+ value.kind_of?(Addressable::URI) ? value : load(value.to_s, property)
20
+ end
17
21
  end # class URI
18
22
  end # module Types
19
23
  end # module DataMapper
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module Types
3
+ VERSION = "0.9.3"
4
+ end
5
+ end
data/lib/dm-types/yaml.rb CHANGED
@@ -26,6 +26,11 @@ module DataMapper
26
26
  ::YAML.dump(value)
27
27
  end
28
28
  end
29
+
30
+ def self.typecast(value, property)
31
+ # No typecasting; leave values exactly as they're provided.
32
+ value
33
+ end
29
34
  end # class Yaml
30
35
  end # module Types
31
36
  end # module DataMapper
@@ -0,0 +1,50 @@
1
+
2
+ require 'pathname'
3
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
4
+
5
+ begin
6
+ require 'bcrypt'
7
+ rescue LoadError
8
+ skip_tests = true
9
+ end
10
+
11
+ describe "DataMapper::Types::BCryptHash" do
12
+ unless skip_tests
13
+ describe "with no options" do
14
+ before(:each) do
15
+ class User
16
+ include DataMapper::Resource
17
+
18
+ property :id, Serial
19
+ property :password, BCryptHash
20
+ end
21
+ User.auto_migrate!
22
+ User.create!(:password => "DataMapper R0cks!")
23
+ end
24
+
25
+ it "should save a password to the DB on creation" do
26
+ repository(:default) do
27
+ User.create!(:password => "password1")
28
+ end
29
+ user = User.all
30
+ user[0].password.should == "DataMapper R0cks!"
31
+ user[1].password.should == "password1"
32
+ end
33
+
34
+ it "should change the password on attribute update" do
35
+ @user = User.first
36
+ @user.attribute_set(:password, "D@t@Mapper R0cks!")
37
+ @user.save
38
+ @user.password.should_not == "DataMapper R0cks!"
39
+ @user.password.should == "D@t@Mapper R0cks!"
40
+ end
41
+
42
+ it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
43
+ @user = User.first
44
+ @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
45
+ end
46
+ end
47
+ else
48
+ it "Needs the bcrypt-ruby gem installed"
49
+ end
50
+ end
@@ -17,8 +17,13 @@ describe DataMapper::Types::Enum do
17
17
  Bug.create!(:status => :crit)
18
18
  Bug.create!(:status => :warn)
19
19
  end
20
+
20
21
  bugs = Bug.all
21
22
  bugs[0].status.should == :crit
22
23
  bugs[1].status.should == :warn
23
24
  end
25
+
26
+ it 'should immediately typecast supplied values' do
27
+ Bug.new(:status => :crit).status.should == :crit
28
+ end
24
29
  end
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::FilePath do
5
+ before(:all) do
6
+ class FilePathTest
7
+ include DataMapper::Resource
8
+
9
+ property :id, Integer, :serial => true
10
+ property :path, FilePath
11
+ end
12
+ FilePathTest.auto_migrate!
13
+ end
14
+
15
+ it "should work" do
16
+ repository(:default) do
17
+ FilePathTest.create!(:path => '/usr')
18
+ end
19
+
20
+ FilePathTest.first.path.should == Pathname.new('/usr')
21
+ end
22
+
23
+ it 'should immediately typecast supplied values' do
24
+ FilePathTest.new(:path => '/usr').path.should == Pathname.new('/usr')
25
+ end
26
+ end
@@ -26,4 +26,8 @@ describe DataMapper::Types::Flag do
26
26
  Shirt.get(2).sizes.should == [:xs, :small]
27
27
  end
28
28
  end
29
+
30
+ it 'should immediately typecast supplied values' do
31
+ Shirt.new(:sizes => [:large, :xl]).sizes.should == [:large, :xl]
32
+ end
29
33
  end
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::IPAddress do
5
+ before(:all) do
6
+ class IPAddressTest
7
+ include DataMapper::Resource
8
+
9
+ property :id, Serial
10
+ property :ip, IPAddress
11
+ end
12
+ IPAddressTest.auto_migrate!
13
+ end
14
+
15
+ it "should work" do
16
+ repository(:default) do
17
+ IPAddressTest.create(:ip => '127.0.0.1')
18
+ end
19
+
20
+ IPAddressTest.first.ip.should == IPAddr.new('127.0.0.1')
21
+ end
22
+
23
+ it 'should immediately typecast supplied values' do
24
+ IPAddressTest.new(:ip => '10.0.0.1').ip.should == IPAddr.new('10.0.0.1')
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Json do
5
+ before(:all) do
6
+ class JsonTest
7
+ include DataMapper::Resource
8
+
9
+ property :id, Serial
10
+ property :json, Json
11
+ end
12
+ JsonTest.auto_migrate!
13
+ end
14
+
15
+ it "should work" do
16
+ repository(:default) do
17
+ JsonTest.create(:json => '[1, 2, 3]')
18
+ end
19
+
20
+ JsonTest.first.json.should == [1, 2, 3]
21
+ end
22
+
23
+ it 'should immediately typecast supplied values' do
24
+ JsonTest.new(:json => '[1, 2, 3]').json.should == [1, 2, 3]
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::URI do
5
+ before(:all) do
6
+ class URITest
7
+ include DataMapper::Resource
8
+
9
+ property :id, Integer, :serial => true
10
+ property :uri, DM::URI
11
+ end
12
+ URITest.auto_migrate!
13
+ end
14
+
15
+ it "should work" do
16
+ repository(:default) do
17
+ URITest.create!(:uri => 'http://localhost')
18
+ end
19
+
20
+ URITest.first.uri.should == Addressable::URI.parse('http://localhost')
21
+ end
22
+
23
+ it 'should immediately typecast supplied values' do
24
+ URITest.new(:uri => 'http://localhost').uri.should == Addressable::URI.parse('http://localhost')
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Enum do
5
+ before(:all) do
6
+ class YamlTest
7
+ include DataMapper::Resource
8
+
9
+ property :id, Serial
10
+ property :yaml, Yaml
11
+ end
12
+ YamlTest.auto_migrate!
13
+
14
+ class SerializeMe
15
+ attr_accessor :name
16
+ end
17
+ end
18
+
19
+ it "should work" do
20
+ obj = SerializeMe.new
21
+ obj.name = 'Hello!'
22
+
23
+ repository(:default) do
24
+ YamlTest.create(:yaml => [1, 2, 3])
25
+ YamlTest.create(:yaml => obj)
26
+ end
27
+
28
+ tests = YamlTest.all
29
+ tests.first.yaml.should == [1, 2, 3]
30
+ tests.last.yaml.should be_kind_of(SerializeMe)
31
+ tests.last.yaml.name.should == 'Hello!'
32
+ end
33
+
34
+ it 'should immediately typecast supplied values' do
35
+ YamlTest.new(:yaml => [1, 2, 3]).yaml.should == [1, 2, 3]
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ include DataMapper::Types
5
+
6
+ begin
7
+ require 'bcrypt'
8
+ rescue LoadError
9
+ skip_tests = true
10
+ end
11
+
12
+ describe "DataMapper::Types::BCryptHash" do
13
+ unless skip_tests
14
+
15
+ before(:each) do
16
+ @clear_password = "DataMapper R0cks!"
17
+ @crypted_password = BCrypt::Password.create(@clear_password)
18
+ end
19
+
20
+ describe ".dump" do
21
+ it "should return a crypted hash as a BCrypt::Password" do
22
+ BCryptHash.dump(@clear_password, :property).should be_an_instance_of(BCrypt::Password)
23
+ end
24
+
25
+ it "should return a string that is 60 characters long" do
26
+ BCryptHash.dump(@clear_password, :property).should have(60).characters
27
+ end
28
+
29
+ it "should return nil if nil is passed" do
30
+ BCryptHash.dump(nil, :property).should be_nil
31
+ end
32
+ end
33
+
34
+ describe ".load" do
35
+ it "should return the password as a BCrypt::Password" do
36
+ BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
37
+ end
38
+
39
+ it "should return the password as a password which matches" do
40
+ BCryptHash.load(@crypted_password, :property).should == @clear_password
41
+ end
42
+
43
+ it "should return nil if given nil" do
44
+ FilePath.load(nil, :property).should be_nil
45
+ end
46
+ end
47
+ else
48
+ it "requires the bcrypt-ruby gem to test"
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ require 'fastercsv'
5
+
6
+ describe DataMapper::Types::Csv, ".load" do
7
+ it 'should parse the value if a string is provided' do
8
+ FasterCSV.should_receive(:parse).with('csv_string').once
9
+ DataMapper::Types::Csv.load('csv_string', :property)
10
+ end
11
+
12
+ it 'should do nothing if the value is a string' do
13
+ FasterCSV.should_not_receive(:parse)
14
+ DataMapper::Types::Csv.load([], :property).should == []
15
+ end
16
+
17
+ it 'should return nil otherwise' do
18
+ DataMapper::Types::Csv.load({}, :property).should == nil
19
+ DataMapper::Types::Csv.load(nil, :property).should == nil
20
+ end
21
+ end
22
+
23
+ describe DataMapper::Types::Csv, ".dump" do
24
+ it 'should dump to CSV' do
25
+ DataMapper::Types::Csv.dump([[1, 2, 3]], :property).should == "1,2,3\n"
26
+ end
27
+
28
+ it 'should do nothing if the value is a string' do
29
+ FasterCSV.should_not_receive(:generate)
30
+ DataMapper::Types::Csv.dump('string', :property).should == 'string'
31
+ end
32
+
33
+ it 'should return nil otherwise' do
34
+ DataMapper::Types::Csv.dump({}, :property).should == nil
35
+ DataMapper::Types::Csv.dump(nil, :property).should == nil
36
+ end
37
+ end
@@ -63,4 +63,34 @@ describe DataMapper::Types::Enum do
63
63
  @enum.load(-1, :property).should be_nil
64
64
  end
65
65
  end
66
+
67
+ describe ".typecast" do
68
+ it 'should attempt to use the Enum type' do
69
+ # Symbol.
70
+ @sym_enum = DataMapper::Types::Enum[:uno]
71
+ @sym_enum.typecast(:uno, :property).should == :uno
72
+ @sym_enum.typecast("uno", :property).should == :uno
73
+
74
+ # String
75
+ @str_enum = DataMapper::Types::Enum["uno"]
76
+ @str_enum.typecast(:uno, :property).should == "uno"
77
+ @str_enum.typecast("uno", :property).should == "uno"
78
+
79
+ # Integer
80
+ @int_enum = DataMapper::Types::Enum[1, 2, 3]
81
+ @int_enum.typecast(1, :property).should == 1
82
+ @int_enum.typecast(1.1, :property).should == 1
83
+ end
84
+
85
+ it "should not throw an error when value is nil" do
86
+ @sym_enum = DataMapper::Types::Enum[:uno]
87
+ @sym_enum.typecast( nil, :property).should == nil
88
+
89
+ @str_enum = DataMapper::Types::Enum["uno"]
90
+ @str_enum.typecast( nil, :property).should == nil
91
+
92
+ @int_enum = DataMapper::Types::Enum[1, 2, 3]
93
+ @int_enum.typecast( nil, :property ).should == nil
94
+ end
95
+ end
66
96
  end
@@ -35,4 +35,15 @@ describe DataMapper::Types::FilePath do
35
35
  DataMapper::Types::FilePath.load("", :property).should == Pathname.new("")
36
36
  end
37
37
  end
38
+
39
+ describe '.typecast' do
40
+ it 'should do nothing if a Pathname is provided' do
41
+ DataMapper::Types::FilePath.typecast(@path, :property).should == @path
42
+ end
43
+
44
+ it 'should defer to .load if a string is provided' do
45
+ DataMapper::Types::FilePath.should_receive(:load).with(@path_str, :property)
46
+ DataMapper::Types::FilePath.typecast(@path_str, :property)
47
+ end
48
+ end
38
49
  end
@@ -64,23 +64,23 @@ describe DataMapper::Types::Flag do
64
64
  end
65
65
 
66
66
  it "should return the value of the key match from the flag map" do
67
- @flag.load(1, :property).should == [:uno]
68
- @flag.load(2, :property).should == [:dos]
69
- @flag.load(4, :property).should == [:tres]
70
- @flag.load(8, :property).should == [:cuatro]
67
+ @flag.load(1, :property).should == [:uno]
68
+ @flag.load(2, :property).should == [:dos]
69
+ @flag.load(4, :property).should == [:tres]
70
+ @flag.load(8, :property).should == [:cuatro]
71
71
  @flag.load(16, :property).should == [:cinco]
72
72
  end
73
73
 
74
74
  it "should return an array of all flags matches" do
75
- @flag.load(3, :property).should include(:uno, :dos)
75
+ @flag.load(3, :property).should include(:uno, :dos)
76
76
  @flag.load(10, :property).should include(:dos, :cuatro)
77
77
  @flag.load(31, :property).should include(:uno, :dos, :tres, :cuatro, :cinco)
78
78
  end
79
79
 
80
80
  it "should return an empty array if there is no key" do
81
- @flag.load(-1, :property).should == []
81
+ @flag.load(-1, :property).should == []
82
82
  @flag.load(nil, :property).should == []
83
- @flag.load(32, :property).should == []
83
+ @flag.load(32, :property).should == []
84
84
  end
85
85
  end
86
86
  end
@@ -34,6 +34,23 @@ describe DataMapper::Types::IPAddress do
34
34
  it "should return an empty IP address if given an empty string" do
35
35
  DataMapper::Types::IPAddress.load("", :property).should == IPAddr.new("0.0.0.0")
36
36
  end
37
+
38
+ it 'should raise an ArgumentError if given something else' do
39
+ lambda {
40
+ DataMapper::Types::IPAddress.load([], :property)
41
+ }.should raise_error(ArgumentError, '+value+ must be nil or a String')
42
+ end
43
+ end
44
+
45
+ describe '.typecast' do
46
+ it 'should do nothing if an IpAddr is provided' do
47
+ DataMapper::Types::IPAddress.typecast(@ip, :property).should == @ip
48
+ end
49
+
50
+ it 'should defer to .load if a string is provided' do
51
+ DataMapper::Types::IPAddress.should_receive(:load).with(@ip_str, :property)
52
+ DataMapper::Types::IPAddress.typecast(@ip_str, :property)
53
+ end
37
54
  end
38
55
 
39
56
  end
@@ -0,0 +1,53 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Json, ".load" do
5
+ it 'should return nil if nil is provided' do
6
+ DataMapper::Types::Json.load(nil, :property).should be_nil
7
+ end
8
+
9
+ it 'should parse the value if a string is provided' do
10
+ JSON.should_receive(:load).with('json_string').once
11
+ DataMapper::Types::Json.load('json_string', :property)
12
+ end
13
+
14
+ it 'should raise an ArgumentError if something else is given' do
15
+ lambda {
16
+ DataMapper::Types::Json.load(:sym, :property)
17
+ }.should raise_error(ArgumentError, '+value+ must be nil or a String')
18
+ end
19
+ end
20
+
21
+ describe DataMapper::Types::Json, ".dump" do
22
+ it 'should return nil if the value is nil' do
23
+ DataMapper::Types::Json.dump(nil, :property).should be_nil
24
+ end
25
+
26
+ it 'should do nothing if the value is a string' do
27
+ JSON.should_not_receive(:dump)
28
+ DataMapper::Types::Json.dump('', :property).should be_kind_of(String)
29
+ end
30
+
31
+ it 'should dump to a JSON string otherwise' do
32
+ JSON.should_receive(:dump).with([]).once
33
+ DataMapper::Types::Json.dump([], :property)
34
+ end
35
+ end
36
+
37
+ describe DataMapper::Types::Json, ".typecast" do
38
+ it 'should parse the value if a string is provided' do
39
+ JSON.should_receive(:load).with('json_string')
40
+
41
+ DataMapper::Types::Json.typecast('json_string', :property)
42
+ end
43
+
44
+ it 'should leave the value alone if an array is given' do
45
+ JSON.should_not_receive(:load)
46
+ DataMapper::Types::Json.typecast([], :property)
47
+ end
48
+
49
+ it 'should leave the value alone if a hash is given' do
50
+ JSON.should_not_receive(:load)
51
+ DataMapper::Types::Json.typecast({}, :property)
52
+ end
53
+ end
@@ -35,4 +35,15 @@ describe DataMapper::Types::URI do
35
35
  DataMapper::Types::URI.load("", :property).should == Addressable::URI.parse("")
36
36
  end
37
37
  end
38
+
39
+ describe '.typecast' do
40
+ it 'should do nothing if an Addressable::URI is provided' do
41
+ DataMapper::Types::URI.typecast(@uri, :property).should == @uri
42
+ end
43
+
44
+ it 'should defer to .load if a string is provided' do
45
+ DataMapper::Types::URI.should_receive(:load).with(@uri_str, :property)
46
+ DataMapper::Types::URI.typecast(@uri_str, :property)
47
+ end
48
+ end
38
49
  end
@@ -0,0 +1,58 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Yaml, ".load" do
5
+ it 'should return nil if nil is provided' do
6
+ DataMapper::Types::Yaml.load(nil, :property).should be_nil
7
+ end
8
+
9
+ it 'should parse the value if a string is provided' do
10
+ YAML.should_receive(:load).with('yaml_string').once
11
+ DataMapper::Types::Yaml.load('yaml_string', :property)
12
+ end
13
+
14
+ it 'should raise an ArgumentError if something else is given' do
15
+ lambda {
16
+ DataMapper::Types::Yaml.load(:sym, :property)
17
+ }.should raise_error(ArgumentError, '+value+ must be nil or a String')
18
+ end
19
+ end
20
+
21
+ describe DataMapper::Types::Yaml, ".dump" do
22
+ it 'should return nil if the value is nil' do
23
+ DataMapper::Types::Yaml.dump(nil, :property).should be_nil
24
+ end
25
+
26
+ it 'should do nothing if the value is a string which begins with ---' do
27
+ YAML.should_not_receive(:dump)
28
+ DataMapper::Types::Yaml.dump('--- str', :property).should be_kind_of(String)
29
+ end
30
+
31
+ it 'should dump to a YAML string if the value is a normal string' do
32
+ YAML.should_receive(:dump).with('string').once
33
+ DataMapper::Types::Yaml.dump('string', :property)
34
+ end
35
+
36
+ it 'should dump to a YAML string otherwise' do
37
+ YAML.should_receive(:dump).with([]).once
38
+ DataMapper::Types::Yaml.dump([], :property)
39
+ end
40
+ end
41
+
42
+ describe DataMapper::Types::Yaml, ".typecast" do
43
+ it 'should leave the value alone' do
44
+ @type = DataMapper::Types::Yaml
45
+ @type.typecast([1, 2, 3], :property).should == [1, 2, 3]
46
+
47
+ class SerializeMe
48
+ attr_accessor :name
49
+ end
50
+
51
+ obj = SerializeMe.new
52
+ obj.name = 'Hello!'
53
+
54
+ casted = @type.typecast(obj, :property)
55
+ casted.should be_kind_of(SerializeMe)
56
+ casted.name.should == 'Hello!'
57
+ end
58
+ 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.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Smoot
@@ -9,29 +9,49 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-25 00:00:00 -05:00
12
+ date: 2008-07-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dm-core
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
20
21
  - - "="
21
22
  - !ruby/object:Gem::Version
22
- version: 0.9.2
23
+ version: 0.9.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.0
23
34
  version:
24
35
  description: DataMapper plugin providing extra data types
25
- email: ssmoot@gmail.com
36
+ email:
37
+ - ssmoot@gmail.com
26
38
  executables: []
27
39
 
28
40
  extensions: []
29
41
 
30
42
  extra_rdoc_files:
31
- - README
43
+ - README.txt
32
44
  - LICENSE
33
45
  - TODO
34
46
  files:
47
+ - History.txt
48
+ - LICENSE
49
+ - Manifest.txt
50
+ - README.txt
51
+ - Rakefile
52
+ - TODO
53
+ - lib/dm-types.rb
54
+ - lib/dm-types/bcrypt_hash.rb
35
55
  - lib/dm-types/csv.rb
36
56
  - lib/dm-types/enum.rb
37
57
  - lib/dm-types/epoch_time.rb
@@ -41,27 +61,34 @@ files:
41
61
  - lib/dm-types/json.rb
42
62
  - lib/dm-types/serial.rb
43
63
  - lib/dm-types/uri.rb
64
+ - lib/dm-types/version.rb
44
65
  - lib/dm-types/yaml.rb
45
- - lib/dm-types.rb
66
+ - spec/integration/bcrypt_hash_spec.rb
46
67
  - spec/integration/enum_spec.rb
68
+ - spec/integration/file_path_spec.rb
47
69
  - spec/integration/flag_spec.rb
70
+ - spec/integration/ip_address_spec.rb
71
+ - spec/integration/json_spec.rb
72
+ - spec/integration/uri_spec.rb
73
+ - spec/integration/yaml_spec.rb
74
+ - spec/spec.opts
48
75
  - spec/spec_helper.rb
76
+ - spec/unit/bcrypt_hash_spec.rb
77
+ - spec/unit/csv_spec.rb
49
78
  - spec/unit/enum_spec.rb
50
79
  - spec/unit/epoch_time_spec.rb
51
80
  - spec/unit/file_path_spec.rb
52
81
  - spec/unit/flag_spec.rb
53
82
  - spec/unit/ip_address_spec.rb
83
+ - spec/unit/json_spec.rb
54
84
  - spec/unit/uri_spec.rb
55
- - spec/spec.opts
56
- - Rakefile
57
- - README
58
- - LICENSE
59
- - TODO
85
+ - spec/unit/yaml_spec.rb
60
86
  has_rdoc: true
61
87
  homepage: http://github.com/sam/dm-more/tree/master/dm-types
62
88
  post_install_message:
63
- rdoc_options: []
64
-
89
+ rdoc_options:
90
+ - --main
91
+ - README.txt
65
92
  require_paths:
66
93
  - lib
67
94
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -78,8 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
105
  version:
79
106
  requirements: []
80
107
 
81
- rubyforge_project:
82
- rubygems_version: 1.0.1
108
+ rubyforge_project: datamapper
109
+ rubygems_version: 1.2.0
83
110
  signing_key:
84
111
  specification_version: 2
85
112
  summary: DataMapper plugin providing extra data types