Class: ActiveRestClient::ResultIterator
- Inherits:
-
Object
- Object
- ActiveRestClient::ResultIterator
- Includes:
- Enumerable
- Defined in:
- lib/active_rest_client/result_iterator.rb
Instance Attribute Summary collapse
-
#_headers ⇒ Object
readonly
Returns the value of attribute _headers.
-
#_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(response = 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(response = nil) ⇒ ResultIterator
Returns a new instance of ResultIterator.
8 9 10 11 12 |
# File 'lib/active_rest_client/result_iterator.rb', line 8 def initialize(response = nil) @_status = response.try :status @_headers = response.try :response_headers @items = [] end |
Instance Attribute Details
#_headers ⇒ Object (readonly)
Returns the value of attribute _headers.
6 7 8 |
# File 'lib/active_rest_client/result_iterator.rb', line 6 def _headers @_headers end |
#_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
14 15 16 |
# File 'lib/active_rest_client/result_iterator.rb', line 14 def <<(item) @items << item end |
#[](key) ⇒ Object
44 45 46 |
# File 'lib/active_rest_client/result_iterator.rb', line 44 def [](key) @items[key] end |
#each ⇒ Object
34 35 36 37 38 |
# File 'lib/active_rest_client/result_iterator.rb', line 34 def each @items.each do |el| yield el end end |
#empty? ⇒ Boolean
26 27 28 |
# File 'lib/active_rest_client/result_iterator.rb', line 26 def empty? size == 0 end |
#index(value) ⇒ Object
22 23 24 |
# File 'lib/active_rest_client/result_iterator.rb', line 22 def index(value) @items.index(value) end |
#last ⇒ Object
40 41 42 |
# File 'lib/active_rest_client/result_iterator.rb', line 40 def last @items.last end |
#paginate(options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_rest_client/result_iterator.rb', line 70 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_rest_client/result_iterator.rb', line 53 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
30 31 32 |
# File 'lib/active_rest_client/result_iterator.rb', line 30 def reverse @reversed_items ||= @items.reverse end |
#shuffle ⇒ Object
48 49 50 51 |
# File 'lib/active_rest_client/result_iterator.rb', line 48 def shuffle @items = @items.shuffle self end |
#size ⇒ Object
18 19 20 |
# File 'lib/active_rest_client/result_iterator.rb', line 18 def size @items.size end |