dm-types 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Sam Smoot
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,4 @@
1
+ dm-types
2
+ ========
3
+
4
+ DataMapper plugin providing many extra types for use in data models.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+ require 'pathname'
7
+
8
+ CLEAN.include '{log,pkg}/'
9
+
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
25
+
26
+ task :default => [ :spec ]
27
+
28
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
+
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc "Install #{spec.name} #{spec.version} (default ruby)"
36
+ task :install => [ :package ] do
37
+ sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
38
+ end
39
+
40
+ desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
41
+ task :uninstall => [ :clobber ] do
42
+ sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
43
+ end
44
+
45
+ namespace :jruby do
46
+ desc "Install #{spec.name} #{spec.version} with JRuby"
47
+ task :install => [ :package ] do
48
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
49
+ end
50
+ end
51
+
52
+ desc 'Run specifications'
53
+ Spec::Rake::SpecTask.new(:spec) do |t|
54
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
55
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
56
+
57
+ begin
58
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
59
+ t.rcov_opts << '--exclude' << 'spec'
60
+ t.rcov_opts << '--text-summary'
61
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
62
+ rescue Exception
63
+ # rcov not installed
64
+ end
65
+ end
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ TODO
2
+ ====
3
+
4
+
5
+
6
+ ---
7
+ TODO tickets may also be found in the DataMapper Issue Tracker:
8
+ http://wm.lighthouseapp.com/projects/4819-datamapper/overview
data/lib/dm-types.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ gem 'dm-core', '=0.9.2'
5
+ require 'dm-core'
6
+
7
+ dir = Pathname(__FILE__).dirname.expand_path / 'dm-types'
8
+
9
+ require dir / 'csv'
10
+ require dir / 'enum'
11
+ require dir / 'epoch_time'
12
+ require dir / 'file_path'
13
+ require dir / 'flag'
14
+ require dir / 'ip_address'
15
+ require dir / "json"""
16
+ require dir / 'uri'
17
+ require dir / 'yaml'
18
+ require dir / 'serial'
@@ -0,0 +1,28 @@
1
+ module DataMapper
2
+ module Types
3
+ class Csv < DataMapper::Type
4
+ primitive String
5
+ size 65535
6
+ lazy true
7
+
8
+ def self.load(value, property)
9
+ case value
10
+ when String then FasterCSV.parse(value)
11
+ when Array then value
12
+ else nil
13
+ end
14
+ end
15
+
16
+ def self.dump(value, property)
17
+ case value
18
+ when Array then
19
+ FasterCSV.generate do |csv|
20
+ value.each { |row| csv << row }
21
+ end
22
+ when String then value
23
+ else nil
24
+ end
25
+ end
26
+ end # class Csv
27
+ end # module Types
28
+ end # module DataMapper
@@ -0,0 +1,40 @@
1
+ module DataMapper
2
+ module Types
3
+ class Enum < DataMapper::Type(Integer)
4
+ def self.inherited(target)
5
+ target.instance_variable_set("@primitive", self.primitive)
6
+ end
7
+
8
+ def self.flag_map
9
+ @flag_map
10
+ end
11
+
12
+ def self.flag_map=(value)
13
+ @flag_map = value
14
+ end
15
+
16
+ def self.new(*flags)
17
+ enum = Class.new(Enum)
18
+ enum.flag_map = {}
19
+
20
+ flags.each_with_index do |flag, i|
21
+ enum.flag_map[i + 1] = flag
22
+ end
23
+
24
+ enum
25
+ end
26
+
27
+ def self.[](*flags)
28
+ new(*flags)
29
+ end
30
+
31
+ def self.load(value, property)
32
+ self.flag_map[value]
33
+ end
34
+
35
+ def self.dump(value, property)
36
+ self.flag_map.invert[value]
37
+ end
38
+ end # class Enum
39
+ end # module Types
40
+ end # module DataMapper
@@ -0,0 +1,27 @@
1
+ module DataMapper
2
+ module Types
3
+ class EpochTime < DataMapper::Type
4
+ primitive Integer
5
+
6
+ def self.load(value, property)
7
+ case value
8
+ when Integer
9
+ Time.at(value)
10
+ else
11
+ value
12
+ end
13
+ end
14
+
15
+ def self.dump(value, property)
16
+ case value
17
+ when Integer
18
+ value
19
+ when Time
20
+ value.to_i
21
+ when DateTime
22
+ Time.parse(value.to_s).to_i
23
+ end
24
+ end
25
+ end # class EpochTime
26
+ end # module Types
27
+ end # module DataMapper
@@ -0,0 +1,22 @@
1
+ require 'pathname'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class FilePath < DataMapper::Type
6
+ primitive String
7
+
8
+ def self.load(value, property)
9
+ if value.nil?
10
+ nil
11
+ else
12
+ Pathname.new(value)
13
+ end
14
+ end
15
+
16
+ def self.dump(value, property)
17
+ return nil if value.nil?
18
+ value.to_s
19
+ end
20
+ end # class FilePath
21
+ end # module Types
22
+ end # module DataMapper
@@ -0,0 +1,51 @@
1
+ module DataMapper
2
+ module Types
3
+ class Flag < DataMapper::Type(Integer)
4
+
5
+ def self.flag_map
6
+ @flag_map
7
+ end
8
+
9
+ def self.flag_map=(value)
10
+ @flag_map = value
11
+ end
12
+
13
+ def self.new(*flags)
14
+ type = Flag.clone # cannot dup a Class
15
+ type.flag_map = {}
16
+
17
+ flags.each_with_index do |flag, i|
18
+ type.flag_map[2 ** i] = flag
19
+ end
20
+
21
+ type
22
+ end
23
+
24
+ def self.[](*flags)
25
+ new(*flags)
26
+ end
27
+
28
+ def self.load(value, property)
29
+ begin
30
+ matches = []
31
+
32
+ 0.upto((Math.log(value) / Math.log(2)).ceil) do |i|
33
+ pow = 2 ** i
34
+ matches << flag_map[pow] if value & pow == pow
35
+ end
36
+
37
+ matches.compact
38
+ rescue TypeError, Errno::EDOM
39
+ []
40
+ end
41
+ end
42
+
43
+ def self.dump(value, property)
44
+ return if value.nil?
45
+ flags = value.is_a?(Array) ? value : [value]
46
+ flags.map!{ |f| f.to_sym }
47
+ flag_map.invert.values_at(*flags.flatten).compact.inject(0) { |sum, i| sum + i }
48
+ end
49
+ end # class Flag
50
+ end # module Types
51
+ end # module DataMapper
@@ -0,0 +1,26 @@
1
+ require 'ipaddr'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class IPAddress < DataMapper::Type
6
+ primitive String
7
+
8
+ def self.load(value, property)
9
+ if value.nil?
10
+ nil
11
+ elsif value.is_a?(String) && !value.empty?
12
+ IPAddr.new(value)
13
+ elsif value.is_a?(String) && value.empty?
14
+ IPAddr.new("0.0.0.0")
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
+ return nil if value.nil?
22
+ value.to_s
23
+ end
24
+ end # class IPAddress
25
+ end # module Types
26
+ end # module DataMapper
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Json < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+ lazy true
9
+
10
+ def self.load(value, property)
11
+ if value.nil?
12
+ nil
13
+ elsif value.is_a?(String)
14
+ ::JSON.load(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
+ value
25
+ else
26
+ ::JSON.dump(value)
27
+ end
28
+ end
29
+ end # class Json
30
+ end # module Types
31
+ end # module DataMapper
@@ -0,0 +1,8 @@
1
+ module DataMapper
2
+ module Types
3
+ class Serial < DataMapper::Type
4
+ primitive Integer
5
+ serial true
6
+ end # class Serial
7
+ end # module Types
8
+ end # module DataMapper
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'addressable/uri'
3
+
4
+ module DataMapper
5
+ module Types
6
+ class URI < DataMapper::Type
7
+ primitive String
8
+
9
+ def self.load(value, property)
10
+ Addressable::URI.parse(value)
11
+ end
12
+
13
+ def self.dump(value, property)
14
+ return nil if value.nil?
15
+ value.to_s
16
+ end
17
+ end # class URI
18
+ end # module Types
19
+ end # module DataMapper
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ module DataMapper
4
+ module Types
5
+ class Yaml < DataMapper::Type
6
+ primitive String
7
+ size 65535
8
+ lazy true
9
+
10
+ def self.load(value, property)
11
+ if value.nil?
12
+ nil
13
+ elsif value.is_a?(String)
14
+ ::YAML.load(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) && value =~ /^---/
24
+ value
25
+ else
26
+ ::YAML.dump(value)
27
+ end
28
+ end
29
+ end # class Yaml
30
+ end # module Types
31
+ end # module DataMapper
@@ -0,0 +1,24 @@
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 Bug
7
+ include DataMapper::Resource
8
+
9
+ property :id, Integer, :serial => true
10
+ property :status, Enum[:crit, :warn, :info, :unknown]
11
+ end
12
+ Bug.auto_migrate!
13
+ end
14
+
15
+ it "should work" do
16
+ repository(:default) do
17
+ Bug.create!(:status => :crit)
18
+ Bug.create!(:status => :warn)
19
+ end
20
+ bugs = Bug.all
21
+ bugs[0].status.should == :crit
22
+ bugs[1].status.should == :warn
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Flag do
5
+ before(:all) do
6
+ class Shirt
7
+ include DataMapper::Resource
8
+ property :id, Serial
9
+ property :sizes, DM::Flag[:xs, :small, :medium, :large, :xl, :xxl]
10
+ end
11
+ Shirt.auto_migrate!
12
+ end
13
+
14
+ it "should save with create({:flag_field => [:flags]})" do
15
+ lambda { Shirt.create(:sizes => [:medium, :large]) }.should_not raise_error
16
+ repository do
17
+ Shirt.get(1).sizes.should == [:medium, :large]
18
+ end
19
+ end
20
+
21
+ it "should save with flag_field=[:flags]" do
22
+ shirt = Shirt.new
23
+ shirt.sizes = [:small, :xs]
24
+ lambda { shirt.save }.should_not raise_error
25
+ repository do
26
+ Shirt.get(2).sizes.should == [:xs, :small]
27
+ end
28
+ end
29
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+ require 'pathname'
5
+ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/dm-types'
6
+
7
+ ENV["SQLITE3_SPEC_URI"] ||= 'sqlite3::memory:'
8
+ ENV["MYSQL_SPEC_URI"] ||= 'mysql://localhost/dm_core_test'
9
+ ENV["POSTGRES_SPEC_URI"] ||= 'postgres://postgres@localhost/dm_more_test'
10
+
11
+ def setup_adapter(name, default_uri = nil)
12
+ begin
13
+ DataMapper.setup(name, ENV["#{ENV['ADAPTER'].to_s.upcase}_SPEC_URI"] || default_uri)
14
+ Object.const_set('ADAPTER', ENV['ADAPTER'].to_sym) if name.to_s == ENV['ADAPTER']
15
+ true
16
+ rescue Exception => e
17
+ if name.to_s == ENV['ADAPTER']
18
+ Object.const_set('ADAPTER', nil)
19
+ warn "Could not load #{name} adapter: #{e}"
20
+ end
21
+ false
22
+ end
23
+ end
24
+
25
+ ENV['ADAPTER'] ||= 'sqlite3'
26
+
27
+ setup_adapter(:default)
@@ -0,0 +1,66 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Enum do
5
+
6
+ describe ".new" do
7
+ it "should create a Class" do
8
+ DataMapper::Types::Enum.new.should be_instance_of(Class)
9
+ end
10
+
11
+ it "should create unique a Class each call" do
12
+ DataMapper::Types::Enum.new.should_not == DataMapper::Types::Enum.new
13
+ end
14
+
15
+ it "should use the arguments as the values in the @flag_map hash" do
16
+ DataMapper::Types::Enum.new(:first, :second, :third).flag_map.values.should == [:first, :second, :third]
17
+ end
18
+
19
+ it "should create incremental keys for the @flag_map hash, staring at 1" do
20
+ DataMapper::Types::Enum.new(:one, :two, :three, :four).flag_map.keys.should == (1..4).to_a
21
+ end
22
+
23
+ it "should have an Integer primitive type" do
24
+ DataMapper::Types::Enum.new.primitive.should == Integer
25
+ end
26
+ end
27
+
28
+ describe ".[]" do
29
+ it "should be an alias for the new method" do
30
+ DataMapper::Types::Enum.should_receive(:new).with(:uno, :dos, :tres)
31
+ DataMapper::Types::Enum[:uno, :dos, :tres]
32
+ end
33
+ end
34
+
35
+ describe ".dump" do
36
+ before(:each) do
37
+ @enum = DataMapper::Types::Enum[:first, :second, :third]
38
+ end
39
+
40
+ it "should return the key of the value match from the flag map" do
41
+ @enum.dump(:first, :property).should == 1
42
+ @enum.dump(:second, :property).should == 2
43
+ @enum.dump(:third, :property).should == 3
44
+ end
45
+
46
+ it "should return nil if there is no match" do
47
+ @enum.dump(:zero, :property).should be_nil
48
+ end
49
+ end
50
+
51
+ describe ".load" do
52
+ before(:each) do
53
+ @enum = DataMapper::Types::Enum[:uno, :dos, :tres]
54
+ end
55
+
56
+ it "should return the value of the key match from the flag map" do
57
+ @enum.load(1, :property).should == :uno
58
+ @enum.load(2, :property).should == :dos
59
+ @enum.load(3, :property).should == :tres
60
+ end
61
+
62
+ it "should return nil if there is no key" do
63
+ @enum.load(-1, :property).should be_nil
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,37 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::EpochTime do
5
+
6
+ describe ".dump" do
7
+ it "should accept Time objects" do
8
+ t = Time.now
9
+
10
+ DataMapper::Types::EpochTime.dump(t, :property).should == t.to_i
11
+ end
12
+
13
+ it "should accept DateTime objects" do
14
+ t = DateTime.now
15
+
16
+ DataMapper::Types::EpochTime.dump(t, :property).should == Time.parse(t.to_s).to_i
17
+ end
18
+
19
+ it "should accept Integer objects" do
20
+ t = Time.now.to_i
21
+
22
+ DataMapper::Types::EpochTime.dump(t, :property).should == t
23
+ end
24
+ end
25
+
26
+ describe ".load" do
27
+
28
+ it "should load null as nil" do
29
+ DataMapper::Types::EpochTime.load(nil, :property).should == nil
30
+ end
31
+
32
+ it "should load #{Time.now.to_i} as Time.at(#{Time.now.to_i})" do
33
+ t = Time.now.to_i
34
+ DataMapper::Types::EpochTime.load(Time.now.to_i, :property).should == Time.at(t)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::FilePath do
5
+
6
+ before(:each) do
7
+ @path_str = "/usr/bin/ruby"
8
+ @path = Pathname.new(@path_str)
9
+ end
10
+
11
+ describe ".dump" do
12
+ it "should return the file path as a String" do
13
+ DataMapper::Types::FilePath.dump(@path_str, :property).should == @path_str
14
+ end
15
+
16
+ it "should return nil if the String is nil" do
17
+ DataMapper::Types::FilePath.dump(nil, :property).should be_nil
18
+ end
19
+
20
+ it "should return an empty file path if the String is empty" do
21
+ DataMapper::Types::FilePath.dump("", :property).should == ""
22
+ end
23
+ end
24
+
25
+ describe ".load" do
26
+ it "should return the file path as a Pathname" do
27
+ DataMapper::Types::FilePath.load(@uri_str, :property).should == @uri
28
+ end
29
+
30
+ it "should return nil if given nil" do
31
+ DataMapper::Types::FilePath.load(nil, :property).should be_nil
32
+ end
33
+
34
+ it "should return an empty Pathname if given an empty String" do
35
+ DataMapper::Types::FilePath.load("", :property).should == Pathname.new("")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,86 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::Flag do
5
+
6
+ describe ".new" do
7
+ it "should create a Class" do
8
+ DataMapper::Types::Flag.new.should be_instance_of(Class)
9
+ end
10
+
11
+ it "should create unique a Class each call" do
12
+ DataMapper::Types::Flag.new.should_not == DataMapper::Types::Flag.new
13
+ end
14
+
15
+ it "should use the arguments as the values in the @flag_map hash" do
16
+ DataMapper::Types::Flag.new(:first, :second, :third).flag_map.values.should == [:first, :second, :third]
17
+ end
18
+
19
+ it "should create keys by the 2 power series for the @flag_map hash, staring at 1" do
20
+ DataMapper::Types::Flag.new(:one, :two, :three, :four, :five).flag_map.keys.should include(1, 2, 4, 8, 16)
21
+ end
22
+ end
23
+
24
+ describe ".[]" do
25
+ it "should be an alias for the new method" do
26
+ DataMapper::Types::Flag.should_receive(:new).with(:uno, :dos, :tres)
27
+ DataMapper::Types::Flag[:uno, :dos, :tres]
28
+ end
29
+ end
30
+
31
+ describe ".dump" do
32
+ before(:each) do
33
+ @flag = DataMapper::Types::Flag[:first, :second, :third, :fourth, :fifth]
34
+ end
35
+
36
+ it "should return the key of the value match from the flag map" do
37
+ @flag.dump(:first, :property).should == 1
38
+ @flag.dump(:second, :property).should == 2
39
+ @flag.dump(:third, :property).should == 4
40
+ @flag.dump(:fourth, :property).should == 8
41
+ @flag.dump(:fifth, :property).should == 16
42
+ end
43
+
44
+ it "should return a binary flag built from the key values of all matches" do
45
+ @flag.dump([:first, :second], :property).should == 3
46
+ @flag.dump([:second, :fourth], :property).should == 10
47
+ @flag.dump([:first, :second, :third, :fourth, :fifth], :property).should == 31
48
+ end
49
+
50
+ it "should return a binary flag built from the key values of all matches even if strings" do
51
+ @flag.dump(["first", "second"], :property).should == 3
52
+ @flag.dump(["second", "fourth"], :property).should == 10
53
+ @flag.dump(["first", "second", "third", "fourth", "fifth"], :property).should == 31
54
+ end
55
+
56
+ it "should return 0 if there is no match" do
57
+ @flag.dump(:zero, :property).should == 0
58
+ end
59
+ end
60
+
61
+ describe ".load" do
62
+ before(:each) do
63
+ @flag = DataMapper::Types::Flag[:uno, :dos, :tres, :cuatro, :cinco]
64
+ end
65
+
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]
71
+ @flag.load(16, :property).should == [:cinco]
72
+ end
73
+
74
+ it "should return an array of all flags matches" do
75
+ @flag.load(3, :property).should include(:uno, :dos)
76
+ @flag.load(10, :property).should include(:dos, :cuatro)
77
+ @flag.load(31, :property).should include(:uno, :dos, :tres, :cuatro, :cinco)
78
+ end
79
+
80
+ it "should return an empty array if there is no key" do
81
+ @flag.load(-1, :property).should == []
82
+ @flag.load(nil, :property).should == []
83
+ @flag.load(32, :property).should == []
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,39 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::IPAddress do
5
+
6
+ before(:each) do
7
+ @ip_str = "81.20.130.1"
8
+ @ip = IPAddr.new(@ip_str)
9
+ end
10
+
11
+ describe ".dump" do
12
+ it "should return the IP address as a string" do
13
+ DataMapper::Types::IPAddress.dump(@ip, :property).should == @ip_str
14
+ end
15
+
16
+ it "should return nil if the string is nil" do
17
+ DataMapper::Types::IPAddress.dump(nil, :property).should be_nil
18
+ end
19
+
20
+ it "should return an empty IP address if the string is empty" do
21
+ DataMapper::Types::IPAddress.dump("", :property).should == ""
22
+ end
23
+ end
24
+
25
+ describe ".load" do
26
+ it "should return the IP address string as IPAddr" do
27
+ DataMapper::Types::IPAddress.load(@ip_str, :property).should == @ip
28
+ end
29
+
30
+ it "should return nil if given nil" do
31
+ DataMapper::Types::IPAddress.load(nil, :property).should be_nil
32
+ end
33
+
34
+ it "should return an empty IP address if given an empty string" do
35
+ DataMapper::Types::IPAddress.load("", :property).should == IPAddr.new("0.0.0.0")
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,38 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
3
+
4
+ describe DataMapper::Types::URI do
5
+
6
+ before(:each) do
7
+ @uri_str = "http://example.com/path/to/resource/"
8
+ @uri = Addressable::URI.parse(@uri_str)
9
+ end
10
+
11
+ describe ".dump" do
12
+ it "should return the URI as a String" do
13
+ DataMapper::Types::URI.dump(@uri, :property).should == @uri_str
14
+ end
15
+
16
+ it "should return nil if the String is nil" do
17
+ DataMapper::Types::URI.dump(nil, :property).should be_nil
18
+ end
19
+
20
+ it "should return an empty URI if the String is empty" do
21
+ DataMapper::Types::URI.dump("", :property).should == ""
22
+ end
23
+ end
24
+
25
+ describe ".load" do
26
+ it "should return the URI as Addressable" do
27
+ DataMapper::Types::URI.load(@uri_str, :property).should == @uri
28
+ end
29
+
30
+ it "should return nil if given nil" do
31
+ DataMapper::Types::URI.load(nil, :property).should be_nil
32
+ end
33
+
34
+ it "should return an empty URI if given an empty String" do
35
+ DataMapper::Types::URI.load("", :property).should == Addressable::URI.parse("")
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-types
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Sam Smoot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ version:
24
+ description: DataMapper plugin providing extra data types
25
+ email: ssmoot@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - lib/dm-types/csv.rb
36
+ - lib/dm-types/enum.rb
37
+ - lib/dm-types/epoch_time.rb
38
+ - lib/dm-types/file_path.rb
39
+ - lib/dm-types/flag.rb
40
+ - lib/dm-types/ip_address.rb
41
+ - lib/dm-types/json.rb
42
+ - lib/dm-types/serial.rb
43
+ - lib/dm-types/uri.rb
44
+ - lib/dm-types/yaml.rb
45
+ - lib/dm-types.rb
46
+ - spec/integration/enum_spec.rb
47
+ - spec/integration/flag_spec.rb
48
+ - spec/spec_helper.rb
49
+ - spec/unit/enum_spec.rb
50
+ - spec/unit/epoch_time_spec.rb
51
+ - spec/unit/file_path_spec.rb
52
+ - spec/unit/flag_spec.rb
53
+ - spec/unit/ip_address_spec.rb
54
+ - spec/unit/uri_spec.rb
55
+ - spec/spec.opts
56
+ - Rakefile
57
+ - README
58
+ - LICENSE
59
+ - TODO
60
+ has_rdoc: true
61
+ homepage: http://github.com/sam/dm-more/tree/master/dm-types
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.0.1
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: DataMapper plugin providing extra data types
86
+ test_files: []
87
+