Class: Poliqarp::QueryResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/poliqarpr/query_result.rb

Overview

Author

Aleksander Pohl ([email protected])

License

MIT License

The query result class is used to paginate results of the query. Each query result has information about its context (the next and previous page).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, page_count, page_size, client, query) ⇒ QueryResult

Returns a new instance of QueryResult.



13
14
15
16
17
18
19
20
# File 'lib/poliqarpr/query_result.rb', line 13

def initialize(page, page_count, page_size, client, query)
  @page = page
  @page_count = page_count
  @page_size = page_size
  @client = client
  @query = query
  @excerpts = []
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



11
12
13
# File 'lib/poliqarpr/query_result.rb', line 11

def page
  @page
end

#page_countObject

Returns the value of attribute page_count.



11
12
13
# File 'lib/poliqarpr/query_result.rb', line 11

def page_count
  @page_count
end

#page_sizeObject

Returns the value of attribute page_size.



11
12
13
# File 'lib/poliqarpr/query_result.rb', line 11

def page_size
  @page_size
end

#queryObject

Returns the value of attribute query.



11
12
13
# File 'lib/poliqarpr/query_result.rb', line 11

def query
  @query
end

Instance Method Details

#<<(excerpt) ⇒ Object

Adds excerpt to the query result



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

def <<(excerpt)
  @excerpts << excerpt
end

#==(other) ⇒ Object

Two excerpts are equal iff their page number, page count, query and page size are equal.



45
46
47
48
49
# File 'lib/poliqarpr/query_result.rb', line 45

def ==(other)
  return false unless other.is_a? QueryResult
  @page == other.page && @page_count == other.page_count &&
    @query == other.query && @page_size == other.page_size
end

#[](index) ⇒ Object

Returns excerpt with given index.



39
40
41
# File 'lib/poliqarpr/query_result.rb', line 39

def [](index)
  @excerpts[index]
end

#eachObject

Allows to iterate over the results stored in the result



28
29
30
# File 'lib/poliqarpr/query_result.rb', line 28

def each
  @excerpts.each{|e| yield e}
end

#next_pageObject

Return the next page of the query result



60
61
62
63
64
65
# File 'lib/poliqarpr/query_result.rb', line 60

def next_page
  if @page < @page_count
    @client.find(@query, :page_size => @page_size,
                 :page_index => @page + 1)
  end
end

#previous_pageObject

Returns the previous page of the query result



52
53
54
55
56
57
# File 'lib/poliqarpr/query_result.rb', line 52

def previous_page
  if @page > 1
    @client.find(@query, :page_size => @page_size,
                 :page_index => @page - 1)
  end
end

#sizeObject

Returns the number of excerpts stored in this page (query result)



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

def size
  @excerpts.size
end

#to_aObject

Converts current query result page into an array.



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

def to_a
  @excerpts.dup
end