samflores-couchrest 0.12.3 → 0.12.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +34 -0
- data/lib/couchrest/monkeypatches.rb +1 -1
- metadata +1 -2
- data/lib/couchrest/core/validations.rb +0 -328
data/Rakefile
CHANGED
@@ -46,6 +46,40 @@ task :gemspec do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
# desc "Update Github Gemspec"
|
50
|
+
# task :gemspec do
|
51
|
+
# skip_fields = %w(new_platform original_platform)
|
52
|
+
# integer_fields = %w(specification_version)
|
53
|
+
#
|
54
|
+
# result = "Gem::Specification.new do |s|\n"
|
55
|
+
# spec.instance_variables.each do |ivar|
|
56
|
+
# value = spec.instance_variable_get(ivar)
|
57
|
+
# name = ivar.split("@").last
|
58
|
+
# next if skip_fields.include?(name) || value.nil? || value == "" || (value.respond_to?(:empty?) && value.empty?)
|
59
|
+
# if name == "dependencies"
|
60
|
+
# value.each do |d|
|
61
|
+
# dep, *ver = d.to_s.split(" ")
|
62
|
+
# result << " s.add_dependency #{dep.inspect}, [#{ /\(([^\,]*)/ . match(ver.join(" "))[1].inspect}]\n"
|
63
|
+
# end
|
64
|
+
# else
|
65
|
+
# case value
|
66
|
+
# when Array
|
67
|
+
# value = name != "files" ? value.inspect : value.inspect.split(",").join(",\n")
|
68
|
+
# when Fixnum
|
69
|
+
# # leave as-is
|
70
|
+
# when String
|
71
|
+
# value = value.to_i if integer_fields.include?(name)
|
72
|
+
# value = value.inspect
|
73
|
+
# else
|
74
|
+
# value = value.to_s.inspect
|
75
|
+
# end
|
76
|
+
# result << " s.#{name} = #{value}\n"
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
# result << "end"
|
80
|
+
# File.open(File.join(File.dirname(__FILE__), "#{spec.name}.gemspec"), "w"){|f| f << result}
|
81
|
+
# end
|
82
|
+
|
49
83
|
desc "Run all specs"
|
50
84
|
Spec::Rake::SpecTask.new('spec') do |t|
|
51
85
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: samflores-couchrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.12.3
|
4
|
+
version: 0.12.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J. Chris Anderson
|
@@ -90,7 +90,6 @@ files:
|
|
90
90
|
- lib/couchrest/core/document.rb
|
91
91
|
- lib/couchrest/core/model.rb
|
92
92
|
- lib/couchrest/core/server.rb
|
93
|
-
- lib/couchrest/core/validations.rb
|
94
93
|
- lib/couchrest/core/view.rb
|
95
94
|
- lib/couchrest/helper
|
96
95
|
- lib/couchrest/helper/pager.rb
|
@@ -1,328 +0,0 @@
|
|
1
|
-
module CouchRest
|
2
|
-
|
3
|
-
class RecordInvalid < StandardError
|
4
|
-
attr_reader :record
|
5
|
-
def initialize(record)
|
6
|
-
@record = record
|
7
|
-
super("Validation failed: #{@self.errors.full_messages.join(", ")}")
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class Errors
|
12
|
-
include Enumerable
|
13
|
-
|
14
|
-
def initialize(base) # :nodoc:
|
15
|
-
@base, @errors = base, {}
|
16
|
-
end
|
17
|
-
|
18
|
-
@@default_error_messages = {
|
19
|
-
:inclusion => "is not included in the list",
|
20
|
-
:exclusion => "is reserved",
|
21
|
-
:invalid => "is invalid",
|
22
|
-
:confirmation => "doesn't match confirmation",
|
23
|
-
:accepted => "must be accepted",
|
24
|
-
:empty => "can't be empty",
|
25
|
-
:blank => "can't be blank",
|
26
|
-
:too_long => "is too long (maximum is %d characters)",
|
27
|
-
:too_short => "is too short (minimum is %d characters)",
|
28
|
-
:wrong_length => "is the wrong length (should be %d characters)",
|
29
|
-
:taken => "has already been taken",
|
30
|
-
:not_a_number => "is not a number",
|
31
|
-
:greater_than => "must be greater than %d",
|
32
|
-
:greater_than_or_equal_to => "must be greater than or equal to %d",
|
33
|
-
:equal_to => "must be equal to %d",
|
34
|
-
:less_than => "must be less than %d",
|
35
|
-
:less_than_or_equal_to => "must be less than or equal to %d",
|
36
|
-
:odd => "must be odd",
|
37
|
-
:even => "must be even",
|
38
|
-
:kind => "must be of kind: %s"
|
39
|
-
}
|
40
|
-
|
41
|
-
cattr_accessor :default_error_messages
|
42
|
-
|
43
|
-
def add_to_base(msg)
|
44
|
-
add(:base, msg)
|
45
|
-
end
|
46
|
-
|
47
|
-
def add(attribute, msg = @@default_error_messages[:invalid])
|
48
|
-
@errors[attribute.to_s] ||= []
|
49
|
-
@errors[attribute.to_s] << msg
|
50
|
-
end
|
51
|
-
|
52
|
-
def add_on_empty(attributes, msg = @@default_error_messages[:empty])
|
53
|
-
for attr in [attributes].flatten
|
54
|
-
value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
|
55
|
-
is_empty = value.respond_to?("empty?") ? value.empty? : false
|
56
|
-
add(attr, msg) unless !value.nil? && !is_empty
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def add_on_blank(attributes, msg = @@default_error_messages[:blank])
|
61
|
-
for attr in [attributes].flatten
|
62
|
-
value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
|
63
|
-
add(attr, msg) if value.blank?
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def invalid?(attribute)
|
68
|
-
!@errors[attribute.to_s].nil?
|
69
|
-
end
|
70
|
-
|
71
|
-
def on(attribute)
|
72
|
-
errors = @errors[attribute.to_s]
|
73
|
-
return nil if errors.nil?
|
74
|
-
errors.size == 1 ? errors.first : errors
|
75
|
-
end
|
76
|
-
|
77
|
-
alias :[] :on
|
78
|
-
|
79
|
-
def on_base
|
80
|
-
on(:base)
|
81
|
-
end
|
82
|
-
|
83
|
-
def each
|
84
|
-
@errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
|
85
|
-
end
|
86
|
-
|
87
|
-
def each_full
|
88
|
-
full_messages.each { |msg| yield msg }
|
89
|
-
end
|
90
|
-
|
91
|
-
def full_messages
|
92
|
-
full_messages = []
|
93
|
-
|
94
|
-
@errors.each_key do |attr|
|
95
|
-
@errors[attr].each do |msg|
|
96
|
-
next if msg.nil?
|
97
|
-
|
98
|
-
if attr == "base"
|
99
|
-
full_messages << msg
|
100
|
-
else
|
101
|
-
full_messages << @base.class.human_attribute_name(attr) + " " + msg
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
full_messages
|
106
|
-
end
|
107
|
-
|
108
|
-
def empty?
|
109
|
-
@errors.empty?
|
110
|
-
end
|
111
|
-
|
112
|
-
def clear
|
113
|
-
@errors = {}
|
114
|
-
end
|
115
|
-
|
116
|
-
def size
|
117
|
-
@errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
|
118
|
-
end
|
119
|
-
|
120
|
-
alias_method :count, :size
|
121
|
-
alias_method :length, :size
|
122
|
-
|
123
|
-
def to_xml(options={})
|
124
|
-
options[:root] ||= "errors"
|
125
|
-
options[:indent] ||= 2
|
126
|
-
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
|
127
|
-
|
128
|
-
options[:builder].instruct! unless options.delete(:skip_instruct)
|
129
|
-
options[:builder].errors do |e|
|
130
|
-
full_messages.each { |msg| e.error(msg) }
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
module Validations
|
136
|
-
VALIDATIONS = %w( validate validate_on_create validate_on_update )
|
137
|
-
|
138
|
-
def self.included(base) # :nodoc:
|
139
|
-
base.extend ClassMethods
|
140
|
-
base.class_eval do
|
141
|
-
alias_method_chain :save, :validation
|
142
|
-
# alias_method_chain :save!, :validation
|
143
|
-
# alias_method_chain :update_attributes, :validation_skipping
|
144
|
-
end
|
145
|
-
|
146
|
-
base.send :include, ActiveSupport::Callbacks
|
147
|
-
base.define_callbacks *VALIDATIONS
|
148
|
-
end
|
149
|
-
|
150
|
-
module ClassMethods
|
151
|
-
DEFAULT_VALIDATION_OPTIONS = {
|
152
|
-
:on => :save,
|
153
|
-
:allow_nil => false,
|
154
|
-
:allow_blank => false,
|
155
|
-
:message => nil
|
156
|
-
}.freeze
|
157
|
-
|
158
|
-
ALL_RANGE_OPTIONS = [ :is, :within, :in, :minimum, :maximum ].freeze
|
159
|
-
ALL_NUMERICALITY_CHECKS = { :greater_than => '>', :greater_than_or_equal_to => '>=',
|
160
|
-
:equal_to => '==', :less_than => '<', :less_than_or_equal_to => '<=',
|
161
|
-
:odd => 'odd?', :even => 'even?' }.freeze
|
162
|
-
|
163
|
-
def validates_each(*attrs)
|
164
|
-
options = attrs.extract_options!.symbolize_keys
|
165
|
-
attrs = attrs.flatten
|
166
|
-
|
167
|
-
send(validation_method(options[:on] || :save), options) do |record|
|
168
|
-
attrs.each do |attr|
|
169
|
-
value = record.send(attr)
|
170
|
-
next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
|
171
|
-
yield record, attr, value
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def validates_presence_of(*attr_names)
|
177
|
-
configuration = { :message => CouchRest::Errors.default_error_messages[:blank], :on => :save }
|
178
|
-
configuration.update(attr_names.extract_options!)
|
179
|
-
|
180
|
-
send(validation_method(configuration[:on]), configuration) do |record|
|
181
|
-
record.errors.add_on_blank(attr_names, configuration[:message])
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
def validates_format_of(*attr_names)
|
186
|
-
configuration = { :message => CouchRest::Errors.default_error_messages[:invalid], :on => :save, :with => nil }
|
187
|
-
configuration.update(attr_names.extract_options!)
|
188
|
-
|
189
|
-
raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp)
|
190
|
-
|
191
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
192
|
-
record.errors.add(attr_name, configuration[:message] % value) unless value.to_s =~ configuration[:with]
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
def validates_kind_of(*attr_names)
|
197
|
-
configuration = { :message => CouchRest::Errors.default_error_messages[:kind], :on => :save, :is => nil, :allow_nil => true }
|
198
|
-
configuration.update(attr_names.extract_options!)
|
199
|
-
|
200
|
-
raise(ArgumentError, "A class must be supplied as the :as option of the configuration hash") unless configuration[:is].is_a?(Class)
|
201
|
-
|
202
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
203
|
-
record.errors.add(attr_name, configuration[:message] % configuration[:is]) unless value.class == configuration[:is]
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
def validates_numericality_of(*attr_names)
|
208
|
-
configuration = { :message => CouchRest::Errors.default_error_messages[:not_a_number], :only_integer => false, :allow_nil => false, :on => :save }
|
209
|
-
configuration.update(attr_names.extract_options!)
|
210
|
-
|
211
|
-
numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys
|
212
|
-
|
213
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
214
|
-
next if value.nil? and configuration[:allow_nil]
|
215
|
-
if configuration[:only_integer]
|
216
|
-
unless value.to_s =~ /\A[+-]?\d+\Z/
|
217
|
-
record.errors.add(attr_name, configuration[:message] % value)
|
218
|
-
next
|
219
|
-
end
|
220
|
-
else
|
221
|
-
begin
|
222
|
-
Kernel.Float(value)
|
223
|
-
rescue ArgumentError, TypeError
|
224
|
-
record.errors.add(attr_name, configuration[:message] % value)
|
225
|
-
next
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
numericality_options.each do |option|
|
230
|
-
case option
|
231
|
-
when :odd, :even
|
232
|
-
record.errors.add(attr_name, CouchRest::Errors.default_error_messages[option] % value) unless value.send(ALL_NUMERICALITY_CHECKS[option])
|
233
|
-
else
|
234
|
-
|
235
|
-
record.errors.add(attr_name, CouchRest::Errors.default_error_messages[option] % configuration[option]) unless value.send(ALL_NUMERICALITY_CHECKS[option], configuration[option])
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
def validates_inclusion_of(*attr_names)
|
242
|
-
configuration = { :message => CouchRest::Errors.default_error_messages[:inclusion], :on => :save }
|
243
|
-
configuration.update(attr_name.extract_options!)
|
244
|
-
|
245
|
-
enum = configuration[:in] || configuration[:within]
|
246
|
-
|
247
|
-
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?(:include?)
|
248
|
-
|
249
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
250
|
-
record.errors.add(attr_name, configuration[:message]) if enum.include?(value)
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
def create!(attributes = nil, &block)
|
255
|
-
if attributes.is_a?(Array)
|
256
|
-
attributes.collect { |attr| create!(attr, &block) }
|
257
|
-
else
|
258
|
-
object = new(attributes)
|
259
|
-
yield(object) if block_given?
|
260
|
-
object.save!
|
261
|
-
object
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
private
|
266
|
-
def validation_method(on)
|
267
|
-
case on
|
268
|
-
when :save then :validate
|
269
|
-
when :create then :validate_on_create
|
270
|
-
when :update then :validate_on_update
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
def save_with_validation(perform_validation = true)
|
276
|
-
if perform_validation && valid? || !perform_validation
|
277
|
-
save_without_validation
|
278
|
-
else
|
279
|
-
false
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
def save_with_validation!
|
284
|
-
if valid?
|
285
|
-
save_without_validation!
|
286
|
-
else
|
287
|
-
raise RecordInvalid.new(self)
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
def update_attribute_with_validation_skipping(name, value)
|
292
|
-
send(name.to_s + '=', value)
|
293
|
-
save(false)
|
294
|
-
end
|
295
|
-
|
296
|
-
def valid?
|
297
|
-
errors.clear
|
298
|
-
|
299
|
-
run_callbacks(:validate)
|
300
|
-
validate
|
301
|
-
|
302
|
-
if new_record?
|
303
|
-
run_callbacks(:validate_on_create)
|
304
|
-
validate_on_create
|
305
|
-
else
|
306
|
-
run_callbacks(:validate_on_update)
|
307
|
-
validate_on_update
|
308
|
-
end
|
309
|
-
|
310
|
-
errors.empty?
|
311
|
-
end
|
312
|
-
|
313
|
-
def errors
|
314
|
-
@errors ||= Errors.new(self)
|
315
|
-
end
|
316
|
-
|
317
|
-
protected
|
318
|
-
|
319
|
-
def validate #:doc:
|
320
|
-
end
|
321
|
-
|
322
|
-
def validate_on_create #:doc:
|
323
|
-
end
|
324
|
-
|
325
|
-
def validate_on_update # :doc:
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|