Class: ActiveRestClient::ResultIterator
- Inherits:
-
Object
- Object
- ActiveRestClient::ResultIterator
- Includes:
- Enumerable
- Defined in:
- lib/active_rest_client/result_iterator.rb
Instance Attribute Summary collapse
-
#_status ⇒ Object
Returns the value of attribute _status.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](key) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #index(value) ⇒ Object
-
#initialize(status = nil) ⇒ ResultIterator
constructor
A new instance of ResultIterator.
- #last ⇒ Object
- #paginate(options = {}) ⇒ Object
- #parallelise(method = nil) ⇒ Object
- #reverse ⇒ Object
- #shuffle ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(status = nil) ⇒ ResultIterator
Returns a new instance of ResultIterator.
8 9 10 11 |
# File 'lib/active_rest_client/result_iterator.rb', line 8 def initialize(status = nil) @_status = status @items = [] end |
Instance Attribute Details
#_status ⇒ Object
Returns the value of attribute _status.
5 6 7 |
# File 'lib/active_rest_client/result_iterator.rb', line 5 def _status @_status end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/active_rest_client/result_iterator.rb', line 6 def items @items end |
Instance Method Details
#<<(item) ⇒ Object
13 14 15 |
# File 'lib/active_rest_client/result_iterator.rb', line 13 def <<(item) @items << item end |
#[](key) ⇒ Object
43 44 45 |
# File 'lib/active_rest_client/result_iterator.rb', line 43 def [](key) @items[key] end |
#each ⇒ Object
33 34 35 36 37 |
# File 'lib/active_rest_client/result_iterator.rb', line 33 def each @items.each do |el| yield el end end |
#empty? ⇒ Boolean
25 26 27 |
# File 'lib/active_rest_client/result_iterator.rb', line 25 def empty? size == 0 end |
#index(value) ⇒ Object
21 22 23 |
# File 'lib/active_rest_client/result_iterator.rb', line 21 def index(value) @items.index(value) end |
#last ⇒ Object
39 40 41 |
# File 'lib/active_rest_client/result_iterator.rb', line 39 def last @items.last end |
#paginate(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_rest_client/result_iterator.rb', line 69 def paginate( = {}) raise WillPaginateNotAvailableException.new unless Object.constants.include?(:WillPaginate) page = [:page] || 1 per_page = [:per_page] || WillPaginate.per_page total = [:total_entries] || @items.length WillPaginate::Collection.create(page, per_page, total) do |pager| pager.replace @items[pager.offset, pager.per_page].to_a end end |
#parallelise(method = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_rest_client/result_iterator.rb', line 52 def parallelise(method=nil) collected_responses = [] threads = [] @items.each do |item| threads << Thread.new do ret = item.send(method) if method ret = yield(item) if block_given? Thread.current[:response] = ret end end threads.each do |t| t.join collected_responses << t[:response] end collected_responses end |
#reverse ⇒ Object
29 30 31 |
# File 'lib/active_rest_client/result_iterator.rb', line 29 def reverse @reversed_items ||= @items.reverse end |
#shuffle ⇒ Object
47 48 49 50 |
# File 'lib/active_rest_client/result_iterator.rb', line 47 def shuffle @items = @items.shuffle self end |
#size ⇒ Object
17 18 19 |
# File 'lib/active_rest_client/result_iterator.rb', line 17 def size @items.size end |