dao 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +96 -17
- data/Rakefile +92 -43
- data/dao.gemspec +12 -8
- data/lib/dao.rb +33 -20
- data/lib/dao/active_record.rb +51 -43
- data/lib/dao/api.rb +1 -1
- data/lib/dao/api/context.rb +6 -6
- data/lib/dao/api/dsl.rb +2 -2
- data/lib/dao/api/interfaces.rb +232 -0
- data/lib/dao/api/modes.rb +9 -6
- data/lib/dao/data.rb +7 -0
- data/lib/dao/errors.rb +27 -30
- data/lib/dao/form.rb +52 -28
- data/lib/dao/instance_exec.rb +37 -0
- data/lib/dao/interface.rb +16 -0
- data/lib/dao/params.rb +48 -24
- data/lib/dao/path.rb +1 -1
- data/lib/dao/rails.rb +5 -2
- data/lib/dao/rails/app/api.rb +1 -1
- data/lib/dao/rails/lib/generators/dao/dao_generator.rb +12 -0
- data/lib/dao/rails/lib/generators/dao/templates/api.rb +7 -1
- data/lib/dao/rails/lib/generators/dao/templates/api_controller.rb +25 -0
- data/lib/dao/rails/lib/generators/dao/templates/dao.css +27 -0
- data/lib/dao/rails/lib/generators/dao/templates/dao.js +149 -0
- data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +10 -0
- data/lib/dao/result.rb +53 -14
- data/lib/dao/status.rb +162 -1
- data/lib/dao/stdext.rb +6 -0
- data/lib/dao/support.rb +10 -2
- data/lib/dao/validations.rb +338 -14
- data/sample/rails_app/pubic/javascripts/dao.js +148 -0
- data/test/dao_test.rb +18 -19
- metadata +29 -18
- data/TODO +0 -37
- data/db/dao.yml +0 -8
@@ -0,0 +1,10 @@
|
|
1
|
+
module DaoHelper
|
2
|
+
def render_dao(result, *args, &block)
|
3
|
+
if result.status =~ 200 or result.status == 420
|
4
|
+
render(*args, &block)
|
5
|
+
else
|
6
|
+
render(:text => result.status, :status => result.status.code)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
ApplicationController.send(:include, DaoHelper)
|
data/lib/dao/result.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
module Dao
|
2
2
|
class Result < ::Map
|
3
|
+
include Dao::InstanceExec
|
4
|
+
|
3
5
|
attr_accessor :api
|
4
|
-
attr_accessor :
|
6
|
+
attr_accessor :interface
|
7
|
+
attr_accessor :mode
|
5
8
|
attr_accessor :params
|
6
9
|
attr_accessor :validations
|
7
10
|
attr_accessor :form
|
11
|
+
attr_accessor :forcing_validity
|
8
12
|
|
9
13
|
def Result.for(*args, &block)
|
10
14
|
new(*args, &block)
|
@@ -20,24 +24,28 @@ module Dao
|
|
20
24
|
data = Data.new
|
21
25
|
|
22
26
|
api = options[:api]
|
23
|
-
|
27
|
+
interface = options[:interface]
|
24
28
|
params = options[:params] || Params.new
|
29
|
+
mode = options[:mode] || (api ? api.mode : Mode.default)
|
25
30
|
|
26
|
-
|
31
|
+
params.result = self
|
32
|
+
path = interface.path if interface
|
27
33
|
|
28
34
|
form = Form.for(self)
|
29
35
|
validations = Validations.for(self)
|
30
36
|
|
31
37
|
self[:path] = path
|
32
38
|
self[:status] = status
|
39
|
+
self[:mode] = mode
|
33
40
|
self[:errors] = errors
|
34
41
|
self[:data] = data
|
35
42
|
|
36
43
|
@api = api
|
37
|
-
@
|
44
|
+
@interface = interface
|
38
45
|
@params = params
|
39
46
|
@form = form
|
40
47
|
@validations = validations
|
48
|
+
@forcing_validity = false
|
41
49
|
end
|
42
50
|
|
43
51
|
def path
|
@@ -52,6 +60,14 @@ module Dao
|
|
52
60
|
status(value)
|
53
61
|
end
|
54
62
|
|
63
|
+
def mode(*args)
|
64
|
+
self[:mode] = Mode.for(*args) unless args.empty?
|
65
|
+
self[:mode]
|
66
|
+
end
|
67
|
+
def mode=(value)
|
68
|
+
mode(value)
|
69
|
+
end
|
70
|
+
|
55
71
|
def errors
|
56
72
|
self[:errors]
|
57
73
|
end
|
@@ -60,28 +76,51 @@ module Dao
|
|
60
76
|
self[:data]
|
61
77
|
end
|
62
78
|
|
63
|
-
def
|
64
|
-
|
79
|
+
def is_valid=(boolean)
|
80
|
+
@is_valid = !!boolean
|
81
|
+
end
|
82
|
+
|
83
|
+
def is_valid(*bool)
|
84
|
+
@is_valid ||= nil
|
85
|
+
@is_valid = !!bool.first unless bool.empty?
|
86
|
+
@is_valid
|
87
|
+
end
|
88
|
+
|
89
|
+
def valid!
|
90
|
+
@forcing_validity = true
|
91
|
+
end
|
92
|
+
|
93
|
+
def valid?
|
94
|
+
if @forcing_validity
|
95
|
+
true
|
96
|
+
else
|
97
|
+
validate unless validations.ran?
|
98
|
+
errors.empty? and status.ok?
|
99
|
+
end
|
65
100
|
end
|
66
101
|
|
67
102
|
def validate(*args, &block)
|
68
103
|
if !args.empty?
|
69
|
-
|
104
|
+
validations.add(*args, &block)
|
70
105
|
else
|
71
106
|
validations.run
|
72
|
-
|
107
|
+
status(420) if(status.ok? and !errors.empty?)
|
73
108
|
errors.empty? and status.ok?
|
74
109
|
end
|
75
110
|
end
|
76
111
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
112
|
+
def validate!(*args, &block)
|
113
|
+
if !args.empty?
|
114
|
+
validations.add(*args, &block)
|
115
|
+
end
|
116
|
+
@forcing_validity = false
|
82
117
|
validations.run!
|
83
|
-
|
118
|
+
status(420) if(status.ok? and !errors.empty?)
|
84
119
|
throw(:result, nil) unless(errors.empty? and status.ok?)
|
85
120
|
end
|
121
|
+
|
122
|
+
def validates(*args, &block)
|
123
|
+
validations.add(*args, &block)
|
124
|
+
end
|
86
125
|
end
|
87
126
|
end
|
data/lib/dao/status.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Dao
|
2
2
|
class Status < ::String
|
3
|
+
## http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
|
4
|
+
#
|
3
5
|
Code2Message = (
|
4
6
|
{
|
5
7
|
100 => "Continue",
|
@@ -109,10 +111,12 @@ module Dao
|
|
109
111
|
}) unless defined?(Groups)
|
110
112
|
|
111
113
|
Groups.each do |code, group|
|
112
|
-
module_eval <<-__
|
114
|
+
module_eval <<-__, __FILE__, __LINE__ -1
|
115
|
+
|
113
116
|
def #{ group }?()
|
114
117
|
#{ code } == @group
|
115
118
|
end
|
119
|
+
|
116
120
|
__
|
117
121
|
end
|
118
122
|
|
@@ -130,6 +134,15 @@ module Dao
|
|
130
134
|
end
|
131
135
|
alias_method 'error?', 'bad?'
|
132
136
|
|
137
|
+
def =~(other)
|
138
|
+
begin
|
139
|
+
other = Status.for(other)
|
140
|
+
self.group == other.group
|
141
|
+
rescue
|
142
|
+
false
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
133
146
|
def ==(other)
|
134
147
|
begin
|
135
148
|
other = Status.for(other)
|
@@ -221,3 +234,151 @@ module Dao
|
|
221
234
|
end
|
222
235
|
end
|
223
236
|
end
|
237
|
+
|
238
|
+
__END__
|
239
|
+
|
240
|
+
|
241
|
+
### ref: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
|
242
|
+
|
243
|
+
1xx Informational
|
244
|
+
|
245
|
+
Request received, continuing process.[2]
|
246
|
+
This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line. Since HTTP/1.0 did not define any 1xx status codes, servers must not send a 1xx response to an HTTP/1.0 client except under experimental conditions.
|
247
|
+
100 Continue
|
248
|
+
This means that the server has received the request headers, and that the client should proceed to send the request body (in the case of a request for which a body needs to be sent; for example, a POST request). If the request body is large, sending it to a server when a request has already been rejected based upon inappropriate headers is inefficient. To have a server check if the request could be accepted based on the request's headers alone, a client must send Expect: 100-continue as a header in its initial request[2] and check if a 100 Continue status code is received in response before continuing (or receive 417 Expectation Failed and not continue).[2]
|
249
|
+
101 Switching Protocols
|
250
|
+
This means the requester has asked the server to switch protocols and the server is acknowledging that it will do so.[2]
|
251
|
+
102 Processing (WebDAV) (RFC 2518)
|
252
|
+
As a WebDAV request may contain many sub-requests involving file operations, it may take a long time to complete the request. This code indicates that the server has received and is processing the request, but no response is available yet.[3] This prevents the client from timing out and assuming the request was lost.
|
253
|
+
[edit]
|
254
|
+
|
255
|
+
|
256
|
+
2xx Success
|
257
|
+
|
258
|
+
This class of status codes indicates the action requested by the client was received, understood, accepted and processed successfully.
|
259
|
+
200 OK
|
260
|
+
Standard response for successful HTTP requests. The actual response will depend on the request method used. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request the response will contain an entity describing or containing the result of the action.[2]
|
261
|
+
201 Created
|
262
|
+
The request has been fulfilled and resulted in a new resource being created.[2]
|
263
|
+
202 Accepted
|
264
|
+
The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.[2]
|
265
|
+
203 Non-Authoritative Information (since HTTP/1.1)
|
266
|
+
The server successfully processed the request, but is returning information that may be from another source.[2]
|
267
|
+
204 No Content
|
268
|
+
The server successfully processed the request, but is not returning any content.[2]
|
269
|
+
205 Reset Content
|
270
|
+
The server successfully processed the request, but is not returning any content. Unlike a 204 response, this response requires that the requester reset the document view.[2]
|
271
|
+
206 Partial Content
|
272
|
+
The server is delivering only part of the resource due to a range header sent by the client. The range header is used by tools like wget to enable resuming of interrupted downloads, or split a download into multiple simultaneous streams.[2]
|
273
|
+
207 Multi-Status (WebDAV) (RFC 4918)
|
274
|
+
The message body that follows is an XML message and can contain a number of separate response codes, depending on how many sub-requests were made.[4]
|
275
|
+
[edit]
|
276
|
+
|
277
|
+
|
278
|
+
3xx Redirection
|
279
|
+
|
280
|
+
The client must take additional action to complete the request.[2]
|
281
|
+
This class of status code indicates that further action needs to be taken by the user agent in order to fulfil the request. The action required may be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD. A user agent should not automatically redirect a request more than five times, since such redirections usually indicate an infinite loop.
|
282
|
+
300 Multiple Choices
|
283
|
+
Indicates multiple options for the resource that the client may follow. It, for instance, could be used to present different format options for video, list files with different extensions, or word sense disambiguation.[2]
|
284
|
+
301 Moved Permanently
|
285
|
+
This and all future requests should be directed to the given URI.[2]
|
286
|
+
302 Found
|
287
|
+
This is the most popular redirect code[citation needed], but also an example of industrial practice contradicting the standard.[2] HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"),[5] but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours. However, the majority of Web applications and frameworks still use the 302 status code as if it were the 303[6].
|
288
|
+
303 See Other (since HTTP/1.1)
|
289
|
+
The response to the request can be found under another URI using a GET method. When received in response to a PUT, it should be assumed that the server has received the data and the redirect should be issued with a separate GET message.[2]
|
290
|
+
304 Not Modified
|
291
|
+
Indicates the resource has not been modified since last requested.[2] Typically, the HTTP client provides a header like the If-Modified-Since header to provide a time against which to compare. Using this saves bandwidth and reprocessing on both the server and client, as only the header data must be sent and received in comparison to the entirety of the page being re-processed by the server, then sent again using more bandwidth of the server and client.
|
292
|
+
305 Use Proxy (since HTTP/1.1)
|
293
|
+
Many HTTP clients (such as Mozilla[7] and Internet Explorer) do not correctly handle responses with this status code, primarily for security reasons.[2]
|
294
|
+
306 Switch Proxy
|
295
|
+
No longer used.[2]
|
296
|
+
307 Temporary Redirect (since HTTP/1.1)
|
297
|
+
In this occasion, the request should be repeated with another URI, but future requests can still use the original URI.[2] In contrast to 303, the request method should not be changed when reissuing the original request. For instance, a POST request must be repeated using another POST request.
|
298
|
+
[edit]
|
299
|
+
|
300
|
+
|
301
|
+
4xx Client Error
|
302
|
+
|
303
|
+
The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents should display any included entity to the user. These are typically the most common error codes encountered while online.
|
304
|
+
400 Bad Request
|
305
|
+
The request cannot be fulfilled due to bad syntax.[2]
|
306
|
+
401 Unauthorized
|
307
|
+
Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided.[2] The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.
|
308
|
+
402 Payment Required
|
309
|
+
Reserved for future use.[2] The original intention was that this code might be used as part of some form of digital cash or micropayment scheme, but that has not happened, and this code is not usually used. As an example of its use, however, Apple's MobileMe service generates a 402 error ("httpStatusCode:402" in the Mac OS X Console log) if the MobileMe account is delinquent.
|
310
|
+
403 Forbidden
|
311
|
+
The request was a legal request, but the server is refusing to respond to it.[2] Unlike a 401 Unauthorized response, authenticating will make no difference.[2]
|
312
|
+
404 Not Found
|
313
|
+
The requested resource could not be found but may be available again in the future.[2] Subsequent requests by the client are permissible.
|
314
|
+
405 Method Not Allowed
|
315
|
+
A request was made of a resource using a request method not supported by that resource;[2] for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.
|
316
|
+
406 Not Acceptable
|
317
|
+
The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.[2]
|
318
|
+
407 Proxy Authentication Required[2]
|
319
|
+
408 Request Timeout
|
320
|
+
The server timed out waiting for the request.[2] According to W3 HTTP specifications: "The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time."
|
321
|
+
409 Conflict
|
322
|
+
Indicates that the request could not be processed because of conflict in the request, such as an edit conflict.[2]
|
323
|
+
410 Gone
|
324
|
+
Indicates that the resource requested is no longer available and will not be available again.[2]This should be used when a resource has been intentionally removed and the resource should be purged. Upon receiving a 410 status code, the client should not request the resource again in the future. Clients such as search engines should remove the resource from their indices. Most use cases do not require clients and search engines to purge the resource, and a "404 Not Found" may be used instead.
|
325
|
+
411 Length Required
|
326
|
+
The request did not specify the length of its content, which is required by the requested resource.[2]
|
327
|
+
412 Precondition Failed
|
328
|
+
The server does not meet one of the preconditions that the requester put on the request.[2]
|
329
|
+
413 Request Entity Too Large
|
330
|
+
The request is larger than the server is willing or able to process.[2]
|
331
|
+
414 Request-URI Too Long
|
332
|
+
The URI provided was too long for the server to process.[2]
|
333
|
+
415 Unsupported Media Type
|
334
|
+
The request entity has a media type which the server or resource does not support.[2] For example the client uploads an image as image/svg+xml, but the server requires that images use a different format.
|
335
|
+
416 Requested Range Not Satisfiable
|
336
|
+
The client has asked for a portion of the file, but the server cannot supply that portion.[2] For example, if the client asked for a part of the file that lies beyond the end of the file.
|
337
|
+
417 Expectation Failed
|
338
|
+
The server cannot meet the requirements of the Expect request-header field.[2]
|
339
|
+
418 I'm a teapot
|
340
|
+
This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol, and is not expected to be implemented by actual HTTP servers.
|
341
|
+
422 Unprocessable Entity (WebDAV) (RFC 4918)
|
342
|
+
The request was well-formed but was unable to be followed due to semantic errors.[4]
|
343
|
+
423 Locked (WebDAV) (RFC 4918)
|
344
|
+
The resource that is being accessed is locked[4]
|
345
|
+
424 Failed Dependency (WebDAV) (RFC 4918)
|
346
|
+
The request failed due to failure of a previous request (e.g. a PROPPATCH).[4]
|
347
|
+
425 Unordered Collection (RFC 3648)
|
348
|
+
Defined in drafts of "WebDAV Advanced Collections Protocol",[8] but not present in "Web Distributed Authoring and Versioning (WebDAV) Ordered Collections Protocol".[9]
|
349
|
+
444 No Response
|
350
|
+
An Nginx HTTP server extension. The server returns no information to the client and closes the connection (useful as a deterrent for malware).
|
351
|
+
426 Upgrade Required (RFC 2817)
|
352
|
+
The client should switch to a different protocol such as TLS/1.0.[10]
|
353
|
+
449 Retry With
|
354
|
+
A Microsoft extension. The request should be retried after performing the appropriate action.[11]
|
355
|
+
450 Blocked by Windows Parental Controls
|
356
|
+
A Microsoft extension. This error is given when Windows Parental Controls are turned on and are blocking access to the given webpage.[12]
|
357
|
+
499 Client Closed Request
|
358
|
+
An Nginx HTTP server extension. This code is introduced to log the case when the connection is closed by client while HTTP server is processing its request, making server unable to send the HTTP header back.[13]
|
359
|
+
[edit]
|
360
|
+
|
361
|
+
|
362
|
+
5xx Server Error
|
363
|
+
|
364
|
+
The server failed to fulfill an apparently valid request.[2]
|
365
|
+
Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and indicate whether it is a temporary or permanent condition. Likewise, user agents should display any included entity to the user. These response codes are applicable to any request method.
|
366
|
+
500 Internal Server Error
|
367
|
+
A generic error message, given when no more specific message is suitable.[2]
|
368
|
+
501 Not Implemented
|
369
|
+
The server either does not recognise the request method, or it lacks the ability to fulfill the request.[2]
|
370
|
+
502 Bad Gateway
|
371
|
+
The server was acting as a gateway or proxy and received an invalid response from the upstream server.[2]
|
372
|
+
503 Service Unavailable
|
373
|
+
The server is currently unavailable (because it is overloaded or down for maintenance).[2] Generally, this is a temporary state.
|
374
|
+
504 Gateway Timeout
|
375
|
+
The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.[2]
|
376
|
+
505 HTTP Version Not Supported
|
377
|
+
The server does not support the HTTP protocol version used in the request.[2]
|
378
|
+
506 Variant Also Negotiates (RFC 2295)
|
379
|
+
Transparent content negotiation for the request results in a circular reference.[14]
|
380
|
+
507 Insufficient Storage (WebDAV) (RFC 4918)[4]
|
381
|
+
509 Bandwidth Limit Exceeded (Apache bw/limited extension)
|
382
|
+
This status code, while used by many servers, is not specified in any RFCs.
|
383
|
+
510 Not Extended (RFC 2774)
|
384
|
+
Further extensions to the request are required for the server to fulfill it.[15]
|
data/lib/dao/stdext.rb
CHANGED
data/lib/dao/support.rb
CHANGED
@@ -29,13 +29,17 @@ module Dao
|
|
29
29
|
def #{ method }(object, *args, &block)
|
30
30
|
case object
|
31
31
|
when Array
|
32
|
-
object.map{|element| Dao.#{ method }(element)}
|
32
|
+
object.map{|element| Dao.#{ method }(element, *args, &block)}
|
33
33
|
|
34
34
|
else
|
35
35
|
if object.respond_to?(:#{ method })
|
36
36
|
object.send(:#{ method }, *args, &block)
|
37
37
|
else
|
38
|
-
object
|
38
|
+
if object.respond_to?(:to_hash)
|
39
|
+
object.to_hash
|
40
|
+
else
|
41
|
+
object
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
@@ -59,4 +63,8 @@ module Dao
|
|
59
63
|
unindent!(s)
|
60
64
|
s
|
61
65
|
end
|
66
|
+
|
67
|
+
def name_for(path, *keys)
|
68
|
+
"#{ path }(#{ Array(keys).flatten.compact.join(',') })"
|
69
|
+
end
|
62
70
|
end
|
data/lib/dao/validations.rb
CHANGED
@@ -11,8 +11,23 @@ module Dao
|
|
11
11
|
def block
|
12
12
|
self
|
13
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
|
14
28
|
end
|
15
29
|
|
30
|
+
|
16
31
|
class << Validations
|
17
32
|
def for(*args, &block)
|
18
33
|
new(*args, &block)
|
@@ -29,16 +44,24 @@ module Dao
|
|
29
44
|
end
|
30
45
|
|
31
46
|
attr_accessor :result
|
47
|
+
attr_accessor :ran
|
32
48
|
|
33
49
|
def initialize(*args, &block)
|
34
50
|
@result = args.shift if args.first.is_a?(Result)
|
51
|
+
@ran = false
|
35
52
|
super
|
36
53
|
end
|
37
54
|
|
55
|
+
alias_method('ran?', 'ran')
|
56
|
+
|
38
57
|
def params
|
39
58
|
result.params
|
40
59
|
end
|
41
60
|
|
61
|
+
def data
|
62
|
+
result.data
|
63
|
+
end
|
64
|
+
|
42
65
|
def errors
|
43
66
|
result.errors
|
44
67
|
end
|
@@ -53,8 +76,8 @@ module Dao
|
|
53
76
|
size
|
54
77
|
end
|
55
78
|
|
56
|
-
alias_method
|
57
|
-
alias_method
|
79
|
+
alias_method('count', 'size')
|
80
|
+
alias_method('length', 'size')
|
58
81
|
|
59
82
|
Cleared = '___CLEARED___'.freeze unless defined?(Cleared)
|
60
83
|
|
@@ -68,18 +91,45 @@ module Dao
|
|
68
91
|
|
69
92
|
errors.clear
|
70
93
|
|
71
|
-
depth_first_each do |keys,
|
72
|
-
|
94
|
+
depth_first_each do |keys, chain|
|
95
|
+
chain.each do |callback|
|
96
|
+
next unless callback and callback.respond_to?(:to_proc)
|
73
97
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
98
|
+
number_of_errors = errors.size
|
99
|
+
value = data.get(keys)
|
100
|
+
returned =
|
101
|
+
catch(:valid) do
|
102
|
+
if result
|
103
|
+
result.instance_exec(value, &callback)
|
104
|
+
else
|
105
|
+
callback.call(value)
|
106
|
+
end
|
107
|
+
end
|
78
108
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
109
|
+
case returned
|
110
|
+
when Hash
|
111
|
+
map = Dao.map(returned)
|
112
|
+
valid = map[:valid]
|
113
|
+
message = map[:message]
|
114
|
+
|
115
|
+
when TrueClass, FalseClass
|
116
|
+
valid = returned
|
117
|
+
message = nil
|
118
|
+
|
119
|
+
else
|
120
|
+
any_errors_added = errors.size > number_of_errors
|
121
|
+
valid = !any_errors_added
|
122
|
+
message = nil
|
123
|
+
end
|
124
|
+
|
125
|
+
message ||= callback.options[:message]
|
126
|
+
message ||= (value.to_s.strip.empty? ? 'is blank' : 'is invalid')
|
127
|
+
|
128
|
+
unless valid
|
129
|
+
new_errors.push([keys, message])
|
130
|
+
else
|
131
|
+
new_errors.push([keys, Cleared])
|
132
|
+
end
|
83
133
|
end
|
84
134
|
end
|
85
135
|
|
@@ -93,6 +143,7 @@ module Dao
|
|
93
143
|
errors.add(keys, message)
|
94
144
|
end
|
95
145
|
|
146
|
+
@ran = true
|
96
147
|
return self
|
97
148
|
end
|
98
149
|
|
@@ -108,8 +159,281 @@ module Dao
|
|
108
159
|
block = args.pop if args.last.respond_to?(:call)
|
109
160
|
block ||= NotNil
|
110
161
|
callback = Validations::Callback.new(options, &block)
|
111
|
-
args.
|
112
|
-
|
162
|
+
set(args => Callback::Chain.new) unless has?(args)
|
163
|
+
get(args).add(callback)
|
164
|
+
callback
|
165
|
+
#args.push(callback)
|
166
|
+
#set(*args)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
module Validations::Common
|
171
|
+
def validates_length_of(*args)
|
172
|
+
options = Dao.options_for!(args)
|
173
|
+
|
174
|
+
message = options[:message]
|
175
|
+
|
176
|
+
if options[:in].is_a?(Range)
|
177
|
+
options[:minimum] = options[:in].begin
|
178
|
+
options[:maximum] = options[:in].end
|
179
|
+
end
|
180
|
+
minimum = options[:minimum] || 1
|
181
|
+
maximum = options[:maximum]
|
182
|
+
|
183
|
+
too_short = options[:too_short] || message || 'is too short'
|
184
|
+
too_long = options[:too_long] || message || 'is too long'
|
185
|
+
|
186
|
+
allow_nil = options[:allow_nil]
|
187
|
+
allow_blank = options[:allow_blank]
|
188
|
+
|
189
|
+
minimum = Float(minimum)
|
190
|
+
maximum = Float(maximum) if maximum
|
191
|
+
|
192
|
+
block =
|
193
|
+
lambda do |value|
|
194
|
+
map = Dao.map(:valid => true)
|
195
|
+
|
196
|
+
if value.nil? and allow_nil
|
197
|
+
map[:valid] = true
|
198
|
+
throw(:valid, map)
|
199
|
+
end
|
200
|
+
|
201
|
+
value = value.to_s.strip
|
202
|
+
|
203
|
+
if value.empty? and allow_blank
|
204
|
+
map[:valid] = true
|
205
|
+
throw(:valid, map)
|
206
|
+
end
|
207
|
+
|
208
|
+
if value.size < minimum
|
209
|
+
map[:message] = too_short
|
210
|
+
map[:valid] = false
|
211
|
+
throw(:valid, map)
|
212
|
+
end
|
213
|
+
|
214
|
+
if(maximum and(value.size > maximum))
|
215
|
+
map[:message] = too_long
|
216
|
+
map[:valid] = false
|
217
|
+
throw(:valid, map)
|
218
|
+
end
|
219
|
+
|
220
|
+
map
|
221
|
+
end
|
222
|
+
|
223
|
+
validates(*args, &block)
|
224
|
+
end
|
225
|
+
|
226
|
+
def validates_word_count_of(*args)
|
227
|
+
options = Dao.options_for!(args)
|
228
|
+
|
229
|
+
message = options[:message]
|
230
|
+
|
231
|
+
if options[:in].is_a?(Range)
|
232
|
+
options[:minimum] = options[:in].begin
|
233
|
+
options[:maximum] = options[:in].end
|
234
|
+
end
|
235
|
+
minimum = options[:minimum] || 1
|
236
|
+
maximum = options[:maximum]
|
237
|
+
|
238
|
+
too_short = options[:too_short] || message || 'is too short'
|
239
|
+
too_long = options[:too_long] || message || 'is too long'
|
240
|
+
|
241
|
+
allow_nil = options[:allow_nil]
|
242
|
+
allow_blank = options[:allow_blank]
|
243
|
+
|
244
|
+
minimum = Float(minimum)
|
245
|
+
maximum = Float(maximum) if maximum
|
246
|
+
|
247
|
+
block =
|
248
|
+
lambda do |value|
|
249
|
+
map = Dao.map(:valid => true)
|
250
|
+
|
251
|
+
if value.nil? and allow_nil
|
252
|
+
map[:valid] = true
|
253
|
+
throw(:valid, map)
|
254
|
+
end
|
255
|
+
|
256
|
+
value = value.to_s.strip
|
257
|
+
|
258
|
+
if value.empty? and allow_blank
|
259
|
+
map[:valid] = true
|
260
|
+
throw(:valid, map)
|
261
|
+
end
|
262
|
+
|
263
|
+
words = value.split(/\s+/)
|
264
|
+
|
265
|
+
if words.size < minimum
|
266
|
+
map[:message] = too_short
|
267
|
+
map[:valid] = false
|
268
|
+
throw(:valid, map)
|
269
|
+
end
|
270
|
+
|
271
|
+
if(maximum and(words.size > maximum))
|
272
|
+
map[:message] = too_long
|
273
|
+
map[:valid] = false
|
274
|
+
throw(:valid, map)
|
275
|
+
end
|
276
|
+
|
277
|
+
map
|
278
|
+
end
|
279
|
+
|
280
|
+
validates(*args, &block)
|
281
|
+
end
|
282
|
+
|
283
|
+
def validates_as_email(*args)
|
284
|
+
options = Dao.options_for!(args)
|
285
|
+
|
286
|
+
message = options[:message] || "doesn't look like an email (username@domain.com)"
|
287
|
+
|
288
|
+
allow_nil = options[:allow_nil]
|
289
|
+
allow_blank = options[:allow_blank]
|
290
|
+
|
291
|
+
block =
|
292
|
+
lambda do |value|
|
293
|
+
map = Dao.map(:valid => true)
|
294
|
+
|
295
|
+
if value.nil? and allow_nil
|
296
|
+
map[:valid] = true
|
297
|
+
throw(:valid, map)
|
298
|
+
end
|
299
|
+
|
300
|
+
value = value.to_s.strip
|
301
|
+
|
302
|
+
if value.empty? and allow_blank
|
303
|
+
map[:valid] = true
|
304
|
+
throw(:valid, map)
|
305
|
+
end
|
306
|
+
|
307
|
+
parts = value.split(/@/)
|
308
|
+
|
309
|
+
unless parts.size == 2
|
310
|
+
map[:valid] = false
|
311
|
+
throw(:valid, map)
|
312
|
+
end
|
313
|
+
|
314
|
+
map
|
315
|
+
end
|
316
|
+
|
317
|
+
args.push(:message => message)
|
318
|
+
validates(*args, &block)
|
319
|
+
end
|
320
|
+
|
321
|
+
def validates_as_url(*args)
|
322
|
+
options = Dao.options_for!(args)
|
323
|
+
|
324
|
+
message = options[:message] || "doesn't look like a url (http://domain.com)"
|
325
|
+
|
326
|
+
allow_nil = options[:allow_nil]
|
327
|
+
allow_blank = options[:allow_blank]
|
328
|
+
|
329
|
+
block =
|
330
|
+
lambda do |value|
|
331
|
+
map = Dao.map(:valid => true)
|
332
|
+
|
333
|
+
if value.nil? and allow_nil
|
334
|
+
map[:valid] = true
|
335
|
+
throw(:valid, map)
|
336
|
+
end
|
337
|
+
|
338
|
+
value = value.to_s.strip
|
339
|
+
|
340
|
+
if value.empty? and allow_blank
|
341
|
+
map[:valid] = true
|
342
|
+
throw(:valid, map)
|
343
|
+
end
|
344
|
+
|
345
|
+
parts = value.split(%r|://|)
|
346
|
+
|
347
|
+
unless parts.size >= 2
|
348
|
+
map[:valid] = false
|
349
|
+
throw(:valid, map)
|
350
|
+
end
|
351
|
+
|
352
|
+
map
|
353
|
+
end
|
354
|
+
|
355
|
+
args.push(:message => message)
|
356
|
+
validates(*args, &block)
|
357
|
+
end
|
358
|
+
|
359
|
+
def validates_as_phone(*args)
|
360
|
+
options = Dao.options_for!(args)
|
361
|
+
|
362
|
+
message = options[:message] || "doesn't look like a phone number (012.345.6789)"
|
363
|
+
|
364
|
+
allow_nil = options[:allow_nil]
|
365
|
+
allow_blank = options[:allow_blank]
|
366
|
+
|
367
|
+
block =
|
368
|
+
lambda do |value|
|
369
|
+
map = Dao.map(:valid => true)
|
370
|
+
|
371
|
+
if value.nil? and allow_nil
|
372
|
+
map[:valid] = true
|
373
|
+
throw(:valid, map)
|
374
|
+
end
|
375
|
+
|
376
|
+
value = value.to_s.strip
|
377
|
+
|
378
|
+
if value.empty? and allow_blank
|
379
|
+
map[:valid] = true
|
380
|
+
throw(:valid, map)
|
381
|
+
end
|
382
|
+
|
383
|
+
parts = value.scan(/\d+/)
|
384
|
+
|
385
|
+
unless parts.size >= 1
|
386
|
+
map[:valid] = false
|
387
|
+
throw(:valid, map)
|
388
|
+
end
|
389
|
+
|
390
|
+
map
|
391
|
+
end
|
392
|
+
|
393
|
+
args.push(:message => message)
|
394
|
+
validates(*args, &block)
|
395
|
+
end
|
396
|
+
|
397
|
+
def validates_presence_of(*args)
|
398
|
+
options = Dao.options_for!(args)
|
399
|
+
|
400
|
+
message = options[:message] || 'is blank or missing'
|
401
|
+
|
402
|
+
allow_nil = options[:allow_nil]
|
403
|
+
allow_blank = options[:allow_blank]
|
404
|
+
|
405
|
+
block =
|
406
|
+
lambda do |value|
|
407
|
+
map = Dao.map(:valid => true)
|
408
|
+
|
409
|
+
if value.nil?
|
410
|
+
unless allow_nil
|
411
|
+
map[:message] = message
|
412
|
+
map[:valid] = false
|
413
|
+
throw(:valid, map)
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
value = value.to_s.strip
|
418
|
+
|
419
|
+
if value.empty?
|
420
|
+
unless allow_blank
|
421
|
+
map[:message] = message
|
422
|
+
map[:valid] = false
|
423
|
+
throw(:valid, map)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
map
|
428
|
+
end
|
429
|
+
|
430
|
+
validates(*args, &block)
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
def Validations.add(method_name, &block)
|
435
|
+
::Dao::Validations::Common.module_eval do
|
436
|
+
define_method(method_name, &block)
|
113
437
|
end
|
114
438
|
end
|
115
439
|
end
|