riot-datamapper 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,11 +39,16 @@ class User
39
39
 
40
40
  property :foo, String
41
41
  property :bar, Serial
42
+ property :buzz, Integer
42
43
  property :monkey, Boolean, :default => false
43
44
  property :name, String, :default => 'monkey', :required => true
44
45
 
45
46
  has n, :comments, :through => :posts
46
47
  has 1, :address
48
+
49
+ validates_presence_of :foo
50
+ validates_uniquness_of :bar
51
+ validates_length_of :buzz, :maximum => 20
47
52
  end
48
53
 
49
54
  class Address
@@ -87,6 +92,10 @@ context "User Model" do
87
92
 
88
93
  asserts_topic.has_association :has_n, :comments, :through => :posts
89
94
  asserts_topic.has_association :has 1, :address
95
+
96
+ asserts_topic.has_validation :validates_presence_of, :foo
97
+ asserts_topic.has_validation :validates_uniquness_of, :bar
98
+ asserts_topic.has_validation :validates_length_of, :buzz, :maximum => 20
90
99
  end
91
100
 
92
101
  context "Address Model" do
@@ -119,11 +128,6 @@ context "Post Model" do
119
128
  end
120
129
  ```
121
130
 
122
- ## TODO ##
123
-
124
- * Add more macros
125
- * Add validation macros so you can do something like #has_validation :validates_presence_of
126
-
127
131
  ## Copyright
128
132
 
129
133
  Copyright © 2011 Arthur Chiu. See [MIT-LICENSE](https://github.com/achiu/riot-datamapper/blob/master/MIT-LICENSE) for details.
@@ -0,0 +1,31 @@
1
+ module Riot
2
+ module DataMapper
3
+ class HasValidation < Riot::AssertionMacro
4
+ register :has_validation
5
+
6
+ def evaluate(model, validation, field, options={})
7
+
8
+ validator = model.validators.context(:default).detect do |v|
9
+ keyword = validation.to_s.match(/validates_(\w+)_of/)[1]
10
+ v.class.inspect.downcase =~ /#{keyword}/ && v.field_name.to_sym == field.to_sym
11
+ end
12
+
13
+ fail_msg = "expected #{model} to have validation :#{validation} on :#{field}"
14
+ pass_msg = "#{model} has validation :#{validation} on :#{field}"
15
+ options_msg = " with options #{options.inspect}"
16
+
17
+ return fail(fail_msg) if validator.nil?
18
+
19
+ unless options.empty?
20
+ if options.all? { |key, value| validator.options[key] == value }
21
+ pass_msg << options_msg
22
+ else
23
+ return fail(fail_msg + options_msg)
24
+ end
25
+ end
26
+
27
+ pass(pass_msg)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  module Riot
2
2
  module Datamapper
3
- VERSION = "0.0.4"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../teststrap', __FILE__)
2
+
3
+ context "has_validation macro" do
4
+ setup do
5
+ Object.send(:remove_const, :FooBar) if Object.const_defined?(:FooBar)
6
+
7
+ class FooBar
8
+ include DataMapper::Resource
9
+
10
+ property :id, Serial
11
+ property :foo, String
12
+ property :bar, Integer
13
+ property :buzz, Integer
14
+
15
+ validates_presence_of :foo
16
+ validates_numericality_of :bar
17
+ validates_length_of :buzz, :maximum => 20
18
+ end
19
+ DataMapper.finalize ; FooBar
20
+ end
21
+
22
+ asserts "that it passes when the model has the validation" do
23
+ Riot::DataMapper::HasValidation.new.evaluate(topic, :validates_presence_of, :foo).first
24
+ end.equals :pass
25
+
26
+ asserts "that it fails when the model does not have the validation" do
27
+ Riot::DataMapper::HasValidation.new.evaluate(topic, :validates_presence_of, :bar).first
28
+ end.equals :fail
29
+
30
+ asserts "that it passes when the model has the validation and options" do
31
+ Riot::DataMapper::HasValidation.new.evaluate(topic, :validates_length_of, :buzz, :maximum => 20).first
32
+ end.equals :pass
33
+
34
+ asserts "that it fails when the model does not have the validation and options" do
35
+ Riot::DataMapper::HasValidation.new.evaluate(topic, :validates_length_of, :buzz, :maximum => 30).first
36
+ end.equals :fail
37
+
38
+ asserts "that it has message when the model has the validation and options" do
39
+ Riot::DataMapper::HasValidation.new.evaluate(topic, :validates_length_of, :buzz, :maximum => 20).last
40
+ end.equals "FooBar has validation :validates_length_of on :buzz with options {:maximum=>20}"
41
+
42
+ end
metadata CHANGED
@@ -1,60 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: riot-datamapper
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
4
5
  prerelease:
5
- version: 0.0.4
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Arthur Chiu
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-18 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: riot
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2172728880 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: dm-core
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *2172728880
25
+ - !ruby/object:Gem::Dependency
26
+ name: dm-core
27
+ requirement: &2172728460 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
35
33
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: dm-validations
39
34
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *2172728460
36
+ - !ruby/object:Gem::Dependency
37
+ name: dm-validations
38
+ requirement: &2172759240 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
46
44
  type: :runtime
47
- version_requirements: *id003
45
+ prerelease: false
46
+ version_requirements: *2172759240
48
47
  description: Riot Assertion Macros for DataMapper
49
- email:
48
+ email:
50
49
  - mr.arthur.chiu@gmail.com
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
52
  extra_rdoc_files: []
56
-
57
- files:
53
+ files:
58
54
  - .gitignore
59
55
  - Gemfile
60
56
  - MIT-LICENSE
@@ -68,35 +64,34 @@ files:
68
64
  - riot-datamapper.gemspec
69
65
  - test/has_association_test.rb
70
66
  - test/has_property_test.rb
67
+ - test/has_validation_test.rb
71
68
  - test/teststrap.rb
72
69
  homepage: https://www.github.com/achiu/riot-datamapper
73
70
  licenses: []
74
-
75
71
  post_install_message:
76
72
  rdoc_options: []
77
-
78
- require_paths:
73
+ require_paths:
79
74
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
75
+ required_ruby_version: !ruby/object:Gem::Requirement
81
76
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- version: "0"
86
- required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
82
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- version: "0"
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
92
87
  requirements: []
93
-
94
88
  rubyforge_project: riot-datamapper
95
- rubygems_version: 1.8.4
89
+ rubygems_version: 1.8.6
96
90
  signing_key:
97
91
  specification_version: 3
98
92
  summary: Riot Assertion Macros for DataMapper
99
- test_files:
93
+ test_files:
100
94
  - test/has_association_test.rb
101
95
  - test/has_property_test.rb
96
+ - test/has_validation_test.rb
102
97
  - test/teststrap.rb