jpbougie-couchrest 0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +176 -0
- data/README.md +95 -0
- data/Rakefile +74 -0
- data/THANKS.md +18 -0
- data/examples/model/example.rb +144 -0
- data/examples/word_count/markov +38 -0
- data/examples/word_count/views/books/chunked-map.js +3 -0
- data/examples/word_count/views/books/united-map.js +1 -0
- data/examples/word_count/views/markov/chain-map.js +6 -0
- data/examples/word_count/views/markov/chain-reduce.js +7 -0
- data/examples/word_count/views/word_count/count-map.js +6 -0
- data/examples/word_count/views/word_count/count-reduce.js +3 -0
- data/examples/word_count/word_count.rb +46 -0
- data/examples/word_count/word_count_query.rb +40 -0
- data/examples/word_count/word_count_views.rb +26 -0
- data/lib/couchrest.rb +200 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/database.rb +304 -0
- data/lib/couchrest/core/design.rb +79 -0
- data/lib/couchrest/core/document.rb +87 -0
- data/lib/couchrest/core/response.rb +16 -0
- data/lib/couchrest/core/server.rb +88 -0
- data/lib/couchrest/core/view.rb +4 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +44 -0
- data/lib/couchrest/helper/upgrade.rb +51 -0
- data/lib/couchrest/mixins.rb +4 -0
- data/lib/couchrest/mixins/attachments.rb +31 -0
- data/lib/couchrest/mixins/callbacks.rb +483 -0
- data/lib/couchrest/mixins/class_proxy.rb +112 -0
- data/lib/couchrest/mixins/design_doc.rb +94 -0
- data/lib/couchrest/mixins/document_queries.rb +44 -0
- data/lib/couchrest/mixins/extended_attachments.rb +68 -0
- data/lib/couchrest/mixins/extended_document_mixins.rb +7 -0
- data/lib/couchrest/mixins/properties.rb +129 -0
- data/lib/couchrest/mixins/validation.rb +257 -0
- data/lib/couchrest/mixins/views.rb +177 -0
- data/lib/couchrest/monkeypatches.rb +113 -0
- data/lib/couchrest/more/casted_model.rb +29 -0
- data/lib/couchrest/more/extended_document.rb +220 -0
- data/lib/couchrest/more/property.rb +40 -0
- data/lib/couchrest/support/blank.rb +42 -0
- data/lib/couchrest/support/class.rb +176 -0
- data/lib/couchrest/support/rails.rb +35 -0
- data/lib/couchrest/validation/auto_validate.rb +163 -0
- data/lib/couchrest/validation/contextual_validators.rb +78 -0
- data/lib/couchrest/validation/validation_errors.rb +118 -0
- data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
- data/lib/couchrest/validation/validators/confirmation_validator.rb +99 -0
- data/lib/couchrest/validation/validators/format_validator.rb +117 -0
- data/lib/couchrest/validation/validators/formats/email.rb +66 -0
- data/lib/couchrest/validation/validators/formats/url.rb +43 -0
- data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
- data/lib/couchrest/validation/validators/length_validator.rb +134 -0
- data/lib/couchrest/validation/validators/method_validator.rb +89 -0
- data/lib/couchrest/validation/validators/numeric_validator.rb +104 -0
- data/lib/couchrest/validation/validators/required_field_validator.rb +109 -0
- data/spec/couchrest/core/couchrest_spec.rb +201 -0
- data/spec/couchrest/core/database_spec.rb +699 -0
- data/spec/couchrest/core/design_spec.rb +138 -0
- data/spec/couchrest/core/document_spec.rb +267 -0
- data/spec/couchrest/core/server_spec.rb +35 -0
- data/spec/couchrest/helpers/pager_spec.rb +122 -0
- data/spec/couchrest/helpers/streamer_spec.rb +23 -0
- data/spec/couchrest/more/casted_extended_doc_spec.rb +40 -0
- data/spec/couchrest/more/casted_model_spec.rb +142 -0
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +130 -0
- data/spec/couchrest/more/extended_doc_spec.rb +537 -0
- data/spec/couchrest/more/extended_doc_subclass_spec.rb +98 -0
- data/spec/couchrest/more/extended_doc_view_spec.rb +338 -0
- data/spec/couchrest/more/property_spec.rb +136 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/more/article.rb +34 -0
- data/spec/fixtures/more/card.rb +20 -0
- data/spec/fixtures/more/cat.rb +18 -0
- data/spec/fixtures/more/course.rb +14 -0
- data/spec/fixtures/more/event.rb +6 -0
- data/spec/fixtures/more/invoice.rb +17 -0
- data/spec/fixtures/more/person.rb +8 -0
- data/spec/fixtures/more/question.rb +6 -0
- data/spec/fixtures/more/service.rb +12 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +38 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +192 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
# a copy of this software and associated documentation files (the
|
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
# the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be
|
|
14
|
+
# included in all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
module CouchRest
|
|
25
|
+
module Validation
|
|
26
|
+
|
|
27
|
+
##
|
|
28
|
+
#
|
|
29
|
+
# @author Guy van den Berg
|
|
30
|
+
# @since 0.9
|
|
31
|
+
class ConfirmationValidator < GenericValidator
|
|
32
|
+
|
|
33
|
+
def initialize(field_name, options = {})
|
|
34
|
+
super
|
|
35
|
+
@options = options
|
|
36
|
+
@field_name, @confirm_field_name = field_name, (options[:confirm] || "#{field_name}_confirmation").to_sym
|
|
37
|
+
@options[:allow_nil] = true unless @options.has_key?(:allow_nil)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def call(target)
|
|
41
|
+
unless valid?(target)
|
|
42
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:confirmation, field_name)
|
|
43
|
+
add_error(target, error_message, field_name)
|
|
44
|
+
return false
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
return true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def valid?(target)
|
|
51
|
+
field_value = target.send(field_name)
|
|
52
|
+
return true if @options[:allow_nil] && field_value.nil?
|
|
53
|
+
return false if !@options[:allow_nil] && field_value.nil?
|
|
54
|
+
|
|
55
|
+
confirm_value = target.instance_variable_get("@#{@confirm_field_name}")
|
|
56
|
+
field_value == confirm_value
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end # class ConfirmationValidator
|
|
60
|
+
|
|
61
|
+
module ValidatesIsConfirmed
|
|
62
|
+
|
|
63
|
+
##
|
|
64
|
+
# Validates that the given attribute is confirmed by another attribute.
|
|
65
|
+
# A common use case scenario is when you require a user to confirm their
|
|
66
|
+
# password, for which you use both password and password_confirmation
|
|
67
|
+
# attributes.
|
|
68
|
+
#
|
|
69
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
|
70
|
+
# @option :confirm<Symbol> the attribute that you want to validate
|
|
71
|
+
# against (default is firstattr_confirmation)
|
|
72
|
+
#
|
|
73
|
+
# @example [Usage]
|
|
74
|
+
#
|
|
75
|
+
# class Page < Hash
|
|
76
|
+
# include CouchRest::ExtendedModel
|
|
77
|
+
# include CouchRest::Validations
|
|
78
|
+
#
|
|
79
|
+
# property :password, String
|
|
80
|
+
# property :email, String
|
|
81
|
+
# attr_accessor :password_confirmation
|
|
82
|
+
# attr_accessor :email_repeated
|
|
83
|
+
#
|
|
84
|
+
# validates_is_confirmed :password
|
|
85
|
+
# validates_is_confirmed :email, :confirm => :email_repeated
|
|
86
|
+
#
|
|
87
|
+
# # a call to valid? will return false unless:
|
|
88
|
+
# # password == password_confirmation
|
|
89
|
+
# # and
|
|
90
|
+
# # email == email_repeated
|
|
91
|
+
#
|
|
92
|
+
def validates_is_confirmed(*fields)
|
|
93
|
+
opts = opts_from_validator_args(fields)
|
|
94
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::ConfirmationValidator)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
end # module ValidatesIsConfirmed
|
|
98
|
+
end # module Validation
|
|
99
|
+
end # module CouchRest
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
# a copy of this software and associated documentation files (the
|
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
# the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be
|
|
14
|
+
# included in all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
require 'pathname'
|
|
25
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/email'
|
|
26
|
+
require Pathname(__FILE__).dirname.expand_path + 'formats/url'
|
|
27
|
+
|
|
28
|
+
module CouchRest
|
|
29
|
+
module Validation
|
|
30
|
+
|
|
31
|
+
##
|
|
32
|
+
#
|
|
33
|
+
# @author Guy van den Berg
|
|
34
|
+
# @since 0.9
|
|
35
|
+
class FormatValidator < GenericValidator
|
|
36
|
+
|
|
37
|
+
FORMATS = {}
|
|
38
|
+
include CouchRest::Validation::Format::Email
|
|
39
|
+
include CouchRest::Validation::Format::Url
|
|
40
|
+
|
|
41
|
+
def initialize(field_name, options = {}, &b)
|
|
42
|
+
super(field_name, options)
|
|
43
|
+
@field_name, @options = field_name, options
|
|
44
|
+
@options[:allow_nil] = false unless @options.has_key?(:allow_nil)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def call(target)
|
|
48
|
+
value = target.validation_property_value(field_name)
|
|
49
|
+
return true if @options[:allow_nil] && value.nil?
|
|
50
|
+
|
|
51
|
+
validation = @options[:as] || @options[:with]
|
|
52
|
+
|
|
53
|
+
raise "No such predefined format '#{validation}'" if validation.is_a?(Symbol) && !FORMATS.has_key?(validation)
|
|
54
|
+
validator = validation.is_a?(Symbol) ? FORMATS[validation][0] : validation
|
|
55
|
+
|
|
56
|
+
valid = case validator
|
|
57
|
+
when Proc then validator.call(value)
|
|
58
|
+
when Regexp then value =~ validator
|
|
59
|
+
else
|
|
60
|
+
raise UnknownValidationFormat, "Can't determine how to validate #{target.class}##{field_name} with #{validator.inspect}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
return true if valid
|
|
64
|
+
|
|
65
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:invalid, field_name)
|
|
66
|
+
|
|
67
|
+
field = CouchRest.humanize(field_name)
|
|
68
|
+
error_message = error_message.call(field, value) if error_message.respond_to?(:call)
|
|
69
|
+
|
|
70
|
+
add_error(target, error_message, field_name)
|
|
71
|
+
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
#class UnknownValidationFormat < StandardError; end
|
|
76
|
+
|
|
77
|
+
end # class FormatValidator
|
|
78
|
+
|
|
79
|
+
module ValidatesFormat
|
|
80
|
+
|
|
81
|
+
##
|
|
82
|
+
# Validates that the attribute is in the specified format. You may use the
|
|
83
|
+
# :as (or :with, it's an alias) option to specify the pre-defined format
|
|
84
|
+
# that you want to validate against. You may also specify your own format
|
|
85
|
+
# via a Proc or Regexp passed to the the :as or :with options.
|
|
86
|
+
#
|
|
87
|
+
# @option :allow_nil<Boolean> true/false (default is true)
|
|
88
|
+
# @option :as<Format, Proc, Regexp> the pre-defined format, Proc or Regexp to validate against
|
|
89
|
+
# @option :with<Format, Proc, Regexp> an alias for :as
|
|
90
|
+
#
|
|
91
|
+
# @details [Pre-defined Formats]
|
|
92
|
+
# :email_address (format is specified in DataMapper::Validation::Format::Email)
|
|
93
|
+
# :url (format is specified in DataMapper::Validation::Format::Url)
|
|
94
|
+
#
|
|
95
|
+
# @example [Usage]
|
|
96
|
+
#
|
|
97
|
+
# class Page
|
|
98
|
+
#
|
|
99
|
+
# property :email, String
|
|
100
|
+
# property :zip_code, String
|
|
101
|
+
#
|
|
102
|
+
# validates_format :email, :as => :email_address
|
|
103
|
+
# validates_format :zip_code, :with => /^\d{5}$/
|
|
104
|
+
#
|
|
105
|
+
# # a call to valid? will return false unless:
|
|
106
|
+
# # email is formatted like an email address
|
|
107
|
+
# # and
|
|
108
|
+
# # zip_code is a string of 5 digits
|
|
109
|
+
#
|
|
110
|
+
def validates_format(*fields)
|
|
111
|
+
opts = opts_from_validator_args(fields)
|
|
112
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::FormatValidator)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
end # module ValidatesFormat
|
|
116
|
+
end # module Validation
|
|
117
|
+
end # module CouchRest
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# encoding: binary
|
|
2
|
+
|
|
3
|
+
# Extracted from dm-validations 0.9.10
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2007 Guy van den Berg
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
+
# a copy of this software and associated documentation files (the
|
|
9
|
+
# "Software"), to deal in the Software without restriction, including
|
|
10
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
+
# the following conditions:
|
|
14
|
+
#
|
|
15
|
+
# The above copyright notice and this permission notice shall be
|
|
16
|
+
# included in all copies or substantial portions of the Software.
|
|
17
|
+
#
|
|
18
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
|
|
26
|
+
module CouchRest
|
|
27
|
+
module Validation
|
|
28
|
+
module Format
|
|
29
|
+
module Email
|
|
30
|
+
|
|
31
|
+
def self.included(base)
|
|
32
|
+
CouchRest::Validation::FormatValidator::FORMATS.merge!(
|
|
33
|
+
:email_address => [ EmailAddress, lambda { |field, value| '%s is not a valid email address'.t(value) }]
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# RFC2822 (No attribution reference available)
|
|
38
|
+
EmailAddress = begin
|
|
39
|
+
alpha = "a-zA-Z"
|
|
40
|
+
digit = "0-9"
|
|
41
|
+
atext = "[#{alpha}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
|
|
42
|
+
dot_atom_text = "#{atext}+([.]#{atext}*)*"
|
|
43
|
+
dot_atom = "#{dot_atom_text}"
|
|
44
|
+
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
|
45
|
+
text = "[\\x01-\\x09\\x11\\x12\\x14-\\x7f]"
|
|
46
|
+
quoted_pair = "(\\x5c#{text})"
|
|
47
|
+
qcontent = "(?:#{qtext}|#{quoted_pair})"
|
|
48
|
+
quoted_string = "[\"]#{qcontent}+[\"]"
|
|
49
|
+
atom = "#{atext}+"
|
|
50
|
+
word = "(?:#{atom}|#{quoted_string})"
|
|
51
|
+
obs_local_part = "#{word}([.]#{word})*"
|
|
52
|
+
local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
|
|
53
|
+
no_ws_ctl = "\\x01-\\x08\\x11\\x12\\x14-\\x1f\\x7f"
|
|
54
|
+
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
|
55
|
+
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
|
56
|
+
domain_literal = "\\[#{dcontent}+\\]"
|
|
57
|
+
obs_domain = "#{atom}([.]#{atom})*"
|
|
58
|
+
domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
|
|
59
|
+
addr_spec = "#{local_part}\@#{domain}"
|
|
60
|
+
pattern = /^#{addr_spec}$/
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end # module Email
|
|
64
|
+
end # module Format
|
|
65
|
+
end # module Validation
|
|
66
|
+
end # module CouchRest
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Extracted from dm-validations 0.9.10
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2007 Guy van den Berg
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
# a copy of this software and associated documentation files (the
|
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
# the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be
|
|
14
|
+
# included in all copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
module CouchRest
|
|
25
|
+
module Validation
|
|
26
|
+
module Format
|
|
27
|
+
module Url
|
|
28
|
+
|
|
29
|
+
def self.included(base)
|
|
30
|
+
CouchRest::Validation::FormatValidator::FORMATS.merge!(
|
|
31
|
+
:url => [ Url, lambda { |field, value| '%s is not a valid URL'.t(value) }]
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
Url = begin
|
|
36
|
+
# Regex from http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/
|
|
37
|
+
/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end # module Url
|
|
41
|
+
end # module Format
|
|
42
|
+
end # module Validation
|
|
43
|
+
end # module CouchRest
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
# Extracted from dm-validations 0.9.10
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2007 Guy van den Berg
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
+
# a copy of this software and associated documentation files (the
|
|
9
|
+
# "Software"), to deal in the Software without restriction, including
|
|
10
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
+
# the following conditions:
|
|
14
|
+
#
|
|
15
|
+
# The above copyright notice and this permission notice shall be
|
|
16
|
+
# included in all copies or substantial portions of the Software.
|
|
17
|
+
#
|
|
18
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
|
|
26
|
+
module CouchRest
|
|
27
|
+
module Validation
|
|
28
|
+
|
|
29
|
+
# All validators extend this base class. Validators must:
|
|
30
|
+
#
|
|
31
|
+
# * Implement the initialize method to capture its parameters, also calling
|
|
32
|
+
# super to have this parent class capture the optional, general :if and
|
|
33
|
+
# :unless parameters.
|
|
34
|
+
# * Implement the call method, returning true or false. The call method
|
|
35
|
+
# provides the validation logic.
|
|
36
|
+
#
|
|
37
|
+
# @author Guy van den Berg
|
|
38
|
+
class GenericValidator
|
|
39
|
+
|
|
40
|
+
attr_accessor :if_clause, :unless_clause
|
|
41
|
+
attr_reader :field_name
|
|
42
|
+
|
|
43
|
+
# Construct a validator. Capture the :if and :unless clauses when present.
|
|
44
|
+
#
|
|
45
|
+
# @param field<String, Symbol> The property specified for validation
|
|
46
|
+
#
|
|
47
|
+
# @option :if<Symbol, Proc> The name of a method or a Proc to call to
|
|
48
|
+
# determine if the validation should occur.
|
|
49
|
+
# @option :unless<Symbol, Proc> The name of a method or a Proc to call to
|
|
50
|
+
# determine if the validation should not occur
|
|
51
|
+
# All additional key/value pairs are passed through to the validator
|
|
52
|
+
# that is sub-classing this GenericValidator
|
|
53
|
+
#
|
|
54
|
+
def initialize(field, opts = {})
|
|
55
|
+
@if_clause = opts.delete(:if)
|
|
56
|
+
@unless_clause = opts.delete(:unless)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Add an error message to a target resource. If the error corresponds to a
|
|
60
|
+
# specific field of the resource, add it to that field, otherwise add it
|
|
61
|
+
# as a :general message.
|
|
62
|
+
#
|
|
63
|
+
# @param <Object> target the resource that has the error
|
|
64
|
+
# @param <String> message the message to add
|
|
65
|
+
# @param <Symbol> field_name the name of the field that caused the error
|
|
66
|
+
#
|
|
67
|
+
# TODO - should the field_name for a general message be :default???
|
|
68
|
+
#
|
|
69
|
+
def add_error(target, message, field_name = :general)
|
|
70
|
+
target.errors.add(field_name, message)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Call the validator. "call" is used so the operation is BoundMethod and
|
|
74
|
+
# Block compatible. This must be implemented in all concrete classes.
|
|
75
|
+
#
|
|
76
|
+
# @param <Object> target the resource that the validator must be called
|
|
77
|
+
# against
|
|
78
|
+
# @return <Boolean> true if valid, otherwise false
|
|
79
|
+
def call(target)
|
|
80
|
+
raise NotImplementedError, "CouchRest::Validation::GenericValidator::call must be overriden in a subclass"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Determines if this validator should be run against the
|
|
84
|
+
# target by evaluating the :if and :unless clauses
|
|
85
|
+
# optionally passed while specifying any validator.
|
|
86
|
+
#
|
|
87
|
+
# @param <Object> target the resource that we check against
|
|
88
|
+
# @return <Boolean> true if should be run, otherwise false
|
|
89
|
+
def execute?(target)
|
|
90
|
+
if unless_clause = self.unless_clause
|
|
91
|
+
if unless_clause.is_a?(Symbol)
|
|
92
|
+
return false if target.send(unless_clause)
|
|
93
|
+
elsif unless_clause.respond_to?(:call)
|
|
94
|
+
return false if unless_clause.call(target)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
if if_clause = self.if_clause
|
|
99
|
+
if if_clause.is_a?(Symbol)
|
|
100
|
+
return target.send(if_clause)
|
|
101
|
+
elsif if_clause.respond_to?(:call)
|
|
102
|
+
return if_clause.call(target)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def ==(other)
|
|
110
|
+
self.class == other.class &&
|
|
111
|
+
self.field_name == other.field_name &&
|
|
112
|
+
self.class == other.class &&
|
|
113
|
+
self.if_clause == other.if_clause &&
|
|
114
|
+
self.unless_clause == other.unless_clause &&
|
|
115
|
+
self.instance_variable_get(:@options) == other.instance_variable_get(:@options)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end # class GenericValidator
|
|
119
|
+
end # module Validation
|
|
120
|
+
end # module CouchRest
|