samflores-couchrest 0.2.1 → 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -34
- data/Rakefile +5 -2
- data/bin/couchdir +20 -0
- data/examples/model/example.rb +13 -19
- data/examples/word_count/word_count.rb +24 -3
- data/examples/word_count/word_count_query.rb +6 -7
- data/lib/couchrest/core/database.rb +49 -126
- data/lib/couchrest/core/document.rb +25 -58
- data/lib/couchrest/core/model.rb +612 -0
- data/lib/couchrest/core/server.rb +10 -47
- data/lib/couchrest/core/validations.rb +328 -0
- data/lib/couchrest/monkeypatches.rb +0 -95
- data/lib/couchrest.rb +10 -57
- data/spec/couchrest/core/database_spec.rb +68 -183
- data/spec/couchrest/core/design_spec.rb +1 -1
- data/spec/couchrest/core/document_spec.rb +104 -285
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/couchrest/helpers/pager_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -13
- metadata +17 -83
- data/examples/word_count/word_count_views.rb +0 -26
- data/lib/couchrest/core/response.rb +0 -16
- data/lib/couchrest/mixins/attachments.rb +0 -31
- data/lib/couchrest/mixins/callbacks.rb +0 -483
- data/lib/couchrest/mixins/design_doc.rb +0 -64
- data/lib/couchrest/mixins/document_queries.rb +0 -48
- data/lib/couchrest/mixins/extended_attachments.rb +0 -68
- data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
- data/lib/couchrest/mixins/properties.rb +0 -125
- data/lib/couchrest/mixins/validation.rb +0 -234
- data/lib/couchrest/mixins/views.rb +0 -168
- data/lib/couchrest/mixins.rb +0 -4
- data/lib/couchrest/more/casted_model.rb +0 -28
- data/lib/couchrest/more/extended_document.rb +0 -217
- data/lib/couchrest/more/property.rb +0 -40
- data/lib/couchrest/support/blank.rb +0 -42
- data/lib/couchrest/support/class.rb +0 -191
- data/lib/couchrest/validation/auto_validate.rb +0 -163
- data/lib/couchrest/validation/contextual_validators.rb +0 -78
- data/lib/couchrest/validation/validation_errors.rb +0 -118
- data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
- data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
- data/lib/couchrest/validation/validators/format_validator.rb +0 -117
- data/lib/couchrest/validation/validators/formats/email.rb +0 -66
- data/lib/couchrest/validation/validators/formats/url.rb +0 -43
- data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
- data/lib/couchrest/validation/validators/length_validator.rb +0 -134
- data/lib/couchrest/validation/validators/method_validator.rb +0 -89
- data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
- data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
- data/spec/couchrest/core/server_spec.rb +0 -35
- data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
- data/spec/couchrest/more/casted_model_spec.rb +0 -98
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
- data/spec/couchrest/more/extended_doc_spec.rb +0 -509
- data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
- data/spec/couchrest/more/property_spec.rb +0 -130
- data/spec/couchrest/support/class_spec.rb +0 -59
- data/spec/fixtures/more/article.rb +0 -34
- data/spec/fixtures/more/card.rb +0 -20
- data/spec/fixtures/more/course.rb +0 -14
- data/spec/fixtures/more/event.rb +0 -6
- data/spec/fixtures/more/invoice.rb +0 -17
- data/spec/fixtures/more/person.rb +0 -8
- data/spec/fixtures/more/question.rb +0 -6
- data/spec/fixtures/more/service.rb +0 -12
@@ -1,120 +0,0 @@
|
|
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
|
@@ -1,134 +0,0 @@
|
|
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 LengthValidator < GenericValidator
|
32
|
-
|
33
|
-
def initialize(field_name, options)
|
34
|
-
super
|
35
|
-
@field_name = field_name
|
36
|
-
@options = options
|
37
|
-
|
38
|
-
@min = options[:minimum] || options[:min]
|
39
|
-
@max = options[:maximum] || options[:max]
|
40
|
-
@equal = options[:is] || options[:equals]
|
41
|
-
@range = options[:within] || options[:in]
|
42
|
-
|
43
|
-
@validation_method ||= :range if @range
|
44
|
-
@validation_method ||= :min if @min && @max.nil?
|
45
|
-
@validation_method ||= :max if @max && @min.nil?
|
46
|
-
@validation_method ||= :equals unless @equal.nil?
|
47
|
-
end
|
48
|
-
|
49
|
-
def call(target)
|
50
|
-
field_value = target.validation_property_value(field_name)
|
51
|
-
return true if @options[:allow_nil] && field_value.nil?
|
52
|
-
|
53
|
-
field_value = '' if field_value.nil?
|
54
|
-
|
55
|
-
# XXX: HACK seems hacky to do this on every validation, probably should
|
56
|
-
# do this elsewhere?
|
57
|
-
field = Extlib::Inflection.humanize(field_name)
|
58
|
-
min = @range ? @range.min : @min
|
59
|
-
max = @range ? @range.max : @max
|
60
|
-
equal = @equal
|
61
|
-
|
62
|
-
case @validation_method
|
63
|
-
when :range then
|
64
|
-
unless valid = @range.include?(field_value.size)
|
65
|
-
error_message = ValidationErrors.default_error_message(:length_between, field, min, max)
|
66
|
-
end
|
67
|
-
when :min then
|
68
|
-
unless valid = field_value.size >= min
|
69
|
-
error_message = ValidationErrors.default_error_message(:too_short, field, min)
|
70
|
-
end
|
71
|
-
when :max then
|
72
|
-
unless valid = field_value.size <= max
|
73
|
-
error_message = ValidationErrors.default_error_message(:too_long, field, max)
|
74
|
-
end
|
75
|
-
when :equals then
|
76
|
-
unless valid = field_value.size == equal
|
77
|
-
error_message = ValidationErrors.default_error_message(:wrong_length, field, equal)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
error_message = @options[:message] || error_message
|
82
|
-
|
83
|
-
add_error(target, error_message, field_name) unless valid
|
84
|
-
|
85
|
-
return valid
|
86
|
-
end
|
87
|
-
|
88
|
-
end # class LengthValidator
|
89
|
-
|
90
|
-
module ValidatesLength
|
91
|
-
|
92
|
-
# Validates that the length of the attribute is equal to, less than,
|
93
|
-
# greater than or within a certain range (depending upon the options
|
94
|
-
# you specify).
|
95
|
-
#
|
96
|
-
# @option :allow_nil<Boolean> true/false (default is true)
|
97
|
-
# @option :minimum ensures that the attribute's length is greater than
|
98
|
-
# or equal to the supplied value
|
99
|
-
# @option :min alias for :minimum
|
100
|
-
# @option :maximum ensures the attribute's length is less than or equal
|
101
|
-
# to the supplied value
|
102
|
-
# @option :max alias for :maximum
|
103
|
-
# @option :equals ensures the attribute's length is equal to the
|
104
|
-
# supplied value
|
105
|
-
# @option :is alias for :equals
|
106
|
-
# @option :in<Range> given a Range, ensures that the attributes length is
|
107
|
-
# include?'ed in the Range
|
108
|
-
# @option :within<Range> alias for :in
|
109
|
-
#
|
110
|
-
# @example [Usage]
|
111
|
-
#
|
112
|
-
# class Page
|
113
|
-
#
|
114
|
-
# property high, Integer
|
115
|
-
# property low, Integer
|
116
|
-
# property just_right, Integer
|
117
|
-
#
|
118
|
-
# validates_length :high, :min => 100000000000
|
119
|
-
# validates_length :low, :equals => 0
|
120
|
-
# validates_length :just_right, :within => 1..10
|
121
|
-
#
|
122
|
-
# # a call to valid? will return false unless:
|
123
|
-
# # high is greater than or equal to 100000000000
|
124
|
-
# # low is equal to 0
|
125
|
-
# # just_right is between 1 and 10 (inclusive of both 1 and 10)
|
126
|
-
#
|
127
|
-
def validates_length(*fields)
|
128
|
-
opts = opts_from_validator_args(fields)
|
129
|
-
add_validator_to_context(opts, fields, CouchRest::Validation::LengthValidator)
|
130
|
-
end
|
131
|
-
|
132
|
-
end # module ValidatesLength
|
133
|
-
end # module Validation
|
134
|
-
end # module CouchRest
|
@@ -1,89 +0,0 @@
|
|
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 MethodValidator < GenericValidator
|
32
|
-
|
33
|
-
def initialize(field_name, options={})
|
34
|
-
super
|
35
|
-
@field_name, @options = field_name, options.clone
|
36
|
-
@options[:method] = @field_name unless @options.has_key?(:method)
|
37
|
-
end
|
38
|
-
|
39
|
-
def call(target)
|
40
|
-
result, message = target.send(@options[:method])
|
41
|
-
add_error(target, message, field_name) unless result
|
42
|
-
result
|
43
|
-
end
|
44
|
-
|
45
|
-
def ==(other)
|
46
|
-
@options[:method] == other.instance_variable_get(:@options)[:method] && super
|
47
|
-
end
|
48
|
-
end # class MethodValidator
|
49
|
-
|
50
|
-
module ValidatesWithMethod
|
51
|
-
|
52
|
-
##
|
53
|
-
# Validate using the given method. The method given needs to return:
|
54
|
-
# [result::<Boolean>, Error Message::<String>]
|
55
|
-
#
|
56
|
-
# @example [Usage]
|
57
|
-
#
|
58
|
-
# class Page
|
59
|
-
#
|
60
|
-
# property :zip_code, String
|
61
|
-
#
|
62
|
-
# validates_with_method :in_the_right_location?
|
63
|
-
#
|
64
|
-
# def in_the_right_location?
|
65
|
-
# if @zip_code == "94301"
|
66
|
-
# return true
|
67
|
-
# else
|
68
|
-
# return [false, "You're in the wrong zip code"]
|
69
|
-
# end
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# # A call to valid? will return false and
|
73
|
-
# # populate the object's errors with "You're in the
|
74
|
-
# # wrong zip code" unless zip_code == "94301"
|
75
|
-
#
|
76
|
-
# # You can also specify field:
|
77
|
-
#
|
78
|
-
# validates_with_method :zip_code, :in_the_right_location?
|
79
|
-
#
|
80
|
-
# # it will add returned error message to :zip_code field
|
81
|
-
#
|
82
|
-
def validates_with_method(*fields)
|
83
|
-
opts = opts_from_validator_args(fields)
|
84
|
-
add_validator_to_context(opts, fields, CouchRest::Validation::MethodValidator)
|
85
|
-
end
|
86
|
-
|
87
|
-
end # module ValidatesWithMethod
|
88
|
-
end # module Validation
|
89
|
-
end # module CouchRest
|
@@ -1,104 +0,0 @@
|
|
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 NumericValidator < GenericValidator
|
32
|
-
|
33
|
-
def initialize(field_name, options={})
|
34
|
-
super
|
35
|
-
@field_name, @options = field_name, options
|
36
|
-
@options[:integer_only] = false unless @options.has_key?(:integer_only)
|
37
|
-
end
|
38
|
-
|
39
|
-
def call(target)
|
40
|
-
value = target.send(field_name)
|
41
|
-
return true if @options[:allow_nil] && value.nil?
|
42
|
-
|
43
|
-
value = value.kind_of?(Float) ? value.to_s('F') : value.to_s
|
44
|
-
|
45
|
-
error_message = @options[:message]
|
46
|
-
precision = @options[:precision]
|
47
|
-
scale = @options[:scale]
|
48
|
-
|
49
|
-
if @options[:integer_only]
|
50
|
-
return true if value =~ /\A[+-]?\d+\z/
|
51
|
-
error_message ||= ValidationErrors.default_error_message(:not_an_integer, field_name)
|
52
|
-
else
|
53
|
-
# FIXME: if precision and scale are not specified, can we assume that it is an integer?
|
54
|
-
# probably not, as floating point numbers don't have hard
|
55
|
-
# defined scale. the scale floats with the length of the
|
56
|
-
# integral and precision. Ie. if precision = 10 and integral
|
57
|
-
# portion of the number is 9834 (4 digits), the max scale will
|
58
|
-
# be 6 (10 - 4). But if the integral length is 1, max scale
|
59
|
-
# will be (10 - 1) = 9, so 1.234567890.
|
60
|
-
if precision && scale
|
61
|
-
#handles both Float when it has scale specified and BigDecimal
|
62
|
-
if precision > scale && scale > 0
|
63
|
-
return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
|
64
|
-
elsif precision > scale && scale == 0
|
65
|
-
return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
|
66
|
-
elsif precision == scale
|
67
|
-
return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
|
68
|
-
else
|
69
|
-
raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})"
|
70
|
-
end
|
71
|
-
elsif precision && scale.nil?
|
72
|
-
# for floats, if scale is not set
|
73
|
-
|
74
|
-
#total number of digits is less or equal precision
|
75
|
-
return true if value.gsub(/[^\d]/, '').length <= precision
|
76
|
-
|
77
|
-
#number of digits before decimal == precision, and the number is x.0. same as scale = 0
|
78
|
-
return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
|
79
|
-
else
|
80
|
-
return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/
|
81
|
-
end
|
82
|
-
error_message ||= ValidationErrors.default_error_message(:not_a_number, field_name)
|
83
|
-
end
|
84
|
-
|
85
|
-
add_error(target, error_message, field_name)
|
86
|
-
|
87
|
-
# TODO: check the gt, gte, lt, lte, and eq options
|
88
|
-
|
89
|
-
return false
|
90
|
-
end
|
91
|
-
end # class NumericValidator
|
92
|
-
|
93
|
-
module ValidatesIsNumber
|
94
|
-
|
95
|
-
# Validate whether a field is numeric
|
96
|
-
#
|
97
|
-
def validates_is_number(*fields)
|
98
|
-
opts = opts_from_validator_args(fields)
|
99
|
-
add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator)
|
100
|
-
end
|
101
|
-
|
102
|
-
end # module ValidatesIsNumber
|
103
|
-
end # module Validation
|
104
|
-
end # module CouchRest
|
@@ -1,109 +0,0 @@
|
|
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 RequiredFieldValidator < GenericValidator
|
32
|
-
|
33
|
-
def initialize(field_name, options={})
|
34
|
-
super
|
35
|
-
@field_name, @options = field_name, options
|
36
|
-
end
|
37
|
-
|
38
|
-
def call(target)
|
39
|
-
value = target.validation_property_value(field_name)
|
40
|
-
property = target.validation_property(field_name)
|
41
|
-
return true if present?(value, property)
|
42
|
-
|
43
|
-
error_message = @options[:message] || default_error(property)
|
44
|
-
add_error(target, error_message, field_name)
|
45
|
-
|
46
|
-
false
|
47
|
-
end
|
48
|
-
|
49
|
-
protected
|
50
|
-
|
51
|
-
# Boolean property types are considered present if non-nil.
|
52
|
-
# Other property types are considered present if non-blank.
|
53
|
-
# Non-properties are considered present if non-blank.
|
54
|
-
def present?(value, property)
|
55
|
-
boolean_type?(property) ? !value.nil? : !value.blank?
|
56
|
-
end
|
57
|
-
|
58
|
-
def default_error(property)
|
59
|
-
actual = boolean_type?(property) ? :nil : :blank
|
60
|
-
ValidationErrors.default_error_message(actual, field_name)
|
61
|
-
end
|
62
|
-
|
63
|
-
# Is +property+ a boolean property?
|
64
|
-
#
|
65
|
-
# Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties.
|
66
|
-
# Returns false for other property types.
|
67
|
-
# Returns false for non-properties.
|
68
|
-
def boolean_type?(property)
|
69
|
-
property ? property.type == TrueClass : false
|
70
|
-
end
|
71
|
-
|
72
|
-
end # class RequiredFieldValidator
|
73
|
-
|
74
|
-
module ValidatesPresent
|
75
|
-
|
76
|
-
##
|
77
|
-
# Validates that the specified attribute is present.
|
78
|
-
#
|
79
|
-
# For most property types "being present" is the same as being "not
|
80
|
-
# blank" as determined by the attribute's #blank? method. However, in
|
81
|
-
# the case of Boolean, "being present" means not nil; i.e. true or
|
82
|
-
# false.
|
83
|
-
#
|
84
|
-
# @note
|
85
|
-
# dm-core's support lib adds the blank? method to many classes,
|
86
|
-
# @see lib/dm-core/support/blank.rb (dm-core) for more information.
|
87
|
-
#
|
88
|
-
# @example [Usage]
|
89
|
-
#
|
90
|
-
# class Page
|
91
|
-
#
|
92
|
-
# property :required_attribute, String
|
93
|
-
# property :another_required, String
|
94
|
-
# property :yet_again, String
|
95
|
-
#
|
96
|
-
# validates_present :required_attribute
|
97
|
-
# validates_present :another_required, :yet_again
|
98
|
-
#
|
99
|
-
# # a call to valid? will return false unless
|
100
|
-
# # all three attributes are !blank?
|
101
|
-
# end
|
102
|
-
def validates_present(*fields)
|
103
|
-
opts = opts_from_validator_args(fields)
|
104
|
-
add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
|
105
|
-
end
|
106
|
-
|
107
|
-
end # module ValidatesPresent
|
108
|
-
end # module Validation
|
109
|
-
end # module CouchRest
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
-
|
3
|
-
describe CouchRest::Server do
|
4
|
-
|
5
|
-
describe "available databases" do
|
6
|
-
before(:each) do
|
7
|
-
@couch = CouchRest::Server.new
|
8
|
-
end
|
9
|
-
|
10
|
-
after(:each) do
|
11
|
-
@couch.available_databases.each do |ref, db|
|
12
|
-
db.delete!
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should let you add more databases" do
|
17
|
-
@couch.available_databases.should be_empty
|
18
|
-
@couch.define_available_database(:default, "cr-server-test-db")
|
19
|
-
@couch.available_databases.keys.should include(:default)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should verify that a database is available" do
|
23
|
-
@couch.define_available_database(:default, "cr-server-test-db")
|
24
|
-
@couch.available_database?(:default).should be_true
|
25
|
-
@couch.available_database?("cr-server-test-db").should be_true
|
26
|
-
@couch.available_database?(:matt).should be_false
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should let you set a default database" do
|
30
|
-
@couch.default_database = 'cr-server-test-default-db'
|
31
|
-
@couch.available_database?(:default).should be_true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
|
-
require File.join(FIXTURE_PATH, 'more', 'card')
|
3
|
-
|
4
|
-
class Car < CouchRest::ExtendedDocument
|
5
|
-
use_database TEST_SERVER.default_database
|
6
|
-
|
7
|
-
property :name
|
8
|
-
property :driver, :cast_as => 'Driver'
|
9
|
-
end
|
10
|
-
|
11
|
-
class Driver < CouchRest::ExtendedDocument
|
12
|
-
use_database TEST_SERVER.default_database
|
13
|
-
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
|
14
|
-
attr_accessor :casted_by
|
15
|
-
|
16
|
-
property :name
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "casting an extended document" do
|
20
|
-
|
21
|
-
before(:each) do
|
22
|
-
@car = Car.new(:name => 'Renault 306')
|
23
|
-
@driver = Driver.new(:name => 'Matt')
|
24
|
-
end
|
25
|
-
|
26
|
-
# it "should not create an empty casted object" do
|
27
|
-
# @car.driver.should be_nil
|
28
|
-
# end
|
29
|
-
|
30
|
-
it "should let you assign the casted attribute after instantializing an object" do
|
31
|
-
@car.driver = @driver
|
32
|
-
@car.driver.name.should == 'Matt'
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should let the casted document who casted it" do
|
36
|
-
Car.new(:name => 'Renault 306', :driver => @driver)
|
37
|
-
@car.driver.casted_by.should == @car
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|