1 class ArvadosResourceList
2 include ArvadosApiClientHelper
5 attr_reader :resource_class
7 def initialize resource_class=nil
8 @resource_class = resource_class
9 @fetch_multiple_pages = true
10 @arvados_api_token = Thread.current[:arvados_api_token]
11 @reader_tokens = Thread.current[:reader_tokens]
19 def limit(max_results)
20 if not max_results.nil? and not max_results.is_a? Integer
21 raise ArgumentError("argument to limit() must be an Integer or nil")
32 def order(orderby_spec)
33 @orderby_spec = orderby_spec
37 def select(columns=nil)
38 # If no column arguments were given, invoke Enumerable#select.
56 @cond.keys.each do |uuid_key|
57 if @cond[uuid_key] and (@cond[uuid_key].is_a? Array or
58 @cond[uuid_key].is_a? ArvadosBase)
59 # Coerce cond[uuid_key] to an array of uuid strings. This
60 # allows caller the convenience of passing an array of real
61 # objects and uuids in cond[uuid_key].
62 if !@cond[uuid_key].is_a? Array
63 @cond[uuid_key] = [@cond[uuid_key]]
65 @cond[uuid_key] = @cond[uuid_key].collect do |item|
66 if item.is_a? ArvadosBase
74 @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
75 if @cond[kind_key].is_a? Class
76 @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) })
82 def fetch_multiple_pages(f)
83 @fetch_multiple_pages = f
99 @items_available = r.items_available if r.respond_to? :items_available
100 @result_limit = r.limit if r.respond_to? :limit
101 @result_offset = r.offset if r.respond_to? :offset
113 self.each_page do |items|
131 results.send('[]', *x)
143 Hash[self.collect { |x| [x.uuid, x] }]
165 # Obsolete method retained during api transition.
166 def links_for item_or_uuid, link_class=false
176 api_params[:where] = @cond if @cond
177 api_params[:eager] = '1' if @eager
178 api_params[:select] = @select if @select
179 api_params[:order] = @orderby_spec if @orderby_spec
180 api_params[:filters] = @filters if @filters
184 offset = @offset || 0
189 api_params[:offset] = offset
190 api_params[:limit] = (@limit - item_count) if @limit
192 res = arvados_api_client.api(@resource_class, '', api_params,
193 arvados_api_token: @arvados_api_token,
194 reader_tokens: @reader_tokens)
195 items = arvados_api_client.unpack_api_response res
197 @items_available = items.items_available if items.respond_to?(:items_available)
198 @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
199 @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
201 break if items.nil? or not items.any?
203 item_count += items.size
204 if items.respond_to?(:offset)
205 offset = items.offset + items.size
212 break if @limit and item_count >= @limit
213 break if items.respond_to? :items_available and offset >= items.items_available
214 end while @fetch_multiple_pages