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

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

Constant Summary collapse

DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, design_doc, view_name, view_options = {}, container_class = nil) ⇒ 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.

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/couchrest/mixins/collection.rb', line 105

def initialize(database, design_doc, view_name, view_options = {}, container_class = nil)
  raise ArgumentError, "database is a required parameter" if database.nil?

  @database = database
  @container_class = container_class

  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

#method_missing(method, *args) ⇒ Object (private)



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

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 Attribute Details

#amount_pagesObject

Returns the value of attribute amount_pages.



96
97
98
# File 'lib/couchrest/mixins/collection.rb', line 96

def amount_pages
  @amount_pages
end

Instance Method Details

#===(other) ⇒ Object

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



150
151
152
153
# File 'lib/couchrest/mixins/collection.rb', line 150

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

#paginate(options = {}) ⇒ Object

See Collection.paginate



122
123
124
125
126
127
128
129
130
131
# File 'lib/couchrest/mixins/collection.rb', line 122

def paginate(options = {})
  page, per_page = parse_options(options)
  results = @database.view(@view_name, pagination_options(page, per_page)) 
  @amount_pages ||= (results['total_rows'].to_f / per_page.to_f).ceil
  remember_where_we_left_off(results, page)
  results = convert_to_container_array(results)
  results.extend(PaginatedResults)
  results.amount_pages = @amount_pages
  results
end

#paginated_each(options = {}, &block) ⇒ Object

See Collection.paginated_each



134
135
136
137
138
139
140
141
142
# File 'lib/couchrest/mixins/collection.rb', line 134

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

#proxy_respond_to?Object



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

alias_method :proxy_respond_to?, :respond_to?

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/couchrest/mixins/collection.rb', line 144

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