Class: CouchRest::Model::Collection::CollectionProxy
- Inherits:
-
Object
- Object
- CouchRest::Model::Collection::CollectionProxy
- Defined in:
- lib/couchrest/model/collection.rb
Constant Summary collapse
- DEFAULT_PAGE =
1
- DEFAULT_PER_PAGE =
30
Instance Method Summary collapse
-
#===(other) ⇒ Object
Explicitly proxy === because the instance method removal above doesn’t catch it.
-
#initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view) ⇒ CollectionProxy
constructor
Create a new CollectionProxy to represent the specified view.
-
#paginate(options = {}) ⇒ Object
See Collection.paginate.
-
#paginated_each(options = {}, &block) ⇒ Object
See Collection.paginated_each.
- #proxy_respond_to? ⇒ Object
- #respond_to?(*args) ⇒ Boolean
Constructor Details
#initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view) ⇒ CollectionProxy
Create a new CollectionProxy to represent the specified view. If a container class is specified, the proxy will create an object of the given type for each row that comes back from the view. If no container class is specified, the raw results are returned.
The CollectionProxy provides support for paginating over a collection via the paginate, and paginated_each methods.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/couchrest/model/collection.rb', line 125 def initialize(database, design_doc, view_name, = {}, container_class = nil, query_type = :view) raise ArgumentError, "database is a required parameter" if database.nil? @database = database @container_class = container_class @query_type = query_type () @view_options = if design_doc.class == Design @view_name = "#{design_doc.name}/#{view_name}" else @view_name = "#{design_doc}/#{view_name}" end # Save the design doc, ready for use @container_class.save_design_doc(@database) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
190 191 192 193 194 195 196 197 198 |
# File 'lib/couchrest/model/collection.rb', line 190 def method_missing(method, *args) if load_target if block_given? @target.send(method, *args) { |*block_args| yield(*block_args) } else @target.send(method, *args) end end end |
Instance Method Details
#===(other) ⇒ Object
Explicitly proxy === because the instance method removal above doesn’t catch it.
183 184 185 186 |
# File 'lib/couchrest/model/collection.rb', line 183 def ===(other) load_target other === @target end |
#paginate(options = {}) ⇒ Object
See Collection.paginate
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/couchrest/model/collection.rb', line 146 def paginate( = {}) page, per_page = () results = @database.send(@query_type, @view_name, (page, per_page)) remember_where_we_left_off(results, page) instances = convert_to_container_array(results) begin if Kernel.const_get('WillPaginate') total_rows = results['total_rows'].to_i paginated = WillPaginate::Collection.create(page, per_page, total_rows) do |pager| pager.replace(instances) end return paginated end rescue NameError # When not using will_paginate, not much we could do about this. :x end return instances end |
#paginated_each(options = {}, &block) ⇒ Object
See Collection.paginated_each
167 168 169 170 171 172 173 174 175 |
# File 'lib/couchrest/model/collection.rb', line 167 def paginated_each( = {}, &block) page, per_page = () begin collection = paginate({:page => page, :per_page => per_page}) collection.each(&block) page += 1 end until collection.size < per_page end |
#proxy_respond_to? ⇒ Object
112 |
# File 'lib/couchrest/model/collection.rb', line 112 alias_method :proxy_respond_to?, :respond_to? |
#respond_to?(*args) ⇒ Boolean
177 178 179 |
# File 'lib/couchrest/model/collection.rb', line 177 def respond_to?(*args) proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args)) end |