dm-types 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
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.3'
4
+ gem 'dm-core', '=0.9.4'
5
5
  require 'dm-core'
6
6
 
7
7
  dir = Pathname(__FILE__).dirname.expand_path / 'dm-types'
data/lib/dm-types/enum.rb CHANGED
@@ -33,7 +33,10 @@ module DataMapper
33
33
  end
34
34
 
35
35
  def self.dump(value, property)
36
- self.flag_map.invert[value]
36
+ case value
37
+ when Array then value.collect { |v| self.dump(v, property) }
38
+ else self.flag_map.invert[value]
39
+ end
37
40
  end
38
41
 
39
42
  def self.typecast(value, property)
@@ -48,4 +51,20 @@ module DataMapper
48
51
  end
49
52
  end # class Enum
50
53
  end # module Types
54
+
55
+ if defined?(Validate)
56
+ module Validate
57
+ module AutoValidate
58
+ alias :orig_auto_generate_validations :auto_generate_validations
59
+ def auto_generate_validations(property)
60
+ orig_auto_generate_validations(property)
61
+ return unless property.options[:auto_validation]
62
+
63
+ if property.type.ancestors.include?(Types::Enum)
64
+ validates_within property.name, options_with_message({:set => property.type.flag_map.values}, property, :within)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
51
70
  end # module DataMapper
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Types
3
- VERSION = "0.9.3"
3
+ VERSION = "0.9.4"
4
4
  end
5
- end
5
+ end
@@ -14,8 +14,8 @@ describe DataMapper::Types::Enum do
14
14
 
15
15
  it "should work" do
16
16
  repository(:default) do
17
- Bug.create!(:status => :crit)
18
- Bug.create!(:status => :warn)
17
+ Bug.create(:status => :crit)
18
+ Bug.create(:status => :warn)
19
19
  end
20
20
 
21
21
  bugs = Bug.all
@@ -26,4 +26,33 @@ describe DataMapper::Types::Enum do
26
26
  it 'should immediately typecast supplied values' do
27
27
  Bug.new(:status => :crit).status.should == :crit
28
28
  end
29
+
30
+ describe "with finders" do
31
+ before(:all) do
32
+ @info = Bug.create(:status => :info)
33
+ end
34
+ it "should work with equality opeand" do
35
+ Bug.all(:status => [:info, :unknown]).entries.should == [@info]
36
+ end
37
+ it "should work with inequality operand" do
38
+ Bug.all(:status.not => [:crit, :warn]).entries.should == [@info]
39
+ end
40
+ end
41
+
42
+ if defined?(Validate)
43
+ describe 'with validation' do
44
+ it "should accept crit status" do
45
+ bug = Bug.new
46
+ bug.status = :crit
47
+ bug.should be_valid
48
+ end
49
+
50
+ it "should not accept blah status" do
51
+ bug = Bug.new
52
+ bug.status = :blah
53
+ bug.should_not be_valid
54
+ bug.errors.on(:status).should_not be_empty
55
+ end
56
+ end
57
+ end
29
58
  end
@@ -16,7 +16,7 @@ describe DataMapper::Types::FilePath do
16
16
  repository(:default) do
17
17
  FilePathTest.create!(:path => '/usr')
18
18
  end
19
-
19
+
20
20
  FilePathTest.first.path.should == Pathname.new('/usr')
21
21
  end
22
22
 
@@ -8,15 +8,15 @@ describe DataMapper::Types::Csv, ".load" do
8
8
  FasterCSV.should_receive(:parse).with('csv_string').once
9
9
  DataMapper::Types::Csv.load('csv_string', :property)
10
10
  end
11
-
11
+
12
12
  it 'should do nothing if the value is a string' do
13
13
  FasterCSV.should_not_receive(:parse)
14
14
  DataMapper::Types::Csv.load([], :property).should == []
15
15
  end
16
-
16
+
17
17
  it 'should return nil otherwise' do
18
18
  DataMapper::Types::Csv.load({}, :property).should == nil
19
- DataMapper::Types::Csv.load(nil, :property).should == nil
19
+ DataMapper::Types::Csv.load(nil, :property).should == nil
20
20
  end
21
21
  end
22
22
 
@@ -24,14 +24,14 @@ describe DataMapper::Types::Csv, ".dump" do
24
24
  it 'should dump to CSV' do
25
25
  DataMapper::Types::Csv.dump([[1, 2, 3]], :property).should == "1,2,3\n"
26
26
  end
27
-
27
+
28
28
  it 'should do nothing if the value is a string' do
29
29
  FasterCSV.should_not_receive(:generate)
30
30
  DataMapper::Types::Csv.dump('string', :property).should == 'string'
31
31
  end
32
-
32
+
33
33
  it 'should return nil otherwise' do
34
34
  DataMapper::Types::Csv.dump({}, :property).should == nil
35
- DataMapper::Types::Csv.dump(nil, :property).should == nil
35
+ DataMapper::Types::Csv.dump(nil, :property).should == nil
36
36
  end
37
37
  end
@@ -5,12 +5,12 @@ describe DataMapper::Types::Json, ".load" do
5
5
  it 'should return nil if nil is provided' do
6
6
  DataMapper::Types::Json.load(nil, :property).should be_nil
7
7
  end
8
-
8
+
9
9
  it 'should parse the value if a string is provided' do
10
10
  JSON.should_receive(:load).with('json_string').once
11
11
  DataMapper::Types::Json.load('json_string', :property)
12
12
  end
13
-
13
+
14
14
  it 'should raise an ArgumentError if something else is given' do
15
15
  lambda {
16
16
  DataMapper::Types::Json.load(:sym, :property)
@@ -22,12 +22,12 @@ describe DataMapper::Types::Json, ".dump" do
22
22
  it 'should return nil if the value is nil' do
23
23
  DataMapper::Types::Json.dump(nil, :property).should be_nil
24
24
  end
25
-
25
+
26
26
  it 'should do nothing if the value is a string' do
27
27
  JSON.should_not_receive(:dump)
28
28
  DataMapper::Types::Json.dump('', :property).should be_kind_of(String)
29
29
  end
30
-
30
+
31
31
  it 'should dump to a JSON string otherwise' do
32
32
  JSON.should_receive(:dump).with([]).once
33
33
  DataMapper::Types::Json.dump([], :property)
@@ -5,12 +5,12 @@ describe DataMapper::Types::Yaml, ".load" do
5
5
  it 'should return nil if nil is provided' do
6
6
  DataMapper::Types::Yaml.load(nil, :property).should be_nil
7
7
  end
8
-
8
+
9
9
  it 'should parse the value if a string is provided' do
10
10
  YAML.should_receive(:load).with('yaml_string').once
11
11
  DataMapper::Types::Yaml.load('yaml_string', :property)
12
12
  end
13
-
13
+
14
14
  it 'should raise an ArgumentError if something else is given' do
15
15
  lambda {
16
16
  DataMapper::Types::Yaml.load(:sym, :property)
@@ -22,17 +22,17 @@ describe DataMapper::Types::Yaml, ".dump" do
22
22
  it 'should return nil if the value is nil' do
23
23
  DataMapper::Types::Yaml.dump(nil, :property).should be_nil
24
24
  end
25
-
25
+
26
26
  it 'should do nothing if the value is a string which begins with ---' do
27
27
  YAML.should_not_receive(:dump)
28
28
  DataMapper::Types::Yaml.dump('--- str', :property).should be_kind_of(String)
29
29
  end
30
-
30
+
31
31
  it 'should dump to a YAML string if the value is a normal string' do
32
32
  YAML.should_receive(:dump).with('string').once
33
33
  DataMapper::Types::Yaml.dump('string', :property)
34
34
  end
35
-
35
+
36
36
  it 'should dump to a YAML string otherwise' do
37
37
  YAML.should_receive(:dump).with([]).once
38
38
  DataMapper::Types::Yaml.dump([], :property)
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.3
4
+ version: 0.9.4
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-07-24 00:00:00 -05:00
12
+ date: 2008-08-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.3
23
+ version: 0.9.4
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe