sequel_validation_helpers_block 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2009 Jeremy Evans
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,89 @@
1
+ module Sequel
2
+ module Plugins
3
+ # The ValidationHelpersBlock plugin allows easy determination of which
4
+ # validation rules apply to a given column, by grouping validations
5
+ # by column instead of by validation type. It is significantly more
6
+ # verbose than the default validation_helpers plugin, but provides
7
+ # a nicer DSL, for example:
8
+ #
9
+ # class Item < Sequel::Model
10
+ # plugin :validation_helpers_block
11
+ #
12
+ # def validate
13
+ # validates do
14
+ # name do
15
+ # presence
16
+ # max_length 10
17
+ # end
18
+ # date do
19
+ # format %r{\d\d/\d\d/\d\d\d\d}
20
+ # end
21
+ # number do
22
+ # presence
23
+ # integer
24
+ # end
25
+ # end
26
+ # end
27
+ # end
28
+ module ValidationHelpersBlock
29
+ # Require the validation_helpers plugin
30
+ def self.apply(model)
31
+ model.plugin(:validation_helpers)
32
+ end
33
+
34
+ # DSL class used directly inside the validates block.
35
+ # Methods without an explicit receiver that are called
36
+ # inside the block are assumed to be attributes of the
37
+ # object that need to be validated.
38
+ class ValidationHelpersAttributesBlock < BasicObject
39
+ # Set the object being validated and instance_eval the block.
40
+ def initialize(obj, &block)
41
+ @obj = obj
42
+ instance_eval(&block)
43
+ end
44
+
45
+ # Create a new ValidationHelpersValidationsBlock object
46
+ # using the stored object and given attribute name.
47
+ def method_missing(m, &block)
48
+ ValidationHelpersValidationsBlock.new(@obj, m, &block)
49
+ end
50
+ end
51
+
52
+ # DSL class used inside attribute blocks.
53
+ # The only methods allowed inside the block are those supported
54
+ # by validation_helpers, and they specify the validations to
55
+ # run for the related attribute on the related object.
56
+ class ValidationHelpersValidationsBlock < BasicObject
57
+ # validation_helpers methods that do not require a leading argument.
58
+ VALIDATION_HELPERS_NO_ARG_METHODS = [:integer, :not_string, :numeric, :presence, :unique].freeze
59
+
60
+ # validation_helpers methods that require a leading argument
61
+ VALIDATION_HELPERS_1_ARG_METHODS = [:exact_length, :format, :includes, :length_range, :max_length, :min_length].freeze
62
+
63
+ # Store the object and attribute and instance_eval the block.
64
+ def initialize(obj, attr, &block)
65
+ @obj = obj
66
+ @attr = attr
67
+ instance_eval(&block)
68
+ end
69
+
70
+ VALIDATION_HELPERS_NO_ARG_METHODS.each do |m|
71
+ class_eval("def #{m}(opts={}); @obj.validates_#{m}(@attr, opts); end", __FILE__, __LINE__)
72
+ end
73
+
74
+ VALIDATION_HELPERS_1_ARG_METHODS.each do |m|
75
+ class_eval("def #{m}(arg, opts={}); @obj.validates_#{m}(arg, @attr, opts); end", __FILE__, __LINE__)
76
+ end
77
+ end
78
+
79
+ module InstanceMethods
80
+ private
81
+
82
+ # Start a new validation_helpers_block DSL for the given object
83
+ def validates(&block)
84
+ ValidationHelpersAttributesBlock.new(self, &block)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,110 @@
1
+ require 'rubygems'
2
+ require 'sequel'
3
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ Sequel.extension :blank
5
+
6
+ describe "Sequel::Plugins::ValidationHelpersBlock" do
7
+ before do
8
+ @db = Sequel::Database.new
9
+ @c = Class.new(Sequel::Model(@db)) do
10
+ def self.set_validations(&block)
11
+ define_method(:validate, &block)
12
+ end
13
+ set_columns([:name, :date, :number])
14
+ end
15
+ @c.plugin :validation_helpers_block
16
+ @m = @c.new
17
+ end
18
+
19
+ specify "should allow specifying validations in a block format" do
20
+ @c.set_validations do
21
+ validates do
22
+ name do
23
+ presence
24
+ max_length 10
25
+ end
26
+ date do
27
+ format %r{\d\d/\d\d/\d\d\d\d}
28
+ end
29
+ number do
30
+ presence
31
+ integer
32
+ end
33
+ end
34
+ end
35
+ @m.should_not be_valid
36
+ @m.errors.should == {:name=>["is not present", "is longer than 10 characters"], :date=>["is invalid"], :number=>["is not present", "is not a number"]}
37
+ @m.set(:name=>'1234567890-', :number=>'a', :date=>'Tuesday')
38
+ @m.should_not be_valid
39
+ @m.errors.should == {:name=>["is longer than 10 characters"], :date=>["is invalid"], :number=>["is not a number"]}
40
+ @m.set(:name=>'1234', :number=>'10', :date=>'10/11/2009')
41
+ @m.should be_valid
42
+ end
43
+
44
+ specify "should accept options for validation methods" do
45
+ @c.set_validations do
46
+ validates do
47
+ name do
48
+ max_length 10, :message=>'cannot be more than 10 characters', :allow_blank=>true
49
+ end
50
+ date do
51
+ format %r{\d\d/\d\d/\d\d\d\d}, :allow_missing=>true
52
+ end
53
+ number do
54
+ integer :allow_nil=>true
55
+ end
56
+ end
57
+ end
58
+ @m.should be_valid
59
+ @m.errors.should == {}
60
+ @m.set(:name=>' ', :number=>nil)
61
+ @m.should be_valid
62
+ @m.errors.should == {}
63
+ @m.set(:name=>' 12 ', :number=>'', :date=>nil)
64
+ @m.should_not be_valid
65
+ @m.errors.should == {:name=>["cannot be more than 10 characters"], :date=>["is invalid"], :number=>["is not a number"]}
66
+ end
67
+
68
+ specify "should support all validation_helpers methods" do
69
+ @c.set_validations do
70
+ validates do
71
+ name do
72
+ max_length 12
73
+ exact_length 10
74
+ min_length 8
75
+ length_range 9..11
76
+ unique
77
+ end
78
+ date do
79
+ format %r{\d\d/\d\d/\d\d\d\d}
80
+ includes ['10/11/2009']
81
+ end
82
+ number do
83
+ presence
84
+ integer
85
+ numeric
86
+ not_string
87
+ end
88
+ end
89
+ end
90
+ ds = @db.dataset
91
+ ds.extend(Module.new {
92
+ def columns(sql)
93
+ [:name, :date, :number]
94
+ end
95
+
96
+ def fetch_rows(sql)
97
+ yield({:v => /COUNT.*"?name"? = '1234567890'/i.match(sql) ? 0 : 1})
98
+ end
99
+ })
100
+ @c.dataset = ds
101
+ @m.should_not be_valid
102
+ @m.errors.should == {:name=>["is longer than 12 characters", "is not 10 characters", "is shorter than 8 characters", "is too short or too long", "is already taken"], :date=>["is invalid", "is not in range or set: [\"10/11/2009\"]"], :number=>["is not present", "is not a number", "is not a number"]}
103
+ @m.set(:name=>'123456789', :date=>'10/12/2009', :number=>'12')
104
+ @m.should_not be_valid
105
+ @m.errors.should == {:name=>["is not 10 characters", "is already taken"], :date=>["is not in range or set: [\"10/11/2009\"]"], :number=>["is a string"]}
106
+ @m.set(:name=>'1234567890', :date=>'10/11/2009', :number=>12)
107
+ @m.should be_valid
108
+ @m.errors.should == {}
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_validation_helpers_block
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: code@jeremyevans.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - LICENSE
26
+ - lib/sequel_validation_helpers_block.rb
27
+ - spec/sequel_validation_helpers_block_spec.rb
28
+ has_rdoc: true
29
+ homepage:
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --inline-source
35
+ - --line-numbers
36
+ - --title
37
+ - "Sequel validation_helpers_block: Allows easy determination of which validation rules apply to a given column, at the expense of increased verbosity"
38
+ - --main
39
+ - Sequel::Plugins::ValidationHelpersBlock
40
+ - lib/sequel_validation_helpers_block.rb
41
+ - LICENSE
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Allows easy determination of which validation rules apply to a given column, at the expense of increased verbosity
63
+ test_files: []
64
+