couchrest_model_thought 1.0.0.beta8
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/Gemfile +9 -0
- data/LICENSE +176 -0
- data/README.md +320 -0
- data/Rakefile +69 -0
- data/THANKS.md +19 -0
- data/examples/model/example.rb +144 -0
- data/lib/couchrest/model/associations.rb +207 -0
- data/lib/couchrest/model/attribute_protection.rb +74 -0
- data/lib/couchrest/model/attributes.rb +91 -0
- data/lib/couchrest/model/base.rb +111 -0
- data/lib/couchrest/model/callbacks.rb +27 -0
- data/lib/couchrest/model/casted_array.rb +39 -0
- data/lib/couchrest/model/casted_model.rb +68 -0
- data/lib/couchrest/model/class_proxy.rb +122 -0
- data/lib/couchrest/model/collection.rb +260 -0
- data/lib/couchrest/model/design_doc.rb +126 -0
- data/lib/couchrest/model/document_queries.rb +82 -0
- data/lib/couchrest/model/errors.rb +23 -0
- data/lib/couchrest/model/extended_attachments.rb +73 -0
- data/lib/couchrest/model/persistence.rb +141 -0
- data/lib/couchrest/model/properties.rb +144 -0
- data/lib/couchrest/model/property.rb +96 -0
- data/lib/couchrest/model/support/couchrest.rb +19 -0
- data/lib/couchrest/model/support/hash.rb +9 -0
- data/lib/couchrest/model/typecast.rb +170 -0
- data/lib/couchrest/model/validations/casted_model.rb +14 -0
- data/lib/couchrest/model/validations/locale/en.yml +5 -0
- data/lib/couchrest/model/validations/uniqueness.rb +42 -0
- data/lib/couchrest/model/validations.rb +68 -0
- data/lib/couchrest/model/version.rb +5 -0
- data/lib/couchrest/model/views.rb +160 -0
- data/lib/couchrest/model.rb +5 -0
- data/lib/couchrest_model.rb +53 -0
- data/spec/couchrest/assocations_spec.rb +213 -0
- data/spec/couchrest/attachment_spec.rb +148 -0
- data/spec/couchrest/attribute_protection_spec.rb +153 -0
- data/spec/couchrest/base_spec.rb +463 -0
- data/spec/couchrest/casted_model_spec.rb +424 -0
- data/spec/couchrest/casted_spec.rb +75 -0
- data/spec/couchrest/class_proxy_spec.rb +132 -0
- data/spec/couchrest/inherited_spec.rb +40 -0
- data/spec/couchrest/persistence_spec.rb +409 -0
- data/spec/couchrest/property_spec.rb +804 -0
- data/spec/couchrest/subclass_spec.rb +99 -0
- data/spec/couchrest/validations.rb +85 -0
- data/spec/couchrest/view_spec.rb +463 -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/base.rb +139 -0
- data/spec/fixtures/more/article.rb +35 -0
- data/spec/fixtures/more/card.rb +17 -0
- data/spec/fixtures/more/cat.rb +19 -0
- data/spec/fixtures/more/course.rb +25 -0
- data/spec/fixtures/more/event.rb +8 -0
- data/spec/fixtures/more/invoice.rb +14 -0
- data/spec/fixtures/more/person.rb +9 -0
- data/spec/fixtures/more/question.rb +7 -0
- data/spec/fixtures/more/service.rb +10 -0
- data/spec/fixtures/more/user.rb +22 -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 +5 -0
- data/spec/spec_helper.rb +48 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +215 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
module CouchRest::Model
|
2
|
+
module CastedModel
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include CouchRest::Model::AttributeProtection
|
8
|
+
include CouchRest::Model::Attributes
|
9
|
+
include CouchRest::Model::Callbacks
|
10
|
+
include CouchRest::Model::Properties
|
11
|
+
include CouchRest::Model::Associations
|
12
|
+
include CouchRest::Model::Validations
|
13
|
+
attr_accessor :casted_by
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(keys = {})
|
17
|
+
raise StandardError unless self.is_a? Hash
|
18
|
+
prepare_all_attributes(keys)
|
19
|
+
super()
|
20
|
+
end
|
21
|
+
|
22
|
+
def []= key, value
|
23
|
+
super(key.to_s, value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def [] key
|
27
|
+
super(key.to_s)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Gets a reference to the top level extended
|
31
|
+
# document that a model is saved inside of
|
32
|
+
def base_doc
|
33
|
+
return nil unless @casted_by
|
34
|
+
@casted_by.base_doc
|
35
|
+
end
|
36
|
+
|
37
|
+
# False if the casted model has already
|
38
|
+
# been saved in the containing document
|
39
|
+
def new?
|
40
|
+
@casted_by.nil? ? true : @casted_by.new?
|
41
|
+
end
|
42
|
+
alias :new_record? :new?
|
43
|
+
|
44
|
+
def persisted?
|
45
|
+
!new?
|
46
|
+
end
|
47
|
+
|
48
|
+
# The to_param method is needed for rails to generate resourceful routes.
|
49
|
+
# In your controller, remember that it's actually the id of the document.
|
50
|
+
def id
|
51
|
+
return nil if base_doc.nil?
|
52
|
+
base_doc.id
|
53
|
+
end
|
54
|
+
alias :to_key :id
|
55
|
+
alias :to_param :id
|
56
|
+
|
57
|
+
# Sets the attributes from a hash
|
58
|
+
def update_attributes_without_saving(hash)
|
59
|
+
hash.each do |k, v|
|
60
|
+
raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
|
61
|
+
end
|
62
|
+
hash.each do |k, v|
|
63
|
+
self.send("#{k}=",v)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
alias :attributes= :update_attributes_without_saving
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module CouchRest
|
2
|
+
module Model
|
3
|
+
module ClassProxy
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
# Return a proxy object which represents a model class on a
|
12
|
+
# chosen database instance. This allows you to DRY operations
|
13
|
+
# where a database is chosen dynamically.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
#
|
17
|
+
# db = CouchRest::Database.new(...)
|
18
|
+
# articles = Article.on(db)
|
19
|
+
#
|
20
|
+
# articles.all { ... }
|
21
|
+
# articles.by_title { ... }
|
22
|
+
#
|
23
|
+
# u = articles.get("someid")
|
24
|
+
#
|
25
|
+
# u = articles.new(:title => "I like plankton")
|
26
|
+
# u.save # saved on the correct database
|
27
|
+
|
28
|
+
def on(database)
|
29
|
+
Proxy.new(self, database)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Proxy #:nodoc:
|
34
|
+
def initialize(klass, database)
|
35
|
+
@klass = klass
|
36
|
+
@database = database
|
37
|
+
end
|
38
|
+
|
39
|
+
# Base
|
40
|
+
|
41
|
+
def new(*args)
|
42
|
+
doc = @klass.new(*args)
|
43
|
+
doc.database = @database
|
44
|
+
doc
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(m, *args, &block)
|
48
|
+
if has_view?(m)
|
49
|
+
query = args.shift || {}
|
50
|
+
return view(m, query, *args, &block)
|
51
|
+
elsif m.to_s =~ /^find_(by_.+)/
|
52
|
+
view_name = $1
|
53
|
+
if has_view?(view_name)
|
54
|
+
return first_from_view(view_name, *args)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
super
|
58
|
+
end
|
59
|
+
|
60
|
+
# DocumentQueries
|
61
|
+
|
62
|
+
def all(opts = {}, &block)
|
63
|
+
docs = @klass.all({:database => @database}.merge(opts), &block)
|
64
|
+
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
|
65
|
+
docs
|
66
|
+
end
|
67
|
+
|
68
|
+
def count(opts = {}, &block)
|
69
|
+
@klass.all({:database => @database, :raw => true, :limit => 0}.merge(opts), &block)['total_rows']
|
70
|
+
end
|
71
|
+
|
72
|
+
def first(opts = {})
|
73
|
+
doc = @klass.first({:database => @database}.merge(opts))
|
74
|
+
doc.database = @database if doc && doc.respond_to?(:database)
|
75
|
+
doc
|
76
|
+
end
|
77
|
+
|
78
|
+
def get(id)
|
79
|
+
doc = @klass.get(id, @database)
|
80
|
+
doc.database = @database if doc && doc.respond_to?(:database)
|
81
|
+
doc
|
82
|
+
end
|
83
|
+
alias :find :get
|
84
|
+
|
85
|
+
# Views
|
86
|
+
|
87
|
+
def has_view?(view)
|
88
|
+
@klass.has_view?(view)
|
89
|
+
end
|
90
|
+
|
91
|
+
def view(name, query={}, &block)
|
92
|
+
docs = @klass.view(name, {:database => @database}.merge(query), &block)
|
93
|
+
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
|
94
|
+
docs
|
95
|
+
end
|
96
|
+
|
97
|
+
def first_from_view(name, *args)
|
98
|
+
# add to first hash available, or add to end
|
99
|
+
(args.last.is_a?(Hash) ? args.last : (args << {}).last)[:database] = @database
|
100
|
+
doc = @klass.first_from_view(name, *args)
|
101
|
+
doc.database = @database if doc && doc.respond_to?(:database)
|
102
|
+
doc
|
103
|
+
end
|
104
|
+
|
105
|
+
# DesignDoc
|
106
|
+
|
107
|
+
def design_doc
|
108
|
+
@klass.design_doc
|
109
|
+
end
|
110
|
+
|
111
|
+
def refresh_design_doc
|
112
|
+
@klass.refresh_design_doc(@database)
|
113
|
+
end
|
114
|
+
|
115
|
+
def save_design_doc
|
116
|
+
@klass.save_design_doc(@database)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,260 @@
|
|
1
|
+
module CouchRest
|
2
|
+
module Model
|
3
|
+
module Collection
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
# Creates a new class method, find_all_<collection_name>, that will
|
12
|
+
# execute the view specified with the design_doc and view_name
|
13
|
+
# parameters, along with the specified view_options. This method will
|
14
|
+
# return the results of the view as an Array of objects which are
|
15
|
+
# instances of the class.
|
16
|
+
#
|
17
|
+
# This method is handy for objects that do not use the view_by method
|
18
|
+
# to declare their views.
|
19
|
+
def provides_collection(collection_name, design_doc, view_name, view_options)
|
20
|
+
class_eval <<-END, __FILE__, __LINE__ + 1
|
21
|
+
def self.find_all_#{collection_name}(options = {})
|
22
|
+
view_options = #{view_options.inspect} || {}
|
23
|
+
CollectionProxy.new(options[:database] || database, "#{design_doc}", "#{view_name}", view_options.merge(options), Kernel.const_get('#{self}'))
|
24
|
+
end
|
25
|
+
END
|
26
|
+
end
|
27
|
+
|
28
|
+
# Fetch a group of objects from CouchDB. Options can include:
|
29
|
+
# :page - Specifies the page to load (starting at 1)
|
30
|
+
# :per_page - Specifies the number of objects to load per page
|
31
|
+
#
|
32
|
+
# Defaults are used if these options are not specified.
|
33
|
+
def paginate(options)
|
34
|
+
proxy = create_collection_proxy(options)
|
35
|
+
proxy.paginate(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Iterate over the objects in a collection, fetching them from CouchDB
|
39
|
+
# in groups. Options can include:
|
40
|
+
# :page - Specifies the page to load
|
41
|
+
# :per_page - Specifies the number of objects to load per page
|
42
|
+
#
|
43
|
+
# Defaults are used if these options are not specified.
|
44
|
+
def paginated_each(options, &block)
|
45
|
+
search = options.delete(:search)
|
46
|
+
unless search == true
|
47
|
+
proxy = create_collection_proxy(options)
|
48
|
+
else
|
49
|
+
proxy = create_search_collection_proxy(options)
|
50
|
+
end
|
51
|
+
proxy.paginated_each(options, &block)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Create a CollectionProxy for the specified view and options.
|
55
|
+
# CollectionProxy behaves just like an Array, but offers support for
|
56
|
+
# pagination.
|
57
|
+
def collection_proxy_for(design_doc, view_name, view_options = {})
|
58
|
+
options = view_options.merge(:design_doc => design_doc, :view_name => view_name)
|
59
|
+
create_collection_proxy(options)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def create_collection_proxy(options)
|
65
|
+
design_doc, view_name, view_options = parse_view_options(options)
|
66
|
+
CollectionProxy.new(options[:database] || database, design_doc, view_name, view_options, self)
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_search_collection_proxy(options)
|
70
|
+
design_doc, search_name, search_options = parse_search_options(options)
|
71
|
+
CollectionProxy.new(options[:database] || database, design_doc, search_name, search_options, self, :search)
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse_view_options(options)
|
75
|
+
design_doc = options.delete(:design_doc)
|
76
|
+
raise ArgumentError, 'design_doc is required' if design_doc.nil?
|
77
|
+
|
78
|
+
view_name = options.delete(:view_name)
|
79
|
+
raise ArgumentError, 'view_name is required' if view_name.nil?
|
80
|
+
|
81
|
+
default_view_options = (design_doc.class == Design &&
|
82
|
+
design_doc['views'][view_name.to_s] &&
|
83
|
+
design_doc['views'][view_name.to_s]["couchrest-defaults"]) || {}
|
84
|
+
view_options = default_view_options.merge(options)
|
85
|
+
|
86
|
+
[design_doc, view_name, view_options]
|
87
|
+
end
|
88
|
+
|
89
|
+
def parse_search_options(options)
|
90
|
+
design_doc = options.delete(:design_doc)
|
91
|
+
raise ArgumentError, 'design_doc is required' if design_doc.nil?
|
92
|
+
|
93
|
+
search_name = options.delete(:view_name)
|
94
|
+
raise ArgumentError, 'search_name is required' if search_name.nil?
|
95
|
+
|
96
|
+
search_options = options.clone
|
97
|
+
[design_doc, search_name, search_options]
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
class CollectionProxy
|
103
|
+
alias_method :proxy_respond_to?, :respond_to?
|
104
|
+
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
|
105
|
+
|
106
|
+
DEFAULT_PAGE = 1
|
107
|
+
DEFAULT_PER_PAGE = 30
|
108
|
+
|
109
|
+
# Create a new CollectionProxy to represent the specified view. If a
|
110
|
+
# container class is specified, the proxy will create an object of the
|
111
|
+
# given type for each row that comes back from the view. If no
|
112
|
+
# container class is specified, the raw results are returned.
|
113
|
+
#
|
114
|
+
# The CollectionProxy provides support for paginating over a collection
|
115
|
+
# via the paginate, and paginated_each methods.
|
116
|
+
def initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view)
|
117
|
+
raise ArgumentError, "database is a required parameter" if database.nil?
|
118
|
+
|
119
|
+
@database = database
|
120
|
+
@container_class = container_class
|
121
|
+
@query_type = query_type
|
122
|
+
|
123
|
+
strip_pagination_options(view_options)
|
124
|
+
@view_options = view_options
|
125
|
+
|
126
|
+
if design_doc.class == Design
|
127
|
+
@view_name = "#{design_doc.name}/#{view_name}"
|
128
|
+
else
|
129
|
+
@view_name = "#{design_doc}/#{view_name}"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# See Collection.paginate
|
134
|
+
def paginate(options = {})
|
135
|
+
page, per_page = parse_options(options)
|
136
|
+
results = @database.send(@query_type, @view_name, pagination_options(page, per_page))
|
137
|
+
remember_where_we_left_off(results, page)
|
138
|
+
instances = convert_to_container_array(results)
|
139
|
+
|
140
|
+
begin
|
141
|
+
if Kernel.const_get('WillPaginate')
|
142
|
+
total_rows = results['total_rows'].to_i
|
143
|
+
paginated = WillPaginate::Collection.create(page, per_page, total_rows) do |pager|
|
144
|
+
pager.replace(instances)
|
145
|
+
end
|
146
|
+
return paginated
|
147
|
+
end
|
148
|
+
rescue NameError
|
149
|
+
# When not using will_paginate, not much we could do about this. :x
|
150
|
+
end
|
151
|
+
return instances
|
152
|
+
end
|
153
|
+
|
154
|
+
# See Collection.paginated_each
|
155
|
+
def paginated_each(options = {}, &block)
|
156
|
+
page, per_page = parse_options(options)
|
157
|
+
|
158
|
+
begin
|
159
|
+
collection = paginate({:page => page, :per_page => per_page})
|
160
|
+
collection.each(&block)
|
161
|
+
page += 1
|
162
|
+
end until collection.size < per_page
|
163
|
+
end
|
164
|
+
|
165
|
+
def respond_to?(*args)
|
166
|
+
proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args))
|
167
|
+
end
|
168
|
+
|
169
|
+
# Explicitly proxy === because the instance method removal above
|
170
|
+
# doesn't catch it.
|
171
|
+
def ===(other)
|
172
|
+
load_target
|
173
|
+
other === @target
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
def method_missing(method, *args)
|
179
|
+
if load_target
|
180
|
+
if block_given?
|
181
|
+
@target.send(method, *args) { |*block_args| yield(*block_args) }
|
182
|
+
else
|
183
|
+
@target.send(method, *args)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def load_target
|
189
|
+
unless loaded?
|
190
|
+
@view_options.merge!({:include_docs => true}) if @query_type == :search
|
191
|
+
results = @database.send(@query_type, @view_name, @view_options)
|
192
|
+
@target = convert_to_container_array(results)
|
193
|
+
end
|
194
|
+
@loaded = true
|
195
|
+
@target
|
196
|
+
end
|
197
|
+
|
198
|
+
def loaded?
|
199
|
+
@loaded
|
200
|
+
end
|
201
|
+
|
202
|
+
def reload
|
203
|
+
reset
|
204
|
+
load_target
|
205
|
+
self unless @target.nil?
|
206
|
+
end
|
207
|
+
|
208
|
+
def reset
|
209
|
+
@loaded = false
|
210
|
+
@target = nil
|
211
|
+
end
|
212
|
+
|
213
|
+
def inspect
|
214
|
+
load_target
|
215
|
+
@target.inspect
|
216
|
+
end
|
217
|
+
|
218
|
+
def convert_to_container_array(results)
|
219
|
+
if @container_class.nil?
|
220
|
+
results
|
221
|
+
else
|
222
|
+
results['rows'].collect { |row| @container_class.create_from_database(row['doc']) } unless results['rows'].nil?
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def pagination_options(page, per_page)
|
227
|
+
view_options = @view_options.clone
|
228
|
+
if @query_type == :view && @last_key && @last_docid && @last_page == page - 1
|
229
|
+
key = view_options.delete(:key)
|
230
|
+
end_key = view_options[:endkey] || key
|
231
|
+
options = { :startkey => @last_key, :endkey => end_key, :startkey_docid => @last_docid, :limit => per_page, :skip => 1 }
|
232
|
+
else
|
233
|
+
options = { :limit => per_page, :skip => per_page * (page - 1) }
|
234
|
+
end
|
235
|
+
view_options.merge(options)
|
236
|
+
end
|
237
|
+
|
238
|
+
def parse_options(options)
|
239
|
+
page = options.delete(:page) || DEFAULT_PAGE
|
240
|
+
per_page = options.delete(:per_page) || DEFAULT_PER_PAGE
|
241
|
+
[page.to_i, per_page.to_i]
|
242
|
+
end
|
243
|
+
|
244
|
+
def strip_pagination_options(options)
|
245
|
+
parse_options(options)
|
246
|
+
end
|
247
|
+
|
248
|
+
def remember_where_we_left_off(results, page)
|
249
|
+
last_row = results['rows'].last
|
250
|
+
if last_row
|
251
|
+
@last_key = last_row['key']
|
252
|
+
@last_docid = last_row['id']
|
253
|
+
end
|
254
|
+
@last_page = page
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module CouchRest
|
3
|
+
module Model
|
4
|
+
module DesignDoc
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def design_doc
|
13
|
+
@design_doc ||= Design.new(default_design_doc)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Use when something has been changed, like a view, so that on the next request
|
17
|
+
# the design docs will be updated (if changed!)
|
18
|
+
def req_design_doc_refresh
|
19
|
+
@design_doc_fresh = { }
|
20
|
+
end
|
21
|
+
|
22
|
+
def design_doc_id
|
23
|
+
"_design/#{design_doc_slug}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def design_doc_slug
|
27
|
+
self.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_design_doc
|
31
|
+
{
|
32
|
+
"_id" => design_doc_id,
|
33
|
+
"language" => "javascript",
|
34
|
+
"views" => {
|
35
|
+
'all' => {
|
36
|
+
'map' => "function(doc) {
|
37
|
+
if (doc['couchrest-type'] == '#{self.to_s}') {
|
38
|
+
emit(doc['_id'],1);
|
39
|
+
}
|
40
|
+
}"
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
# DEPRECATED
|
47
|
+
# use stored_design_doc to retrieve the current design doc
|
48
|
+
def all_design_doc_versions(db = database)
|
49
|
+
db.documents :startkey => "_design/#{self.to_s}",
|
50
|
+
:endkey => "_design/#{self.to_s}-\u9999"
|
51
|
+
end
|
52
|
+
|
53
|
+
# Retreive the latest version of the design document directly
|
54
|
+
# from the database.
|
55
|
+
def stored_design_doc(db = database)
|
56
|
+
db.get(design_doc_id) rescue nil
|
57
|
+
end
|
58
|
+
alias :model_design_doc :stored_design_doc
|
59
|
+
|
60
|
+
def refresh_design_doc(db = database)
|
61
|
+
raise "Database missing for design document refresh" if db.nil?
|
62
|
+
unless design_doc_fresh(db)
|
63
|
+
save_design_doc(db)
|
64
|
+
design_doc_fresh(db, true)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Save the design doc onto a target database in a thread-safe way,
|
69
|
+
# not modifying the model's design_doc
|
70
|
+
#
|
71
|
+
# See also save_design_doc! to always save the design doc even if there
|
72
|
+
# are no changes.
|
73
|
+
def save_design_doc(db = database, force = false)
|
74
|
+
update_design_doc(Design.new(design_doc), db, force)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Force the update of the model's design_doc even if it hasn't changed.
|
78
|
+
def save_design_doc!(db = database)
|
79
|
+
save_design_doc(db, true)
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
def design_doc_fresh(db, fresh = nil)
|
85
|
+
@design_doc_fresh ||= {}
|
86
|
+
if fresh.nil?
|
87
|
+
@design_doc_fresh[db.uri] || false
|
88
|
+
else
|
89
|
+
@design_doc_fresh[db.uri] = fresh
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Writes out a design_doc to a given database, returning the
|
94
|
+
# updated design doc
|
95
|
+
def update_design_doc(design_doc, db, force = false)
|
96
|
+
saved = stored_design_doc(db)
|
97
|
+
if saved
|
98
|
+
changes = force
|
99
|
+
design_doc['views'].each do |name, view|
|
100
|
+
if !compare_views(saved['views'][name], view)
|
101
|
+
changes = true
|
102
|
+
saved['views'][name] = view
|
103
|
+
end
|
104
|
+
end
|
105
|
+
if changes
|
106
|
+
db.save_doc(saved)
|
107
|
+
end
|
108
|
+
design_doc
|
109
|
+
else
|
110
|
+
design_doc.database = db
|
111
|
+
design_doc.save
|
112
|
+
design_doc
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Return true if the two views match
|
117
|
+
def compare_views(orig, repl)
|
118
|
+
return false if orig.nil? or repl.nil?
|
119
|
+
(orig['map'].to_s.strip == repl['map'].to_s.strip) && (orig['reduce'].to_s.strip == repl['reduce'].to_s.strip)
|
120
|
+
end
|
121
|
+
|
122
|
+
end # module ClassMethods
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module CouchRest
|
2
|
+
module Model
|
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
|
+
alias :find :get
|
60
|
+
|
61
|
+
# Load a document from the database by id
|
62
|
+
# An exception will be raised if the document isn't found
|
63
|
+
#
|
64
|
+
# ==== Returns
|
65
|
+
# Object:: if the document was found
|
66
|
+
# or
|
67
|
+
# Exception
|
68
|
+
#
|
69
|
+
# === Parameters
|
70
|
+
# id<String, Integer>:: Document ID
|
71
|
+
# db<Database>:: optional option to pass a custom database to use
|
72
|
+
def get!(id, db = database)
|
73
|
+
doc = db.get id
|
74
|
+
create_from_database(doc)
|
75
|
+
end
|
76
|
+
alias :find! :get!
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module CouchRest
|
3
|
+
module Model
|
4
|
+
module Errors
|
5
|
+
|
6
|
+
class CouchRestModelError < StandardError; end
|
7
|
+
|
8
|
+
# Raised when a persisence method ending in ! fails validation. The message
|
9
|
+
# will contain the full error messages from the +Document+ in question.
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# <tt>Validations.new(person.errors)</tt>
|
14
|
+
class Validations < CouchRestModelError
|
15
|
+
attr_reader :document
|
16
|
+
def initialize(document)
|
17
|
+
@document = document
|
18
|
+
super("Validation Failed: #{@document.errors.full_messages.join(", ")}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|