Merge branch 'master' into 3889-functional-testing
[arvados.git] / apps / workbench / app / models / arvados_resource_list.rb
index ba3f0a05bfac9b6336f20b15a9af4807e78647af..1a3c6b7e3c8e55036306743843f31839489c4da3 100644 (file)
@@ -1,4 +1,5 @@
 class ArvadosResourceList
+  include ArvadosApiClientHelper
   include Enumerable
 
   def initialize resource_class=nil
@@ -25,6 +26,17 @@ class ArvadosResourceList
     self
   end
 
+  def select(columns=nil)
+    # If no column arguments were given, invoke Enumerable#select.
+    if columns.nil?
+      super()
+    else
+      @select ||= []
+      @select += columns
+      self
+    end
+  end
+
   def filter _filters
     @filters ||= []
     @filters += _filters
@@ -53,7 +65,7 @@ class ArvadosResourceList
     end
     cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
       if cond[kind_key].is_a? Class
-        cond = cond.merge({ kind_key => 'arvados#' + $arvados_api_client.class_kind(cond[kind_key]) })
+        cond = cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(cond[kind_key]) })
       end
     end
     api_params = {
@@ -63,10 +75,11 @@ class ArvadosResourceList
     api_params[:eager] = '1' if @eager
     api_params[:limit] = @limit if @limit
     api_params[:offset] = @offset if @offset
+    api_params[:select] = @select if @select
     api_params[:order] = @orderby_spec if @orderby_spec
     api_params[:filters] = @filters if @filters
-    res = $arvados_api_client.api @resource_class, '', api_params
-    @results = $arvados_api_client.unpack_api_response res
+    res = arvados_api_client.api @resource_class, '', api_params
+    @results = arvados_api_client.unpack_api_response res
     self
   end
 
@@ -90,6 +103,12 @@ class ArvadosResourceList
     self
   end
 
+  def collect
+    results.collect do |m|
+      yield m
+    end
+  end
+
   def first
     results.first
   end
@@ -138,10 +157,15 @@ class ArvadosResourceList
     results.links if results.respond_to? :links
   end
 
-  def links_for item_or_uuid, link_class=nil
+  # Return links provided with API response that point to the
+  # specified object, and have the specified link_class. If link_class
+  # is false or omitted, return all links pointing to the specified
+  # object.
+  def links_for item_or_uuid, link_class=false
+    return [] if !result_links
     unless @links_for_uuid
       @links_for_uuid = {}
-      results.links.each do |link|
+      result_links.each do |link|
         if link.respond_to? :head_uuid
           @links_for_uuid[link.head_uuid] ||= []
           @links_for_uuid[link.head_uuid] << link
@@ -154,12 +178,13 @@ class ArvadosResourceList
       uuid = item_or_uuid
     end
     (@links_for_uuid[uuid] || []).select do |link|
-      link.link_class == link_class
+      link_class == false or link.link_class == link_class
     end
   end
 
+  # Note: this arbitrarily chooses one of (possibly) multiple names.
   def name_for item_or_uuid
-    links_for(item_or_uuid, 'name').first.name
+    links_for(item_or_uuid, 'name').first.andand.name
   end
 
 end