Class: QueryResult

Inherits:
RestObject show all
Includes:
Enumerable
Defined in:
lib/rally_rest_api/query_result.rb,
lib/rally_rest_api/ruport.rb

Overview

An interface to the paged query result

QueryResult is a wrapper around the xml returned from a webservice query operation. A query could result in a large number of hits being returned, therefore the results are paged into page_size chunks (20 by default). QueryResult will seamlessly deal with the paging when using the #each iterator.

Example

rally = RallyRestAPI.new(...)
results = rally.find(:defect) { equal :name, "My Defects" }
results.each do |defect|
  defect.update(:state => "Closed")
end

Instance Attribute Summary collapse

Attributes inherited from RestObject

#rally_rest

Instance Method Summary collapse

Methods inherited from RestObject

#<=>, #==, #body, #delete, #elements, #eql?, #hash, #marshal_dump, #marshal_load, #method_missing, #name, #oid, #parse_document, #ref, #refresh, #save!, #to_hash, #type, #type=, #type_as_symbol, #typedef, #underscore, #update

Constructor Details

#initialize(query, rally_rest, document_content) ⇒ QueryResult

Returns a new instance of QueryResult.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rally_rest_api/query_result.rb', line 26

def initialize(query, rally_rest, document_content)
  super(rally_rest, document_content)
  elements[:results] = case self.results
    when Array then self.results.flatten
    when Hash then self.results.values.flatten
    when nil then []
  end
                         
  @query = query

  @total_result_count = elements[:total_result_count].to_i
  @page_size = elements[:page_size].to_i
  @start_index = elements[:start_index].to_i
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RestObject

Instance Attribute Details

#page_sizeObject (readonly)

Returns the value of attribute page_size.



23
24
25
# File 'lib/rally_rest_api/query_result.rb', line 23

def page_size
  @page_size
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



24
25
26
# File 'lib/rally_rest_api/query_result.rb', line 24

def start_index
  @start_index
end

#total_result_countObject (readonly)

Returns the value of attribute total_result_count.



22
23
24
# File 'lib/rally_rest_api/query_result.rb', line 22

def total_result_count
  @total_result_count
end

Instance Method Details

#eachObject

Iteration all pages of the result



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rally_rest_api/query_result.rb', line 48

def each
  current_result = self
  begin 
    last_result = current_result
    current_result.elements[:results].each do |result|
	# The collection of refs we are holding onto could grow without bounds, so dup
	# the ref
	yield result.dup
    end
    current_result = current_result.next_page if current_result.more_pages?
  end while !last_result.equal? current_result
end

#firstObject

return the first element. Useful for queries that return only one result



62
63
64
# File 'lib/rally_rest_api/query_result.rb', line 62

def first
  results.first
end

#more_pages?Boolean

Are there more pages?

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/rally_rest_api/query_result.rb', line 73

def more_pages?
  return false if start_index == 0
  (self.start_index + self.page_length) -1 < self.total_result_count
end

#next_pageObject

fetch the next page of results. Uses the original query to generate the query string.



42
43
44
45
# File 'lib/rally_rest_api/query_result.rb', line 42

def next_page
  @rally_rest.query(@query.next_page(:start => self.start_index + self.page_size,
		       :pagesize => self.page_size))
end

#page_lengthObject

The length of the current page of results



67
68
69
70
# File 'lib/rally_rest_api/query_result.rb', line 67

def page_length
  return 0 if self.elements[:results].nil?
  self.elements[:results].length
end

#to_table(columns = []) ⇒ Object

return a Ruport::Data::Table. Takes an array of columns for the report

defects = rally.find(:defect, :fetch => true) { equal :state, "Open }
table = defects.to_table([:name, :severity, :priority, :owner])
table.to_pdf


21
22
23
24
25
# File 'lib/rally_rest_api/ruport.rb', line 21

def to_table(columns = [])
  table = Ruport::Data::Table.new(:column_names => columns)
  self.each { |i| table << i }
  table
end