Class: CouchRest::Mixins::Collection::CollectionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/mixins/collection.rb

Constant Summary

DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
30

Instance Method Summary (collapse)

Constructor Details

- (CollectionProxy) initialize(database, design_doc, view_name, view_options = {}, container_class = nil, query_type = :view)

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.

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/couchrest/mixins/collection.rb', line 116

def initialize(database, design_doc, view_name, view_options = {}, 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

  strip_pagination_options(view_options)
  @view_options = view_options

  if design_doc.class == Design
    @view_name = "#{design_doc.name}/#{view_name}"
  else
    @view_name = "#{design_doc}/#{view_name}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args) (private)



178
179
180
181
182
183
184
185
186
# File 'lib/couchrest/mixins/collection.rb', line 178

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

- (Object) ===(other)

Explicitly proxy === because the instance method removal above doesn't catch it.



171
172
173
174
# File 'lib/couchrest/mixins/collection.rb', line 171

def ===(other)
  load_target
  other === @target
end

- (Object) paginate(options = {})

See Collection.paginate



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/couchrest/mixins/collection.rb', line 134

def paginate(options = {})
  page, per_page = parse_options(options)
  results = @database.send(@query_type, @view_name, pagination_options(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

- (Object) paginated_each(options = {}, &block)

See Collection.paginated_each



155
156
157
158
159
160
161
162
163
# File 'lib/couchrest/mixins/collection.rb', line 155

def paginated_each(options = {}, &block)
  page, per_page = parse_options(options)

  begin
    collection = paginate({:page => page, :per_page => per_page})
    collection.each(&block)
    page += 1
  end until collection.size < per_page
end

- (Object) proxy_respond_to?



103
# File 'lib/couchrest/mixins/collection.rb', line 103

alias_method :proxy_respond_to?, :respond_to?

- (Boolean) respond_to?(*args)

Returns:

  • (Boolean)


165
166
167
# File 'lib/couchrest/mixins/collection.rb', line 165

def respond_to?(*args)
  proxy_respond_to?(*args) || (load_target && @target.respond_to?(*args))
end