couchrest 0.38 → 1.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -8
- data/Rakefile +3 -4
- data/couchrest.gemspec +25 -105
- data/history.txt +5 -4
- data/lib/couchrest.rb +31 -52
- data/lib/couchrest/{core/database.rb → database.rb} +6 -11
- data/lib/couchrest/{core/design.rb → design.rb} +2 -2
- data/lib/couchrest/{core/document.rb → document.rb} +1 -1
- data/lib/couchrest/helper/attachments.rb +29 -0
- data/lib/couchrest/middlewares/logger.rb +3 -3
- data/lib/couchrest/monkeypatches.rb +1 -71
- data/lib/couchrest/{core/response.rb → response.rb} +0 -0
- data/lib/couchrest/{core/rest_api.rb → rest_api.rb} +8 -12
- data/lib/couchrest/{core/server.rb → server.rb} +0 -2
- data/spec/couchrest/{core/couchrest_spec.rb → couchrest_spec.rb} +15 -9
- data/spec/couchrest/{core/database_spec.rb → database_spec.rb} +4 -4
- data/spec/couchrest/{core/design_spec.rb → design_spec.rb} +2 -2
- data/spec/couchrest/{core/document_spec.rb → document_spec.rb} +1 -1
- data/spec/couchrest/{core/server_spec.rb → server_spec.rb} +2 -2
- data/spec/spec.opts +0 -1
- data/spec/spec_helper.rb +0 -4
- metadata +32 -133
- data/examples/model/example.rb +0 -144
- data/lib/couchrest/core/adapters/restclient.rb +0 -35
- data/lib/couchrest/core/http_abstraction.rb +0 -48
- data/lib/couchrest/core/view.rb +0 -4
- data/lib/couchrest/mixins.rb +0 -4
- data/lib/couchrest/mixins/attachments.rb +0 -31
- data/lib/couchrest/mixins/attribute_protection.rb +0 -74
- data/lib/couchrest/mixins/callbacks.rb +0 -532
- data/lib/couchrest/mixins/class_proxy.rb +0 -124
- data/lib/couchrest/mixins/collection.rb +0 -260
- data/lib/couchrest/mixins/design_doc.rb +0 -103
- data/lib/couchrest/mixins/document_queries.rb +0 -80
- data/lib/couchrest/mixins/extended_attachments.rb +0 -70
- data/lib/couchrest/mixins/extended_document_mixins.rb +0 -9
- data/lib/couchrest/mixins/properties.rb +0 -158
- data/lib/couchrest/mixins/validation.rb +0 -246
- data/lib/couchrest/mixins/views.rb +0 -173
- data/lib/couchrest/more/casted_model.rb +0 -58
- data/lib/couchrest/more/extended_document.rb +0 -310
- data/lib/couchrest/more/property.rb +0 -58
- data/lib/couchrest/more/typecast.rb +0 -180
- data/lib/couchrest/support/blank.rb +0 -42
- data/lib/couchrest/support/rails.rb +0 -42
- data/lib/couchrest/validation/auto_validate.rb +0 -157
- data/lib/couchrest/validation/contextual_validators.rb +0 -78
- data/lib/couchrest/validation/validation_errors.rb +0 -125
- data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
- data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -107
- data/lib/couchrest/validation/validators/format_validator.rb +0 -122
- 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 -139
- data/lib/couchrest/validation/validators/method_validator.rb +0 -89
- data/lib/couchrest/validation/validators/numeric_validator.rb +0 -109
- data/lib/couchrest/validation/validators/required_field_validator.rb +0 -114
- data/spec/couchrest/more/attribute_protection_spec.rb +0 -150
- data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -73
- data/spec/couchrest/more/casted_model_spec.rb +0 -406
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -135
- data/spec/couchrest/more/extended_doc_inherited_spec.rb +0 -40
- data/spec/couchrest/more/extended_doc_spec.rb +0 -807
- data/spec/couchrest/more/extended_doc_subclass_spec.rb +0 -98
- data/spec/couchrest/more/extended_doc_view_spec.rb +0 -456
- data/spec/couchrest/more/property_spec.rb +0 -628
- data/spec/fixtures/more/article.rb +0 -35
- data/spec/fixtures/more/card.rb +0 -22
- data/spec/fixtures/more/cat.rb +0 -20
- data/spec/fixtures/more/course.rb +0 -22
- data/spec/fixtures/more/event.rb +0 -8
- data/spec/fixtures/more/invoice.rb +0 -17
- data/spec/fixtures/more/person.rb +0 -9
- data/spec/fixtures/more/question.rb +0 -6
- data/spec/fixtures/more/service.rb +0 -12
- data/spec/fixtures/more/user.rb +0 -22
@@ -1,80 +0,0 @@
|
|
1
|
-
module CouchRest
|
2
|
-
module Mixins
|
3
|
-
module DocumentQueries
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.extend(ClassMethods)
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
|
11
|
-
# Load all documents that have the "couchrest-type" field equal to the
|
12
|
-
# name of the current class. Take the standard set of
|
13
|
-
# CouchRest::Database#view options.
|
14
|
-
def all(opts = {}, &block)
|
15
|
-
view(:all, opts, &block)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Returns the number of documents that have the "couchrest-type" field
|
19
|
-
# equal to the name of the current class. Takes the standard set of
|
20
|
-
# CouchRest::Database#view options
|
21
|
-
def count(opts = {}, &block)
|
22
|
-
all({:raw => true, :limit => 0}.merge(opts), &block)['total_rows']
|
23
|
-
end
|
24
|
-
|
25
|
-
# Load the first document that have the "couchrest-type" field equal to
|
26
|
-
# the name of the current class.
|
27
|
-
#
|
28
|
-
# ==== Returns
|
29
|
-
# Object:: The first object instance available
|
30
|
-
# or
|
31
|
-
# Nil:: if no instances available
|
32
|
-
#
|
33
|
-
# ==== Parameters
|
34
|
-
# opts<Hash>::
|
35
|
-
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
|
36
|
-
def first(opts = {})
|
37
|
-
first_instance = self.all(opts.merge!(:limit => 1))
|
38
|
-
first_instance.empty? ? nil : first_instance.first
|
39
|
-
end
|
40
|
-
|
41
|
-
# Load a document from the database by id
|
42
|
-
# No exceptions will be raised if the document isn't found
|
43
|
-
#
|
44
|
-
# ==== Returns
|
45
|
-
# Object:: if the document was found
|
46
|
-
# or
|
47
|
-
# Nil::
|
48
|
-
#
|
49
|
-
# === Parameters
|
50
|
-
# id<String, Integer>:: Document ID
|
51
|
-
# db<Database>:: optional option to pass a custom database to use
|
52
|
-
def get(id, db = database)
|
53
|
-
begin
|
54
|
-
get!(id, db)
|
55
|
-
rescue
|
56
|
-
nil
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# Load a document from the database by id
|
61
|
-
# An exception will be raised if the document isn't found
|
62
|
-
#
|
63
|
-
# ==== Returns
|
64
|
-
# Object:: if the document was found
|
65
|
-
# or
|
66
|
-
# Exception
|
67
|
-
#
|
68
|
-
# === Parameters
|
69
|
-
# id<String, Integer>:: Document ID
|
70
|
-
# db<Database>:: optional option to pass a custom database to use
|
71
|
-
def get!(id, db = database)
|
72
|
-
doc = db.get id
|
73
|
-
create_from_database(doc)
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
module CouchRest
|
2
|
-
module Mixins
|
3
|
-
module ExtendedAttachments
|
4
|
-
|
5
|
-
# creates a file attachment to the current doc
|
6
|
-
def create_attachment(args={})
|
7
|
-
raise ArgumentError unless args[:file] && args[:name]
|
8
|
-
return if has_attachment?(args[:name])
|
9
|
-
self['_attachments'] ||= {}
|
10
|
-
set_attachment_attr(args)
|
11
|
-
rescue ArgumentError => e
|
12
|
-
raise ArgumentError, 'You must specify :file and :name'
|
13
|
-
end
|
14
|
-
|
15
|
-
# reads the data from an attachment
|
16
|
-
def read_attachment(attachment_name)
|
17
|
-
database.fetch_attachment(self, attachment_name)
|
18
|
-
end
|
19
|
-
|
20
|
-
# modifies a file attachment on the current doc
|
21
|
-
def update_attachment(args={})
|
22
|
-
raise ArgumentError unless args[:file] && args[:name]
|
23
|
-
return unless has_attachment?(args[:name])
|
24
|
-
delete_attachment(args[:name])
|
25
|
-
set_attachment_attr(args)
|
26
|
-
rescue ArgumentError => e
|
27
|
-
raise ArgumentError, 'You must specify :file and :name'
|
28
|
-
end
|
29
|
-
|
30
|
-
# deletes a file attachment from the current doc
|
31
|
-
def delete_attachment(attachment_name)
|
32
|
-
return unless self['_attachments']
|
33
|
-
self['_attachments'].delete attachment_name
|
34
|
-
end
|
35
|
-
|
36
|
-
# returns true if attachment_name exists
|
37
|
-
def has_attachment?(attachment_name)
|
38
|
-
!!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
|
39
|
-
end
|
40
|
-
|
41
|
-
# returns URL to fetch the attachment from
|
42
|
-
def attachment_url(attachment_name)
|
43
|
-
return unless has_attachment?(attachment_name)
|
44
|
-
"#{database.root}/#{self.id}/#{attachment_name}"
|
45
|
-
end
|
46
|
-
|
47
|
-
# returns URI to fetch the attachment from
|
48
|
-
def attachment_uri(attachment_name)
|
49
|
-
return unless has_attachment?(attachment_name)
|
50
|
-
"#{database.uri}/#{self.id}/#{attachment_name}"
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def get_mime_type(file)
|
56
|
-
::MIME::Types.type_for(file.path).empty? ?
|
57
|
-
'text\/plain' : MIME::Types.type_for(file.path).first.content_type.gsub(/\//,'\/')
|
58
|
-
end
|
59
|
-
|
60
|
-
def set_attachment_attr(args)
|
61
|
-
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file])
|
62
|
-
self['_attachments'][args[:name]] = {
|
63
|
-
'content_type' => content_type,
|
64
|
-
'data' => args[:file].read
|
65
|
-
}
|
66
|
-
end
|
67
|
-
|
68
|
-
end # module ExtendedAttachments
|
69
|
-
end
|
70
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'properties')
|
2
|
-
require File.join(File.dirname(__FILE__), 'document_queries')
|
3
|
-
require File.join(File.dirname(__FILE__), 'views')
|
4
|
-
require File.join(File.dirname(__FILE__), 'design_doc')
|
5
|
-
require File.join(File.dirname(__FILE__), 'validation')
|
6
|
-
require File.join(File.dirname(__FILE__), 'extended_attachments')
|
7
|
-
require File.join(File.dirname(__FILE__), 'class_proxy')
|
8
|
-
require File.join(File.dirname(__FILE__), 'collection')
|
9
|
-
require File.join(File.dirname(__FILE__), 'attribute_protection')
|
@@ -1,158 +0,0 @@
|
|
1
|
-
require 'time'
|
2
|
-
require File.join(File.dirname(__FILE__), '..', 'more', 'property')
|
3
|
-
require File.join(File.dirname(__FILE__), '..', 'more', 'typecast')
|
4
|
-
|
5
|
-
module CouchRest
|
6
|
-
module Mixins
|
7
|
-
module Properties
|
8
|
-
|
9
|
-
class IncludeError < StandardError; end
|
10
|
-
|
11
|
-
include ::CouchRest::More::Typecast
|
12
|
-
|
13
|
-
def self.included(base)
|
14
|
-
base.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
15
|
-
extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties)
|
16
|
-
self.properties ||= []
|
17
|
-
EOS
|
18
|
-
base.extend(ClassMethods)
|
19
|
-
raise CouchRest::Mixins::Properties::IncludeError, "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (base.new.respond_to?(:[]) && base.new.respond_to?(:[]=))
|
20
|
-
end
|
21
|
-
|
22
|
-
def apply_defaults
|
23
|
-
return if self.respond_to?(:new?) && (new? == false)
|
24
|
-
return unless self.class.respond_to?(:properties)
|
25
|
-
return if self.class.properties.empty?
|
26
|
-
# TODO: cache the default object
|
27
|
-
self.class.properties.each do |property|
|
28
|
-
key = property.name.to_s
|
29
|
-
# let's make sure we have a default
|
30
|
-
unless property.default.nil?
|
31
|
-
if property.default.class == Proc
|
32
|
-
self[key] = property.default.call
|
33
|
-
else
|
34
|
-
self[key] = Marshal.load(Marshal.dump(property.default))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def cast_keys
|
41
|
-
return unless self.class.properties
|
42
|
-
self.class.properties.each do |property|
|
43
|
-
cast_property(property)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def cast_property(property, assigned=false)
|
48
|
-
return unless property.casted
|
49
|
-
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
50
|
-
# Don't cast the property unless it has a value
|
51
|
-
return unless self[key]
|
52
|
-
if property.type.is_a?(Array)
|
53
|
-
klass = ::CouchRest.constantize(property.type[0])
|
54
|
-
self[key] = [self[key]] unless self[key].is_a?(Array)
|
55
|
-
arr = self[key].collect do |value|
|
56
|
-
value = typecast_value(value, klass, property.init_method)
|
57
|
-
associate_casted_to_parent(value, assigned)
|
58
|
-
value
|
59
|
-
end
|
60
|
-
self[key] = klass != String ? CastedArray.new(arr) : arr
|
61
|
-
self[key].casted_by = self if self[key].respond_to?(:casted_by)
|
62
|
-
else
|
63
|
-
if property.type.downcase == 'boolean'
|
64
|
-
klass = TrueClass
|
65
|
-
else
|
66
|
-
klass = ::CouchRest.constantize(property.type)
|
67
|
-
end
|
68
|
-
|
69
|
-
self[key] = typecast_value(self[key], klass, property.init_method)
|
70
|
-
associate_casted_to_parent(self[key], assigned)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def associate_casted_to_parent(casted, assigned)
|
75
|
-
casted.casted_by = self if casted.respond_to?(:casted_by)
|
76
|
-
casted.document_saved = true if !assigned && casted.respond_to?(:document_saved)
|
77
|
-
end
|
78
|
-
|
79
|
-
def cast_property_by_name(property_name)
|
80
|
-
return unless self.class.properties
|
81
|
-
property = self.class.properties.detect{|property| property.name == property_name}
|
82
|
-
return unless property
|
83
|
-
cast_property(property, true)
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
module ClassMethods
|
88
|
-
|
89
|
-
def property(name, options={})
|
90
|
-
existing_property = self.properties.find{|p| p.name == name.to_s}
|
91
|
-
if existing_property.nil? || (existing_property.default != options[:default])
|
92
|
-
define_property(name, options)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
protected
|
97
|
-
|
98
|
-
# This is not a thread safe operation, if you have to set new properties at runtime
|
99
|
-
# make sure to use a mutex.
|
100
|
-
def define_property(name, options={})
|
101
|
-
# check if this property is going to casted
|
102
|
-
options[:casted] = !!(options[:cast_as] || options[:type])
|
103
|
-
property = CouchRest::Property.new(name, (options.delete(:cast_as) || options.delete(:type)), options)
|
104
|
-
create_property_getter(property)
|
105
|
-
create_property_setter(property) unless property.read_only == true
|
106
|
-
properties << property
|
107
|
-
end
|
108
|
-
|
109
|
-
# defines the getter for the property (and optional aliases)
|
110
|
-
def create_property_getter(property)
|
111
|
-
# meth = property.name
|
112
|
-
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
113
|
-
def #{property.name}
|
114
|
-
self['#{property.name}']
|
115
|
-
end
|
116
|
-
EOS
|
117
|
-
|
118
|
-
if property.type == 'boolean'
|
119
|
-
class_eval <<-EOS, __FILE__, __LINE__
|
120
|
-
def #{property.name}?
|
121
|
-
if self['#{property.name}'].nil? || self['#{property.name}'] == false || self['#{property.name}'].to_s.downcase == 'false'
|
122
|
-
false
|
123
|
-
else
|
124
|
-
true
|
125
|
-
end
|
126
|
-
end
|
127
|
-
EOS
|
128
|
-
end
|
129
|
-
|
130
|
-
if property.alias
|
131
|
-
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
132
|
-
alias #{property.alias.to_sym} #{property.name.to_sym}
|
133
|
-
EOS
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
# defines the setter for the property (and optional aliases)
|
138
|
-
def create_property_setter(property)
|
139
|
-
property_name = property.name
|
140
|
-
class_eval <<-EOS
|
141
|
-
def #{property_name}=(value)
|
142
|
-
self['#{property_name}'] = value
|
143
|
-
cast_property_by_name('#{property_name}')
|
144
|
-
end
|
145
|
-
EOS
|
146
|
-
|
147
|
-
if property.alias
|
148
|
-
class_eval <<-EOS
|
149
|
-
alias #{property.alias.to_sym}= #{property_name.to_sym}=
|
150
|
-
EOS
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
end # module ClassMethods
|
155
|
-
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
@@ -1,246 +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
|
-
class Object
|
25
|
-
def validatable?
|
26
|
-
false
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
require 'pathname'
|
31
|
-
require File.join(File.dirname(__FILE__), '..', 'support', 'class')
|
32
|
-
|
33
|
-
dir = File.join(Pathname(__FILE__).dirname.expand_path, '..', 'validation')
|
34
|
-
|
35
|
-
require File.join(dir, 'validation_errors')
|
36
|
-
require File.join(dir, 'contextual_validators')
|
37
|
-
require File.join(dir, 'auto_validate')
|
38
|
-
|
39
|
-
require File.join(dir, 'validators', 'generic_validator')
|
40
|
-
require File.join(dir, 'validators', 'required_field_validator')
|
41
|
-
require File.join(dir, 'validators', 'absent_field_validator')
|
42
|
-
require File.join(dir, 'validators', 'format_validator')
|
43
|
-
require File.join(dir, 'validators', 'length_validator')
|
44
|
-
require File.join(dir, 'validators', 'numeric_validator')
|
45
|
-
require File.join(dir, 'validators', 'method_validator')
|
46
|
-
require File.join(dir, 'validators', 'confirmation_validator')
|
47
|
-
|
48
|
-
module CouchRest
|
49
|
-
module Validation
|
50
|
-
|
51
|
-
def self.included(base)
|
52
|
-
base.extlib_inheritable_accessor(:auto_validation)
|
53
|
-
base.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
54
|
-
# Callbacks
|
55
|
-
define_callbacks :validate
|
56
|
-
|
57
|
-
# Turn off auto validation by default
|
58
|
-
self.auto_validation ||= false
|
59
|
-
|
60
|
-
# Force the auto validation for the class properties
|
61
|
-
# This feature is still not fully ported over,
|
62
|
-
# test are lacking, so please use with caution
|
63
|
-
def self.auto_validate!
|
64
|
-
self.auto_validation = true
|
65
|
-
end
|
66
|
-
|
67
|
-
# share the validations with subclasses
|
68
|
-
def self.inherited(subklass)
|
69
|
-
self.validators.contexts.each do |k, v|
|
70
|
-
subklass.validators.contexts[k] = v.dup
|
71
|
-
end
|
72
|
-
super
|
73
|
-
end
|
74
|
-
EOS
|
75
|
-
|
76
|
-
base.extend(ClassMethods)
|
77
|
-
base.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
78
|
-
define_callbacks :validate
|
79
|
-
if method_defined?(:_run_save_callbacks)
|
80
|
-
set_callback :save, :before, :check_validations
|
81
|
-
end
|
82
|
-
EOS
|
83
|
-
base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
84
|
-
def self.define_property(name, options={})
|
85
|
-
super
|
86
|
-
auto_generate_validations(properties.last) if properties && properties.size > 0
|
87
|
-
autovalidation_check = true
|
88
|
-
end
|
89
|
-
RUBY_EVAL
|
90
|
-
end
|
91
|
-
|
92
|
-
# Ensures the object is valid for the context provided, and otherwise
|
93
|
-
# throws :halt and returns false.
|
94
|
-
#
|
95
|
-
def check_validations(context = :default)
|
96
|
-
throw(:halt, false) unless context.nil? || valid?(context)
|
97
|
-
end
|
98
|
-
|
99
|
-
# Return the ValidationErrors
|
100
|
-
#
|
101
|
-
def errors
|
102
|
-
@errors ||= ValidationErrors.new
|
103
|
-
end
|
104
|
-
|
105
|
-
# Mark this resource as validatable. When we validate associations of a
|
106
|
-
# resource we can check if they respond to validatable? before trying to
|
107
|
-
# recursivly validate them
|
108
|
-
#
|
109
|
-
def validatable?
|
110
|
-
true
|
111
|
-
end
|
112
|
-
|
113
|
-
# Alias for valid?(:default)
|
114
|
-
#
|
115
|
-
def valid_for_default?
|
116
|
-
valid?(:default)
|
117
|
-
end
|
118
|
-
|
119
|
-
# Check if a resource is valid in a given context
|
120
|
-
#
|
121
|
-
def valid?(context = :default)
|
122
|
-
recursive_valid?(self, context, true)
|
123
|
-
end
|
124
|
-
|
125
|
-
# checking on casted objects
|
126
|
-
def validate_casted_arrays
|
127
|
-
result = true
|
128
|
-
array_casted_properties = self.class.properties.select { |property| property.casted && property.type.instance_of?(Array) }
|
129
|
-
array_casted_properties.each do |property|
|
130
|
-
casted_values = self.send(property.name)
|
131
|
-
next unless casted_values.is_a?(Array) && casted_values.first.respond_to?(:valid?)
|
132
|
-
casted_values.each do |value|
|
133
|
-
result = (result && value.valid?) if value.respond_to?(:valid?)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
result
|
137
|
-
end
|
138
|
-
|
139
|
-
# Do recursive validity checking
|
140
|
-
#
|
141
|
-
def recursive_valid?(target, context, state)
|
142
|
-
valid = state
|
143
|
-
target.each do |key, prop|
|
144
|
-
if prop.is_a?(Array)
|
145
|
-
prop.each do |item|
|
146
|
-
if item.validatable?
|
147
|
-
valid = recursive_valid?(item, context, valid) && valid
|
148
|
-
end
|
149
|
-
end
|
150
|
-
elsif prop.validatable?
|
151
|
-
valid = recursive_valid?(prop, context, valid) && valid
|
152
|
-
end
|
153
|
-
end
|
154
|
-
target._run_validate_callbacks do
|
155
|
-
target.class.validators.execute(context, target) && valid
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
def validation_property_value(name)
|
161
|
-
self.respond_to?(name, true) ? self.send(name) : nil
|
162
|
-
end
|
163
|
-
|
164
|
-
# Get the corresponding Object property, if it exists.
|
165
|
-
def validation_property(field_name)
|
166
|
-
properties.find{|p| p.name == field_name}
|
167
|
-
end
|
168
|
-
|
169
|
-
module ClassMethods
|
170
|
-
include CouchRest::Validation::ValidatesPresent
|
171
|
-
include CouchRest::Validation::ValidatesAbsent
|
172
|
-
include CouchRest::Validation::ValidatesIsConfirmed
|
173
|
-
# include CouchRest::Validation::ValidatesIsPrimitive
|
174
|
-
# include CouchRest::Validation::ValidatesIsAccepted
|
175
|
-
include CouchRest::Validation::ValidatesFormat
|
176
|
-
include CouchRest::Validation::ValidatesLength
|
177
|
-
# include CouchRest::Validation::ValidatesWithin
|
178
|
-
include CouchRest::Validation::ValidatesIsNumber
|
179
|
-
include CouchRest::Validation::ValidatesWithMethod
|
180
|
-
# include CouchRest::Validation::ValidatesWithBlock
|
181
|
-
# include CouchRest::Validation::ValidatesIsUnique
|
182
|
-
include CouchRest::Validation::AutoValidate
|
183
|
-
|
184
|
-
# Return the set of contextual validators or create a new one
|
185
|
-
#
|
186
|
-
def validators
|
187
|
-
@validations ||= ContextualValidators.new
|
188
|
-
end
|
189
|
-
|
190
|
-
# Clean up the argument list and return a opts hash, including the
|
191
|
-
# merging of any default opts. Set the context to default if none is
|
192
|
-
# provided. Also allow :context to be aliased to :on, :when & group
|
193
|
-
#
|
194
|
-
def opts_from_validator_args(args, defaults = nil)
|
195
|
-
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
196
|
-
context = :default
|
197
|
-
context = opts[:context] if opts.has_key?(:context)
|
198
|
-
context = opts.delete(:on) if opts.has_key?(:on)
|
199
|
-
context = opts.delete(:when) if opts.has_key?(:when)
|
200
|
-
context = opts.delete(:group) if opts.has_key?(:group)
|
201
|
-
opts[:context] = context
|
202
|
-
opts.merge!(defaults) unless defaults.nil?
|
203
|
-
opts
|
204
|
-
end
|
205
|
-
|
206
|
-
# Given a new context create an instance method of
|
207
|
-
# valid_for_<context>? which simply calls valid?(context)
|
208
|
-
# if it does not already exist
|
209
|
-
#
|
210
|
-
def create_context_instance_methods(context)
|
211
|
-
name = "valid_for_#{context.to_s}?" # valid_for_signup?
|
212
|
-
if !self.instance_methods.include?(name)
|
213
|
-
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
214
|
-
def #{name} # def valid_for_signup?
|
215
|
-
valid?('#{context.to_s}'.to_sym) # valid?('signup'.to_sym)
|
216
|
-
end # end
|
217
|
-
EOS
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
# Create a new validator of the given klazz and push it onto the
|
222
|
-
# requested context for each of the attributes in the fields list
|
223
|
-
#
|
224
|
-
def add_validator_to_context(opts, fields, klazz)
|
225
|
-
fields.each do |field|
|
226
|
-
validator = klazz.new(field.to_sym, opts)
|
227
|
-
if opts[:context].is_a?(Symbol)
|
228
|
-
unless validators.context(opts[:context]).include?(validator)
|
229
|
-
validators.context(opts[:context]) << validator
|
230
|
-
create_context_instance_methods(opts[:context])
|
231
|
-
end
|
232
|
-
elsif opts[:context].is_a?(Array)
|
233
|
-
opts[:context].each do |c|
|
234
|
-
unless validators.context(c).include?(validator)
|
235
|
-
validators.context(c) << validator
|
236
|
-
create_context_instance_methods(c)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
end # module ClassMethods
|
244
|
-
end # module Validation
|
245
|
-
|
246
|
-
end # module CouchRest
|