dao 3.3.0 → 4.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +7 -0
- data/Rakefile +36 -17
- data/b.rb +38 -0
- data/dao.gemspec +41 -13
- data/lib/dao.rb +44 -13
- data/lib/dao/api.rb +1 -1
- data/lib/dao/api/context.rb +35 -45
- data/lib/dao/api/endpoints.rb +225 -91
- data/lib/dao/conducer.rb +437 -0
- data/lib/dao/conducer/attributes.rb +21 -0
- data/lib/dao/conducer/crud.rb +70 -0
- data/lib/dao/current.rb +66 -0
- data/lib/dao/db.rb +44 -5
- data/lib/dao/endpoint.rb +13 -1
- data/lib/dao/errors.rb +74 -59
- data/lib/dao/exceptions.rb +1 -2
- data/lib/dao/extractor.rb +68 -0
- data/lib/dao/form.rb +139 -46
- data/lib/dao/image_cache.rb +193 -0
- data/lib/dao/instance_exec.rb +1 -1
- data/lib/dao/name.rb +7 -0
- data/lib/dao/params.rb +16 -66
- data/lib/dao/rack.rb +3 -0
- data/lib/dao/rack/middleware.rb +5 -0
- data/lib/dao/rack/middleware/params_parser.rb +24 -0
- data/lib/dao/rails.rb +22 -5
- data/lib/dao/rails/lib/generators/dao/USAGE +2 -6
- data/lib/dao/rails/lib/generators/dao/dao_generator.rb +52 -7
- data/lib/dao/rails/lib/generators/dao/templates/api.rb +23 -7
- data/lib/dao/rails/lib/generators/dao/templates/api_controller.rb +24 -7
- data/lib/dao/rails/lib/generators/dao/templates/conducer.rb +64 -0
- data/lib/dao/rails/lib/generators/dao/templates/conducer_controller.rb +79 -0
- data/lib/dao/rails/lib/generators/dao/templates/dao.js +13 -6
- data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +75 -11
- data/lib/dao/result.rb +1 -26
- data/lib/dao/slug.rb +37 -8
- data/lib/dao/status.rb +4 -0
- data/lib/dao/support.rb +155 -0
- data/lib/dao/validations.rb +48 -157
- data/lib/dao/validations/callback.rb +30 -0
- data/lib/dao/validations/common.rb +322 -320
- data/lib/dao/validations/validator.rb +219 -0
- data/test/active_model_conducer_lint_test.rb +19 -0
- data/test/api_test.rb +261 -0
- data/test/conducer_test.rb +205 -0
- data/test/db.yml +9 -0
- data/test/form_test.rb +42 -0
- data/test/support_test.rb +52 -0
- data/test/testing.rb +145 -24
- data/test/validations_test.rb +156 -0
- metadata +138 -21
- data/TODO +0 -33
- data/a.rb +0 -80
- data/db/dao.yml +0 -5
- data/lib/dao/api/interfaces.rb +0 -306
- data/lib/dao/interface.rb +0 -28
- data/lib/dao/presenter.rb +0 -129
- data/lib/dao/rails/lib/generators/dao/api_generator.rb +0 -3
- data/lib/dao/validations/base.rb +0 -68
- data/test/dao_test.rb +0 -506
data/lib/dao/validations.rb
CHANGED
@@ -1,178 +1,69 @@
|
|
1
1
|
module Dao
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
class Callback < ::Proc
|
6
|
-
attr :options
|
2
|
+
module Validations
|
3
|
+
class Error < Dao::Error; end
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
5
|
+
Dao.load('validations/validator.rb')
|
6
|
+
Dao.load('validations/callback.rb')
|
7
|
+
Dao.load('validations/common.rb')
|
12
8
|
|
13
|
-
|
14
|
-
|
9
|
+
ClassMethods = proc do
|
10
|
+
def validator
|
11
|
+
@validator ||= Validator.new(self)
|
15
12
|
end
|
16
13
|
|
17
|
-
|
18
|
-
|
19
|
-
@chain = []
|
20
|
-
end
|
21
|
-
|
22
|
-
def add(callback)
|
23
|
-
@chain.push(callback)
|
24
|
-
end
|
25
|
-
|
26
|
-
def each(&block)
|
27
|
-
@chain.each(&block)
|
28
|
-
end
|
14
|
+
def validator=(validator)
|
15
|
+
@validator = validator
|
29
16
|
end
|
30
|
-
end
|
31
17
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
18
|
+
%w(
|
19
|
+
validations
|
20
|
+
validates
|
21
|
+
).each do |method|
|
22
|
+
module_eval <<-__, __FILE__, __LINE__
|
23
|
+
def self.#{ method }(*args, &block)
|
24
|
+
validator.#{ method }(*args, &block)
|
25
|
+
end
|
26
|
+
__
|
37
27
|
end
|
38
28
|
end
|
39
29
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
attr_accessor :errors
|
44
|
-
attr_accessor :status
|
45
|
-
attr_accessor :ran
|
46
|
-
|
47
|
-
def initialize(*args, &block)
|
48
|
-
@map = args.first.is_a?(Map) ? args.shift : Map.new
|
49
|
-
options = Map.options_for!(args)
|
50
|
-
|
51
|
-
@errors = options[:errors] || Errors.new
|
52
|
-
@status = options[:status] || Status.default
|
53
|
-
|
54
|
-
@map.extend(InstanceExec) unless @map.respond_to?(:instance_exec)
|
55
|
-
@ran = false
|
56
|
-
super
|
57
|
-
end
|
58
|
-
alias_method('ran?', 'ran')
|
59
|
-
|
60
|
-
def errors
|
61
|
-
@map.errors
|
62
|
-
end
|
63
|
-
|
64
|
-
def run
|
65
|
-
previous_errors = []
|
66
|
-
new_errors = []
|
67
|
-
|
68
|
-
errors.each_message do |keys, message|
|
69
|
-
previous_errors.push([keys, message])
|
30
|
+
InstanceMethods = proc do
|
31
|
+
def validator
|
32
|
+
@validator ||= Validator.new(self)
|
70
33
|
end
|
71
|
-
errors.clear!
|
72
|
-
|
73
|
-
depth_first_each do |keys, chain|
|
74
|
-
chain.each do |callback|
|
75
|
-
next unless callback and callback.respond_to?(:to_proc)
|
76
|
-
|
77
|
-
number_of_errors = errors.size
|
78
|
-
value = @map.get(keys)
|
79
|
-
returned =
|
80
|
-
catch(:valid) do
|
81
|
-
args = [value, map].slice(0, callback.arity)
|
82
|
-
@map.instance_exec(*args, &callback)
|
83
|
-
end
|
84
|
-
|
85
|
-
case returned
|
86
|
-
when Hash
|
87
|
-
map = Dao.map(returned)
|
88
|
-
valid = map[:valid]
|
89
|
-
message = map[:message]
|
90
|
-
|
91
|
-
when TrueClass, FalseClass
|
92
|
-
valid = returned
|
93
|
-
message = nil
|
94
|
-
|
95
|
-
else
|
96
|
-
any_errors_added = errors.size > number_of_errors
|
97
|
-
valid = !any_errors_added
|
98
|
-
message = nil
|
99
|
-
end
|
100
34
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
unless valid
|
105
|
-
new_errors.push([keys, message])
|
106
|
-
else
|
107
|
-
new_errors.push([keys, Cleared])
|
108
|
-
end
|
109
|
-
end
|
35
|
+
def validator=(validator)
|
36
|
+
@validator = validator
|
110
37
|
end
|
111
38
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
39
|
+
%w(
|
40
|
+
validations
|
41
|
+
validates
|
42
|
+
validated?
|
43
|
+
validated!
|
44
|
+
validate
|
45
|
+
validate!
|
46
|
+
run_validations!
|
47
|
+
valid!
|
48
|
+
forcing_validity?
|
49
|
+
valid?
|
50
|
+
errors
|
51
|
+
status
|
52
|
+
).each do |method|
|
53
|
+
module_eval <<-__, __FILE__, __LINE__
|
54
|
+
def #{ method }(*args, &block)
|
55
|
+
validator.#{ method }(*args, &block)
|
56
|
+
end
|
57
|
+
__
|
120
58
|
end
|
121
|
-
|
122
|
-
@ran = true
|
123
|
-
return self
|
124
|
-
end
|
125
|
-
Cleared = 'Cleared'.freeze unless defined?(Cleared)
|
126
|
-
|
127
|
-
def run!
|
128
|
-
errors.clear!
|
129
|
-
run
|
130
|
-
end
|
131
|
-
|
132
|
-
def each(&block)
|
133
|
-
depth_first_each(&block)
|
134
59
|
end
|
135
60
|
|
136
|
-
def
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
alias_method('count', 'size')
|
142
|
-
alias_method('length', 'size')
|
143
|
-
|
144
|
-
def add(*args, &block)
|
145
|
-
options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
|
146
|
-
block = args.pop if args.last.respond_to?(:call)
|
147
|
-
block ||= NotBlank
|
148
|
-
callback = Callback.new(options, &block)
|
149
|
-
set(args => Callback::Chain.new) unless has?(args)
|
150
|
-
get(args).add(callback)
|
151
|
-
callback
|
152
|
-
end
|
153
|
-
NotBlank = lambda{|value| !value.to_s.strip.empty?} unless defined?(NotBlank)
|
154
|
-
end
|
155
|
-
|
156
|
-
Dao.load('validations/base.rb')
|
157
|
-
Dao.load('validations/common.rb')
|
158
|
-
|
159
|
-
module Validations::Mixin
|
160
|
-
def self.included(other)
|
161
|
-
other.module_eval do
|
162
|
-
include Validations::Base
|
163
|
-
include Validations::Common
|
164
|
-
end
|
61
|
+
def Validations.included(other)
|
62
|
+
other.send(:instance_eval, &ClassMethods)
|
63
|
+
other.send(:class_eval, &InstanceMethods)
|
64
|
+
other.send(:include, Common)
|
65
|
+
other.send(:extend, Common)
|
165
66
|
super
|
166
67
|
end
|
167
|
-
|
168
|
-
def self.list
|
169
|
-
@list ||= (
|
170
|
-
c = Class.new
|
171
|
-
a = c.instance_methods
|
172
|
-
c.class_eval{ include Validations::Mixin }
|
173
|
-
b = c.instance_methods
|
174
|
-
b - a
|
175
|
-
)
|
176
|
-
end
|
177
68
|
end
|
178
69
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Dao
|
2
|
+
module Validations
|
3
|
+
class Callback < ::Proc
|
4
|
+
attr :options
|
5
|
+
|
6
|
+
def initialize(options = {}, &block)
|
7
|
+
@options = Map.for(options || {})
|
8
|
+
super(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def block
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
class Chain
|
16
|
+
def initialize
|
17
|
+
@chain = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(callback)
|
21
|
+
@chain.push(callback)
|
22
|
+
end
|
23
|
+
|
24
|
+
def each(&block)
|
25
|
+
@chain.each(&block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,457 +1,459 @@
|
|
1
1
|
module Dao
|
2
|
-
module Validations
|
3
|
-
|
4
|
-
|
2
|
+
module Validations
|
3
|
+
module Common
|
4
|
+
def validates_length_of(*args)
|
5
|
+
options = Map.options_for!(args)
|
5
6
|
|
6
|
-
|
7
|
+
message = options[:message]
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
if options[:in].is_a?(Range)
|
10
|
+
options[:minimum] = options[:in].begin
|
11
|
+
options[:maximum] = options[:in].end
|
12
|
+
end
|
13
|
+
minimum = options[:minimum] || 1
|
14
|
+
maximum = options[:maximum]
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
too_short = options[:too_short] || message || 'is too short'
|
17
|
+
too_long = options[:too_long] || message || 'is too long'
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
allow_nil = options[:allow_nil]
|
20
|
+
allow_blank = options[:allow_blank]
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
minimum = Float(minimum)
|
23
|
+
maximum = Float(maximum) if maximum
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
block =
|
26
|
+
lambda do |value|
|
27
|
+
m = Map(:valid => true)
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
if value.nil? and allow_nil
|
30
|
+
m[:valid] = true
|
31
|
+
throw(:validation, m)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
+
value = value.to_s.strip
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
if value.empty? and allow_blank
|
37
|
+
m[:valid] = true
|
38
|
+
throw(:validation, m)
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
if value.size < minimum
|
42
|
+
m[:message] = too_short
|
43
|
+
m[:valid] = false
|
44
|
+
throw(:validation, m)
|
45
|
+
end
|
46
|
+
|
47
|
+
if(maximum and(value.size > maximum))
|
48
|
+
m[:message] = too_long
|
49
|
+
m[:valid] = false
|
50
|
+
throw(:validation, m)
|
51
|
+
end
|
45
52
|
|
46
|
-
|
47
|
-
m[:message] = too_long
|
48
|
-
m[:valid] = false
|
49
|
-
throw(:valid, m)
|
53
|
+
m
|
50
54
|
end
|
51
55
|
|
52
|
-
|
53
|
-
|
56
|
+
validates(*args, &block)
|
57
|
+
end
|
54
58
|
|
55
|
-
|
56
|
-
|
59
|
+
def validates_word_count_of(*args)
|
60
|
+
options = Map.options_for!(args)
|
57
61
|
|
58
|
-
|
59
|
-
options = Dao.options_for!(args)
|
62
|
+
message = options[:message]
|
60
63
|
|
61
|
-
|
64
|
+
if options[:in].is_a?(Range)
|
65
|
+
options[:minimum] = options[:in].begin
|
66
|
+
options[:maximum] = options[:in].end
|
67
|
+
end
|
68
|
+
minimum = options[:minimum] || 1
|
69
|
+
maximum = options[:maximum]
|
62
70
|
|
63
|
-
|
64
|
-
|
65
|
-
options[:maximum] = options[:in].end
|
66
|
-
end
|
67
|
-
minimum = options[:minimum] || 1
|
68
|
-
maximum = options[:maximum]
|
71
|
+
too_short = options[:too_short] || message || 'is too short'
|
72
|
+
too_long = options[:too_long] || message || 'is too long'
|
69
73
|
|
70
|
-
|
71
|
-
|
74
|
+
allow_nil = options[:allow_nil]
|
75
|
+
allow_blank = options[:allow_blank]
|
72
76
|
|
73
|
-
|
74
|
-
|
77
|
+
minimum = Float(minimum)
|
78
|
+
maximum = Float(maximum) if maximum
|
75
79
|
|
76
|
-
|
77
|
-
|
80
|
+
block =
|
81
|
+
lambda do |value|
|
82
|
+
m = Map(:valid => true)
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
84
|
+
if value.nil? and allow_nil
|
85
|
+
m[:valid] = true
|
86
|
+
throw(:validation, m)
|
87
|
+
end
|
82
88
|
|
83
|
-
|
84
|
-
m[:valid] = true
|
85
|
-
throw(:valid, m)
|
86
|
-
end
|
89
|
+
value = value.to_s.strip
|
87
90
|
|
88
|
-
|
91
|
+
if value.empty? and allow_blank
|
92
|
+
m[:valid] = true
|
93
|
+
throw(:validation, m)
|
94
|
+
end
|
89
95
|
|
90
|
-
|
91
|
-
m[:valid] = true
|
92
|
-
throw(:valid, m)
|
93
|
-
end
|
96
|
+
words = value.split(/\s+/)
|
94
97
|
|
95
|
-
|
98
|
+
if words.size < minimum
|
99
|
+
m[:message] = too_short
|
100
|
+
m[:valid] = false
|
101
|
+
throw(:validation, m)
|
102
|
+
end
|
96
103
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
104
|
+
if(maximum and(words.size > maximum))
|
105
|
+
m[:message] = too_long
|
106
|
+
m[:valid] = false
|
107
|
+
throw(:validation, m)
|
108
|
+
end
|
102
109
|
|
103
|
-
|
104
|
-
m[:message] = too_long
|
105
|
-
m[:valid] = false
|
106
|
-
throw(:valid, m)
|
110
|
+
m
|
107
111
|
end
|
108
112
|
|
109
|
-
|
110
|
-
|
113
|
+
validates(*args, &block)
|
114
|
+
end
|
111
115
|
|
112
|
-
|
113
|
-
|
116
|
+
def validates_as_email(*args)
|
117
|
+
options = Map.options_for!(args)
|
114
118
|
|
115
|
-
|
116
|
-
options = Dao.options_for!(args)
|
119
|
+
message = options[:message] || "doesn't look like an email (username@domain.com)"
|
117
120
|
|
118
|
-
|
121
|
+
allow_nil = options[:allow_nil]
|
122
|
+
allow_blank = options[:allow_blank]
|
119
123
|
|
120
|
-
|
121
|
-
|
124
|
+
block =
|
125
|
+
lambda do |value|
|
126
|
+
m = Map(:valid => true)
|
122
127
|
|
123
|
-
|
124
|
-
|
125
|
-
|
128
|
+
if value.nil? and allow_nil
|
129
|
+
m[:valid] = true
|
130
|
+
throw(:validation, m)
|
131
|
+
end
|
126
132
|
|
127
|
-
|
128
|
-
m[:valid] = true
|
129
|
-
throw(:valid, m)
|
130
|
-
end
|
133
|
+
value = value.to_s.strip
|
131
134
|
|
132
|
-
|
135
|
+
if value.empty? and allow_blank
|
136
|
+
m[:valid] = true
|
137
|
+
throw(:validation, m)
|
138
|
+
end
|
133
139
|
|
134
|
-
|
135
|
-
m[:valid] = true
|
136
|
-
throw(:valid, m)
|
137
|
-
end
|
140
|
+
parts = value.split(/@/)
|
138
141
|
|
139
|
-
|
142
|
+
unless parts.size == 2
|
143
|
+
m[:valid] = false
|
144
|
+
throw(:validation, m)
|
145
|
+
end
|
140
146
|
|
141
|
-
|
142
|
-
m[:valid] = false
|
143
|
-
throw(:valid, m)
|
147
|
+
m
|
144
148
|
end
|
145
149
|
|
146
|
-
|
147
|
-
|
150
|
+
args.push(:message => message)
|
151
|
+
validates(*args, &block)
|
152
|
+
end
|
148
153
|
|
149
|
-
args
|
150
|
-
|
151
|
-
end
|
154
|
+
def validates_as_url(*args)
|
155
|
+
options = Map.options_for!(args)
|
152
156
|
|
153
|
-
|
154
|
-
options = Dao.options_for!(args)
|
157
|
+
message = options[:message] || "doesn't look like a url (http://domain.com)"
|
155
158
|
|
156
|
-
|
159
|
+
allow_nil = options[:allow_nil]
|
160
|
+
allow_blank = options[:allow_blank]
|
157
161
|
|
158
|
-
|
159
|
-
|
162
|
+
block =
|
163
|
+
lambda do |value|
|
164
|
+
m = Map(:valid => true)
|
160
165
|
|
161
|
-
|
162
|
-
|
163
|
-
|
166
|
+
if value.nil? and allow_nil
|
167
|
+
m[:valid] = true
|
168
|
+
throw(:validation, m)
|
169
|
+
end
|
164
170
|
|
165
|
-
|
166
|
-
m[:valid] = true
|
167
|
-
throw(:valid, m)
|
168
|
-
end
|
171
|
+
value = value.to_s.strip
|
169
172
|
|
170
|
-
|
173
|
+
if value.empty? and allow_blank
|
174
|
+
m[:valid] = true
|
175
|
+
throw(:validation, m)
|
176
|
+
end
|
171
177
|
|
172
|
-
|
173
|
-
m[:valid] = true
|
174
|
-
throw(:valid, m)
|
175
|
-
end
|
178
|
+
parts = value.split(%r|://|)
|
176
179
|
|
177
|
-
|
180
|
+
unless parts.size >= 2
|
181
|
+
m[:valid] = false
|
182
|
+
throw(:validation, m)
|
183
|
+
end
|
178
184
|
|
179
|
-
|
180
|
-
m[:valid] = false
|
181
|
-
throw(:valid, m)
|
185
|
+
m
|
182
186
|
end
|
183
187
|
|
184
|
-
|
185
|
-
|
188
|
+
args.push(:message => message)
|
189
|
+
validates(*args, &block)
|
190
|
+
end
|
186
191
|
|
187
|
-
args
|
188
|
-
|
189
|
-
end
|
192
|
+
def validates_as_phone(*args)
|
193
|
+
options = Map.options_for!(args)
|
190
194
|
|
191
|
-
|
192
|
-
options = Dao.options_for!(args)
|
195
|
+
message = options[:message] || "doesn't look like a phone number (012.345.6789)"
|
193
196
|
|
194
|
-
|
197
|
+
allow_nil = options[:allow_nil]
|
198
|
+
allow_blank = options[:allow_blank]
|
195
199
|
|
196
|
-
|
197
|
-
|
200
|
+
block =
|
201
|
+
lambda do |value|
|
202
|
+
m = Map(:valid => true)
|
198
203
|
|
199
|
-
|
200
|
-
|
201
|
-
|
204
|
+
if value.nil? and allow_nil
|
205
|
+
m[:valid] = true
|
206
|
+
throw(:validation, m)
|
207
|
+
end
|
202
208
|
|
203
|
-
|
204
|
-
m[:valid] = true
|
205
|
-
throw(:valid, m)
|
206
|
-
end
|
209
|
+
value = value.to_s.strip
|
207
210
|
|
208
|
-
|
211
|
+
if value.empty? and allow_blank
|
212
|
+
m[:valid] = true
|
213
|
+
throw(:validation, m)
|
214
|
+
end
|
209
215
|
|
210
|
-
|
211
|
-
m[:valid] = true
|
212
|
-
throw(:valid, m)
|
213
|
-
end
|
216
|
+
parts = value.scan(/\d+/)
|
214
217
|
|
215
|
-
|
218
|
+
unless parts.size >= 1
|
219
|
+
m[:valid] = false
|
220
|
+
throw(:validation, m)
|
221
|
+
end
|
216
222
|
|
217
|
-
|
218
|
-
m[:valid] = false
|
219
|
-
throw(:valid, m)
|
223
|
+
m
|
220
224
|
end
|
221
225
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
args.push(:message => message)
|
226
|
-
validates(*args, &block)
|
227
|
-
end
|
226
|
+
args.push(:message => message)
|
227
|
+
validates(*args, &block)
|
228
|
+
end
|
228
229
|
|
229
|
-
|
230
|
-
|
230
|
+
def validates_presence_of(*args)
|
231
|
+
options = Map.options_for!(args)
|
231
232
|
|
232
|
-
|
233
|
+
message = options[:message] || 'is blank or missing'
|
233
234
|
|
234
|
-
|
235
|
-
|
235
|
+
allow_nil = options[:allow_nil]
|
236
|
+
allow_blank = options[:allow_blank]
|
236
237
|
|
237
|
-
|
238
|
-
|
239
|
-
|
238
|
+
block =
|
239
|
+
lambda do |value|
|
240
|
+
m = Map(:valid => true)
|
240
241
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
242
|
+
if value.nil?
|
243
|
+
unless allow_nil
|
244
|
+
m[:message] = message
|
245
|
+
m[:valid] = false
|
246
|
+
throw(:validation, m)
|
247
|
+
end
|
246
248
|
end
|
247
|
-
end
|
248
249
|
|
249
|
-
|
250
|
+
value = value.to_s.strip
|
250
251
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
252
|
+
if value.empty?
|
253
|
+
unless allow_blank
|
254
|
+
m[:message] = message
|
255
|
+
m[:valid] = false
|
256
|
+
throw(:validation, m)
|
257
|
+
end
|
256
258
|
end
|
257
|
-
end
|
258
259
|
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
validates(*args, &block)
|
263
|
-
end
|
260
|
+
m
|
261
|
+
end
|
264
262
|
|
265
|
-
|
266
|
-
|
267
|
-
list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
|
263
|
+
validates(*args, &block)
|
264
|
+
end
|
268
265
|
|
269
|
-
|
270
|
-
|
271
|
-
|
266
|
+
def validates_any_of(*args)
|
267
|
+
options = Map.options_for!(args)
|
268
|
+
list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
|
272
269
|
|
273
|
-
|
274
|
-
|
275
|
-
|
270
|
+
list.each do |args|
|
271
|
+
candidates = list.dup
|
272
|
+
candidates.delete(args)
|
276
273
|
|
277
|
-
|
274
|
+
message = options[:message] || "(or #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
|
275
|
+
allow_nil = options[:allow_nil]
|
276
|
+
allow_blank = options[:allow_blank]
|
278
277
|
|
279
|
-
|
280
|
-
lambda do |value|
|
281
|
-
m = Dao.map(:valid => true)
|
282
|
-
values = list.map{|key| result.get(key)}
|
283
|
-
valid = false
|
284
|
-
values.each do |val|
|
285
|
-
if val
|
286
|
-
valid = true
|
287
|
-
break
|
288
|
-
end
|
278
|
+
result = self.result
|
289
279
|
|
290
|
-
|
291
|
-
|
280
|
+
block =
|
281
|
+
lambda do |value|
|
282
|
+
m = Map(:valid => true)
|
283
|
+
values = list.map{|key| result.get(key)}
|
284
|
+
valid = false
|
285
|
+
values.each do |val|
|
286
|
+
if val
|
292
287
|
valid = true
|
293
288
|
break
|
294
289
|
end
|
295
|
-
end
|
296
290
|
|
297
|
-
|
291
|
+
if val.nil?
|
292
|
+
if allow_nil
|
293
|
+
valid = true
|
294
|
+
break
|
295
|
+
end
|
296
|
+
end
|
298
297
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
298
|
+
val = val.to_s.strip
|
299
|
+
|
300
|
+
if val.empty?
|
301
|
+
if allow_blank
|
302
|
+
valid = true
|
303
|
+
break
|
304
|
+
end
|
303
305
|
end
|
304
306
|
end
|
305
|
-
end
|
306
307
|
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
308
|
+
unless valid
|
309
|
+
if value.nil?
|
310
|
+
unless allow_nil
|
311
|
+
m[:message] = message
|
312
|
+
m[:valid] = false
|
313
|
+
throw(:validation, m)
|
314
|
+
end
|
313
315
|
end
|
314
|
-
end
|
315
316
|
|
316
|
-
|
317
|
+
value = value.to_s.strip
|
317
318
|
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
319
|
+
if value.empty?
|
320
|
+
unless allow_blank
|
321
|
+
m[:message] = message
|
322
|
+
m[:valid] = false
|
323
|
+
throw(:validation, m)
|
324
|
+
end
|
323
325
|
end
|
324
326
|
end
|
325
|
-
end
|
326
327
|
|
327
|
-
|
328
|
-
|
329
|
-
|
328
|
+
m
|
329
|
+
end
|
330
|
+
validates(*args, &block)
|
331
|
+
end
|
330
332
|
end
|
331
|
-
end
|
332
333
|
|
333
|
-
|
334
|
-
|
335
|
-
|
334
|
+
def validates_all_of(*args)
|
335
|
+
options = Map.options_for!(args)
|
336
|
+
list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
|
336
337
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
|
342
|
-
allow_nil = options[:allow_nil]
|
343
|
-
allow_blank = options[:allow_blank]
|
338
|
+
list.each do |args|
|
339
|
+
candidates = list.dup
|
340
|
+
candidates.delete(args)
|
344
341
|
|
345
|
-
|
342
|
+
message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
|
343
|
+
allow_nil = options[:allow_nil]
|
344
|
+
allow_blank = options[:allow_blank]
|
346
345
|
|
347
|
-
|
348
|
-
lambda do |value|
|
349
|
-
m = Dao.map(:valid => true)
|
346
|
+
result = self.result
|
350
347
|
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
if val
|
355
|
-
break
|
356
|
-
end
|
348
|
+
block =
|
349
|
+
lambda do |value|
|
350
|
+
m = Map(:valid => true)
|
357
351
|
|
358
|
-
|
359
|
-
|
360
|
-
|
352
|
+
values = list.map{|key| result.get(key)}
|
353
|
+
valid = true
|
354
|
+
values.each do |val|
|
355
|
+
if val
|
361
356
|
break
|
362
357
|
end
|
363
|
-
end
|
364
358
|
|
365
|
-
|
359
|
+
if val.nil?
|
360
|
+
unless allow_nil
|
361
|
+
valid = false
|
362
|
+
break
|
363
|
+
end
|
364
|
+
end
|
366
365
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
366
|
+
val = val.to_s.strip
|
367
|
+
|
368
|
+
if val.empty?
|
369
|
+
unless allow_blank
|
370
|
+
valid = false
|
371
|
+
break
|
372
|
+
end
|
371
373
|
end
|
372
374
|
end
|
373
|
-
end
|
374
375
|
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
376
|
+
unless valid
|
377
|
+
if value.nil?
|
378
|
+
unless allow_nil
|
379
|
+
m[:message] = message
|
380
|
+
m[:valid] = false
|
381
|
+
throw(:validation, m)
|
382
|
+
end
|
381
383
|
end
|
382
|
-
end
|
383
384
|
|
384
|
-
|
385
|
+
value = value.to_s.strip
|
385
386
|
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
387
|
+
if value.empty?
|
388
|
+
unless allow_blank
|
389
|
+
m[:message] = message
|
390
|
+
m[:valid] = false
|
391
|
+
throw(:validation, m)
|
392
|
+
end
|
391
393
|
end
|
392
394
|
end
|
393
|
-
end
|
394
395
|
|
395
|
-
|
396
|
-
|
397
|
-
|
396
|
+
m
|
397
|
+
end
|
398
|
+
validates(*args, &block)
|
399
|
+
end
|
398
400
|
end
|
399
|
-
end
|
400
401
|
|
401
|
-
|
402
|
-
|
402
|
+
def validates_confirmation_of(*args)
|
403
|
+
options = Map.options_for!(args)
|
403
404
|
|
404
405
|
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
406
|
+
confirmation_key = args.map{|k| k.to_s}
|
407
|
+
last = confirmation_key.pop
|
408
|
+
last = "#{ last }_confirmation" unless last =~ /_confirmation$/
|
409
|
+
confirmation_key.push(last)
|
409
410
|
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
411
|
+
key = args.map{|k| k.to_s}
|
412
|
+
last = key.pop
|
413
|
+
last.sub!(/_confirmation$/, '')
|
414
|
+
key.push(last)
|
414
415
|
|
415
|
-
|
416
|
+
message = options[:message] || "does not match #{ key.join('.') }"
|
416
417
|
|
417
|
-
|
418
|
-
|
418
|
+
allow_nil = options[:allow_nil]
|
419
|
+
allow_blank = options[:allow_blank]
|
419
420
|
|
420
|
-
|
421
|
-
|
422
|
-
|
421
|
+
block =
|
422
|
+
lambda do |value|
|
423
|
+
m = Map(:valid => true)
|
423
424
|
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
425
|
+
if value.nil?
|
426
|
+
unless allow_nil
|
427
|
+
m[:message] = message
|
428
|
+
m[:valid] = false
|
429
|
+
throw(:validation, m)
|
430
|
+
end
|
429
431
|
end
|
430
|
-
end
|
431
432
|
|
432
|
-
|
433
|
+
value = value.to_s.strip
|
433
434
|
|
434
|
-
|
435
|
-
|
435
|
+
if value.empty?
|
436
|
+
unless allow_blank
|
437
|
+
m[:message] = message
|
438
|
+
m[:valid] = false
|
439
|
+
throw(:validation, m)
|
440
|
+
end
|
441
|
+
end
|
442
|
+
|
443
|
+
target = get(key).to_s.strip
|
444
|
+
confirmed = target == value
|
445
|
+
|
446
|
+
unless confirmed
|
436
447
|
m[:message] = message
|
437
448
|
m[:valid] = false
|
438
|
-
throw(:
|
449
|
+
throw(:validation, m)
|
439
450
|
end
|
440
|
-
end
|
441
451
|
|
442
|
-
|
443
|
-
confirmed = target == value
|
444
|
-
|
445
|
-
unless confirmed
|
446
|
-
m[:message] = message
|
447
|
-
m[:valid] = false
|
448
|
-
throw(:valid, m)
|
452
|
+
m
|
449
453
|
end
|
450
454
|
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
validates(confirmation_key, &block)
|
455
|
+
validates(confirmation_key, &block)
|
456
|
+
end
|
455
457
|
end
|
456
458
|
end
|
457
459
|
|