brianmario-couchrest 0.23
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 +75 -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 +198 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/database.rb +303 -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 +108 -0
- data/lib/couchrest/mixins/design_doc.rb +90 -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 +242 -0
- data/lib/couchrest/mixins/views.rb +169 -0
- data/lib/couchrest/monkeypatches.rb +113 -0
- data/lib/couchrest/more/casted_model.rb +28 -0
- data/lib/couchrest/more/extended_document.rb +215 -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/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 +98 -0
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +130 -0
- data/spec/couchrest/more/extended_doc_spec.rb +509 -0
- data/spec/couchrest/more/extended_doc_subclass_spec.rb +98 -0
- data/spec/couchrest/more/extended_doc_view_spec.rb +355 -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/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 +26 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +200 -0
@@ -0,0 +1,242 @@
|
|
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__
|
54
|
+
# Turn off auto validation by default
|
55
|
+
self.auto_validation ||= false
|
56
|
+
|
57
|
+
# Force the auto validation for the class properties
|
58
|
+
# This feature is still not fully ported over,
|
59
|
+
# test are lacking, so please use with caution
|
60
|
+
def self.auto_validate!
|
61
|
+
self.auto_validation = true
|
62
|
+
end
|
63
|
+
|
64
|
+
# share the validations with subclasses
|
65
|
+
def self.inherited(subklass)
|
66
|
+
self.validators.contexts.each do |k, v|
|
67
|
+
subklass.validators.contexts[k] = v.dup
|
68
|
+
end
|
69
|
+
super
|
70
|
+
end
|
71
|
+
EOS
|
72
|
+
|
73
|
+
base.extend(ClassMethods)
|
74
|
+
base.class_eval <<-EOS, __FILE__, __LINE__
|
75
|
+
if method_defined?(:_run_save_callbacks)
|
76
|
+
save_callback :before, :check_validations
|
77
|
+
end
|
78
|
+
EOS
|
79
|
+
base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
80
|
+
def self.define_property(name, options={})
|
81
|
+
super
|
82
|
+
auto_generate_validations(properties.last) if properties && properties.size > 0
|
83
|
+
autovalidation_check = true
|
84
|
+
end
|
85
|
+
RUBY_EVAL
|
86
|
+
end
|
87
|
+
|
88
|
+
# Ensures the object is valid for the context provided, and otherwise
|
89
|
+
# throws :halt and returns false.
|
90
|
+
#
|
91
|
+
def check_validations(context = :default)
|
92
|
+
throw(:halt, false) unless context.nil? || valid?(context)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Return the ValidationErrors
|
96
|
+
#
|
97
|
+
def errors
|
98
|
+
@errors ||= ValidationErrors.new
|
99
|
+
end
|
100
|
+
|
101
|
+
# Mark this resource as validatable. When we validate associations of a
|
102
|
+
# resource we can check if they respond to validatable? before trying to
|
103
|
+
# recursivly validate them
|
104
|
+
#
|
105
|
+
def validatable?
|
106
|
+
true
|
107
|
+
end
|
108
|
+
|
109
|
+
# Alias for valid?(:default)
|
110
|
+
#
|
111
|
+
def valid_for_default?
|
112
|
+
valid?(:default)
|
113
|
+
end
|
114
|
+
|
115
|
+
# Check if a resource is valid in a given context
|
116
|
+
#
|
117
|
+
def valid?(context = :default)
|
118
|
+
self.class.validators.execute(context, self)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Begin a recursive walk of the model checking validity
|
122
|
+
#
|
123
|
+
def all_valid?(context = :default)
|
124
|
+
recursive_valid?(self, context, true)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Do recursive validity checking
|
128
|
+
#
|
129
|
+
def recursive_valid?(target, context, state)
|
130
|
+
valid = state
|
131
|
+
target.instance_variables.each do |ivar|
|
132
|
+
ivar_value = target.instance_variable_get(ivar)
|
133
|
+
if ivar_value.validatable?
|
134
|
+
valid = valid && recursive_valid?(ivar_value, context, valid)
|
135
|
+
elsif ivar_value.respond_to?(:each)
|
136
|
+
ivar_value.each do |item|
|
137
|
+
if item.validatable?
|
138
|
+
valid = valid && recursive_valid?(item, context, valid)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
return valid && target.valid?
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
def validation_property_value(name)
|
148
|
+
self.respond_to?(name, true) ? self.send(name) : nil
|
149
|
+
end
|
150
|
+
|
151
|
+
# Get the corresponding Object property, if it exists.
|
152
|
+
def validation_property(field_name)
|
153
|
+
properties.find{|p| p.name == field_name}
|
154
|
+
end
|
155
|
+
|
156
|
+
module ClassMethods
|
157
|
+
include CouchRest::Validation::ValidatesPresent
|
158
|
+
include CouchRest::Validation::ValidatesAbsent
|
159
|
+
include CouchRest::Validation::ValidatesIsConfirmed
|
160
|
+
# include CouchRest::Validation::ValidatesIsPrimitive
|
161
|
+
# include CouchRest::Validation::ValidatesIsAccepted
|
162
|
+
include CouchRest::Validation::ValidatesFormat
|
163
|
+
include CouchRest::Validation::ValidatesLength
|
164
|
+
# include CouchRest::Validation::ValidatesWithin
|
165
|
+
include CouchRest::Validation::ValidatesIsNumber
|
166
|
+
include CouchRest::Validation::ValidatesWithMethod
|
167
|
+
# include CouchRest::Validation::ValidatesWithBlock
|
168
|
+
# include CouchRest::Validation::ValidatesIsUnique
|
169
|
+
include CouchRest::Validation::AutoValidate
|
170
|
+
|
171
|
+
# Return the set of contextual validators or create a new one
|
172
|
+
#
|
173
|
+
def validators
|
174
|
+
@validations ||= ContextualValidators.new
|
175
|
+
end
|
176
|
+
|
177
|
+
# Clean up the argument list and return a opts hash, including the
|
178
|
+
# merging of any default opts. Set the context to default if none is
|
179
|
+
# provided. Also allow :context to be aliased to :on, :when & group
|
180
|
+
#
|
181
|
+
def opts_from_validator_args(args, defaults = nil)
|
182
|
+
opts = args.last.kind_of?(Hash) ? args.pop : {}
|
183
|
+
context = :default
|
184
|
+
context = opts[:context] if opts.has_key?(:context)
|
185
|
+
context = opts.delete(:on) if opts.has_key?(:on)
|
186
|
+
context = opts.delete(:when) if opts.has_key?(:when)
|
187
|
+
context = opts.delete(:group) if opts.has_key?(:group)
|
188
|
+
opts[:context] = context
|
189
|
+
opts.merge!(defaults) unless defaults.nil?
|
190
|
+
opts
|
191
|
+
end
|
192
|
+
|
193
|
+
# Given a new context create an instance method of
|
194
|
+
# valid_for_<context>? which simply calls valid?(context)
|
195
|
+
# if it does not already exist
|
196
|
+
#
|
197
|
+
def create_context_instance_methods(context)
|
198
|
+
name = "valid_for_#{context.to_s}?" # valid_for_signup?
|
199
|
+
if !self.instance_methods.include?(name)
|
200
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
201
|
+
def #{name} # def valid_for_signup?
|
202
|
+
valid?('#{context.to_s}'.to_sym) # valid?('signup'.to_sym)
|
203
|
+
end # end
|
204
|
+
EOS
|
205
|
+
end
|
206
|
+
|
207
|
+
all = "all_valid_for_#{context.to_s}?" # all_valid_for_signup?
|
208
|
+
if !self.instance_methods.include?(all)
|
209
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
210
|
+
def #{all} # def all_valid_for_signup?
|
211
|
+
all_valid?('#{context.to_s}'.to_sym) # all_valid?('signup'.to_sym)
|
212
|
+
end # end
|
213
|
+
EOS
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
# Create a new validator of the given klazz and push it onto the
|
218
|
+
# requested context for each of the attributes in the fields list
|
219
|
+
#
|
220
|
+
def add_validator_to_context(opts, fields, klazz)
|
221
|
+
fields.each do |field|
|
222
|
+
validator = klazz.new(field.to_sym, opts)
|
223
|
+
if opts[:context].is_a?(Symbol)
|
224
|
+
unless validators.context(opts[:context]).include?(validator)
|
225
|
+
validators.context(opts[:context]) << validator
|
226
|
+
create_context_instance_methods(opts[:context])
|
227
|
+
end
|
228
|
+
elsif opts[:context].is_a?(Array)
|
229
|
+
opts[:context].each do |c|
|
230
|
+
unless validators.context(c).include?(validator)
|
231
|
+
validators.context(c) << validator
|
232
|
+
create_context_instance_methods(c)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
end # module ClassMethods
|
240
|
+
end # module Validation
|
241
|
+
|
242
|
+
end # module CouchRest
|
@@ -0,0 +1,169 @@
|
|
1
|
+
module CouchRest
|
2
|
+
module Mixins
|
3
|
+
module Views
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# Define a CouchDB view. The name of the view will be the concatenation
|
11
|
+
# of <tt>by</tt> and the keys joined by <tt>_and_</tt>
|
12
|
+
#
|
13
|
+
# ==== Example views:
|
14
|
+
#
|
15
|
+
# class Post
|
16
|
+
# # view with default options
|
17
|
+
# # query with Post.by_date
|
18
|
+
# view_by :date, :descending => true
|
19
|
+
#
|
20
|
+
# # view with compound sort-keys
|
21
|
+
# # query with Post.by_user_id_and_date
|
22
|
+
# view_by :user_id, :date
|
23
|
+
#
|
24
|
+
# # view with custom map/reduce functions
|
25
|
+
# # query with Post.by_tags :reduce => true
|
26
|
+
# view_by :tags,
|
27
|
+
# :map =>
|
28
|
+
# "function(doc) {
|
29
|
+
# if (doc['couchrest-type'] == 'Post' && doc.tags) {
|
30
|
+
# doc.tags.forEach(function(tag){
|
31
|
+
# emit(doc.tag, 1);
|
32
|
+
# });
|
33
|
+
# }
|
34
|
+
# }",
|
35
|
+
# :reduce =>
|
36
|
+
# "function(keys, values, rereduce) {
|
37
|
+
# return sum(values);
|
38
|
+
# }"
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# <tt>view_by :date</tt> will create a view defined by this Javascript
|
42
|
+
# function:
|
43
|
+
#
|
44
|
+
# function(doc) {
|
45
|
+
# if (doc['couchrest-type'] == 'Post' && doc.date) {
|
46
|
+
# emit(doc.date, null);
|
47
|
+
# }
|
48
|
+
# }
|
49
|
+
#
|
50
|
+
# It can be queried by calling <tt>Post.by_date</tt> which accepts all
|
51
|
+
# valid options for CouchRest::Database#view. In addition, calling with
|
52
|
+
# the <tt>:raw => true</tt> option will return the view rows
|
53
|
+
# themselves. By default <tt>Post.by_date</tt> will return the
|
54
|
+
# documents included in the generated view.
|
55
|
+
#
|
56
|
+
# Calling with :database => [instance of CouchRest::Database] will
|
57
|
+
# send the query to a specific database, otherwise it will go to
|
58
|
+
# the model's default database (use_database)
|
59
|
+
#
|
60
|
+
# CouchRest::Database#view options can be applied at view definition
|
61
|
+
# time as defaults, and they will be curried and used at view query
|
62
|
+
# time. Or they can be overridden at query time.
|
63
|
+
#
|
64
|
+
# Custom views can be queried with <tt>:reduce => true</tt> to return
|
65
|
+
# reduce results. The default for custom views is to query with
|
66
|
+
# <tt>:reduce => false</tt>.
|
67
|
+
#
|
68
|
+
# Views are generated (on a per-model basis) lazily on first-access.
|
69
|
+
# This means that if you are deploying changes to a view, the views for
|
70
|
+
# that model won't be available until generation is complete. This can
|
71
|
+
# take some time with large databases. Strategies are in the works.
|
72
|
+
#
|
73
|
+
# To understand the capabilities of this view system more completely,
|
74
|
+
# it is recommended that you read the RSpec file at
|
75
|
+
# <tt>spec/core/model_spec.rb</tt>.
|
76
|
+
|
77
|
+
def view_by(*keys)
|
78
|
+
opts = keys.pop if keys.last.is_a?(Hash)
|
79
|
+
opts ||= {}
|
80
|
+
ducktype = opts.delete(:ducktype)
|
81
|
+
unless ducktype || opts[:map]
|
82
|
+
opts[:guards] ||= []
|
83
|
+
opts[:guards].push "(doc['couchrest-type'] == '#{self.to_s}')"
|
84
|
+
end
|
85
|
+
keys.push opts
|
86
|
+
self.design_doc.view_by(*keys)
|
87
|
+
self.design_doc_fresh = false
|
88
|
+
end
|
89
|
+
|
90
|
+
# returns stored defaults if the there is a view named this in the design doc
|
91
|
+
def has_view?(view)
|
92
|
+
view = view.to_s
|
93
|
+
design_doc && design_doc['views'] && design_doc['views'][view]
|
94
|
+
end
|
95
|
+
|
96
|
+
# Dispatches to any named view.
|
97
|
+
def view(name, query={}, &block)
|
98
|
+
unless design_doc_fresh
|
99
|
+
refresh_design_doc
|
100
|
+
end
|
101
|
+
query[:raw] = true if query[:reduce]
|
102
|
+
db = query.delete(:database) || database
|
103
|
+
raw = query.delete(:raw)
|
104
|
+
fetch_view_with_docs(db, name, query, raw, &block)
|
105
|
+
end
|
106
|
+
|
107
|
+
def all_design_doc_versions(db = database)
|
108
|
+
db.documents :startkey => "_design/#{self.to_s}-",
|
109
|
+
:endkey => "_design/#{self.to_s}-\u9999"
|
110
|
+
end
|
111
|
+
|
112
|
+
# Deletes any non-current design docs that were created by this class.
|
113
|
+
# Running this when you're deployed version of your application is steadily
|
114
|
+
# and consistently using the latest code, is the way to clear out old design
|
115
|
+
# docs. Running it to early could mean that live code has to regenerate
|
116
|
+
# potentially large indexes.
|
117
|
+
def cleanup_design_docs!(db = database)
|
118
|
+
ddocs = all_design_doc_versions(db)
|
119
|
+
ddocs["rows"].each do |row|
|
120
|
+
if (row['id'] != design_doc_id)
|
121
|
+
db.delete_doc({
|
122
|
+
"_id" => row['id'],
|
123
|
+
"_rev" => row['value']['rev']
|
124
|
+
})
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def fetch_view_with_docs(db, name, opts, raw=false, &block)
|
132
|
+
if raw || (opts.has_key?(:include_docs) && opts[:include_docs] == false)
|
133
|
+
fetch_view(db, name, opts, &block)
|
134
|
+
else
|
135
|
+
begin
|
136
|
+
view = fetch_view db, name, opts.merge({:include_docs => true}), &block
|
137
|
+
view['rows'].collect{|r|new(r['doc'])} if view['rows']
|
138
|
+
rescue
|
139
|
+
# fallback for old versions of couchdb that don't
|
140
|
+
# have include_docs support
|
141
|
+
view = fetch_view(db, name, opts, &block)
|
142
|
+
view['rows'].collect{|r|new(db.get(r['id']))} if view['rows']
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def fetch_view(db, view_name, opts, &block)
|
148
|
+
raise "A view needs a database to operate on (specify :database option, or use_database in the #{self.class} class)" unless db
|
149
|
+
retryable = true
|
150
|
+
begin
|
151
|
+
design_doc.view_on(db, view_name, opts, &block)
|
152
|
+
# the design doc may not have been saved yet on this database
|
153
|
+
rescue RestClient::ResourceNotFound => e
|
154
|
+
if retryable
|
155
|
+
save_design_doc_on(db)
|
156
|
+
retryable = false
|
157
|
+
retry
|
158
|
+
else
|
159
|
+
raise e
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
end # module ClassMethods
|
165
|
+
|
166
|
+
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'support', 'class')
|
2
|
+
require File.join(File.dirname(__FILE__), 'support', 'blank')
|
3
|
+
|
4
|
+
# This file must be loaded after the JSON gem and any other library that beats up the Time class.
|
5
|
+
class Time
|
6
|
+
# This date format sorts lexicographically
|
7
|
+
# and is compatible with Javascript's <tt>new Date(time_string)</tt> constructor.
|
8
|
+
# Note this this format stores all dates in UTC so that collation
|
9
|
+
# order is preserved. (There's no longer a need to set <tt>ENV['TZ'] = 'UTC'</tt>
|
10
|
+
# in your application.)
|
11
|
+
|
12
|
+
def to_json(options = nil)
|
13
|
+
u = self.getutc
|
14
|
+
%("#{u.strftime("%Y/%m/%d %H:%M:%S +0000")}")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Decodes the JSON time format to a UTC time.
|
18
|
+
# Based on Time.parse from ActiveSupport. ActiveSupport's version
|
19
|
+
# is more complete, returning a time in your current timezone,
|
20
|
+
# rather than keeping the time in UTC. YMMV.
|
21
|
+
# def self.parse string, fallback=nil
|
22
|
+
# d = DateTime.parse(string).new_offset
|
23
|
+
# self.utc(d.year, d.month, d.day, d.hour, d.min, d.sec)
|
24
|
+
# rescue
|
25
|
+
# fallback
|
26
|
+
# end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Monkey patch for faster net/http io
|
30
|
+
if RUBY_VERSION.to_f < 1.9
|
31
|
+
class Net::BufferedIO #:nodoc:
|
32
|
+
alias :old_rbuf_fill :rbuf_fill
|
33
|
+
def rbuf_fill
|
34
|
+
if @io.respond_to?(:read_nonblock)
|
35
|
+
begin
|
36
|
+
@rbuf << @io.read_nonblock(65536)
|
37
|
+
rescue Errno::EWOULDBLOCK
|
38
|
+
if IO.select([@io], nil, nil, @read_timeout)
|
39
|
+
retry
|
40
|
+
else
|
41
|
+
raise Timeout::TimeoutError
|
42
|
+
end
|
43
|
+
end
|
44
|
+
else
|
45
|
+
timeout(@read_timeout) do
|
46
|
+
@rbuf << @io.sysread(65536)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module RestClient
|
54
|
+
def self.copy(url, headers={})
|
55
|
+
Request.execute(:method => :copy,
|
56
|
+
:url => url,
|
57
|
+
:headers => headers)
|
58
|
+
end
|
59
|
+
|
60
|
+
# class Request
|
61
|
+
#
|
62
|
+
# def establish_connection(uri)
|
63
|
+
# Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
|
64
|
+
# p net_http_class
|
65
|
+
# net = net_http_class.new(uri.host, uri.port)
|
66
|
+
# net.use_ssl = uri.is_a?(URI::HTTPS)
|
67
|
+
# net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
68
|
+
# Thread.current[:connection] = net
|
69
|
+
# Thread.current[:connection].start
|
70
|
+
# Thread.current[:connection]
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# def transmit(uri, req, payload)
|
74
|
+
# setup_credentials(req)
|
75
|
+
#
|
76
|
+
# Thread.current[:host] ||= uri.host
|
77
|
+
# Thread.current[:port] ||= uri.port
|
78
|
+
#
|
79
|
+
# if (Thread.current[:connection].nil? || (Thread.current[:host] != uri.host))
|
80
|
+
# p "establishing a connection"
|
81
|
+
# establish_connection(uri)
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# display_log request_log
|
85
|
+
# http = Thread.current[:connection]
|
86
|
+
# http.read_timeout = @timeout if @timeout
|
87
|
+
#
|
88
|
+
# begin
|
89
|
+
# res = http.request(req, payload)
|
90
|
+
# rescue
|
91
|
+
# p "Net::HTTP connection failed, reconnecting"
|
92
|
+
# establish_connection(uri)
|
93
|
+
# http = Thread.current[:connection]
|
94
|
+
# require 'ruby-debug'
|
95
|
+
# debugger
|
96
|
+
# req.body_stream = nil
|
97
|
+
#
|
98
|
+
# res = http.request(req, payload)
|
99
|
+
# display_log response_log(res)
|
100
|
+
# result res
|
101
|
+
# else
|
102
|
+
# display_log response_log(res)
|
103
|
+
# process_result res
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# rescue EOFError
|
107
|
+
# raise RestClient::ServerBrokeConnection
|
108
|
+
# rescue Timeout::Error
|
109
|
+
# raise RestClient::RequestTimeout
|
110
|
+
# end
|
111
|
+
# end
|
112
|
+
|
113
|
+
end
|