Class: Hackeroo::Cursor
- Inherits:
-
Object
- Object
- Hackeroo::Cursor
- Includes:
- Enumerable
- Defined in:
- lib/hackeroo/cursor.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_hash)
readonly
Returns the value of attribute attrs.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Class Method Summary collapse
-
.from_response(response, collection_name, klass, client, method_name, method_options) ⇒ Hackeroo::Cursor
Initializes a new Cursor object.
Instance Method Summary collapse
- #all(collection = collection, cursor = next_cursor) ⇒ Array
- #each ⇒ Enumerable
- #first? ⇒ Boolean (also: #first)
-
#initialize(attrs, collection_name, klass, client, method_name, method_options) ⇒ Hackeroo::Cursor
constructor
Initializes a new Cursor.
- #last? ⇒ Boolean (also: #last)
- #next_cursor ⇒ Object (also: #next)
- #previous_cursor ⇒ Object (also: #previous)
Methods included from Enumerable
Constructor Details
#initialize(attrs, collection_name, klass, client, method_name, method_options) ⇒ Hackeroo::Cursor
Initializes a new Cursor
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hackeroo/cursor.rb', line 31 def initialize(attrs, collection_name, klass, client, method_name, ) @attrs = attrs @client = client @method_name = method_name @method_options = @collection = Array(attrs[collection_name.to_sym]).map do |item| if klass klass.fetch_or_new(item) else item end end singleton_class.class_eval do alias_method(collection_name.to_sym, :collection) end end |
Instance Attribute Details
#attrs ⇒ Object (readonly) Also known as: to_hash
Returns the value of attribute attrs.
6 7 8 |
# File 'lib/hackeroo/cursor.rb', line 6 def attrs @attrs end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/hackeroo/cursor.rb', line 6 def collection @collection end |
Class Method Details
.from_response(response, collection_name, klass, client, method_name, method_options) ⇒ Hackeroo::Cursor
Initializes a new Cursor object
18 19 20 |
# File 'lib/hackeroo/cursor.rb', line 18 def self.from_response(response, collection_name, klass, client, method_name, ) new(response[:body], collection_name, klass, client, method_name, ) end |
Instance Method Details
#all(collection = collection, cursor = next_cursor) ⇒ Array
51 52 53 54 55 |
# File 'lib/hackeroo/cursor.rb', line 51 def all(collection=collection, cursor=next_cursor) cursor = @client.send(@method_name.to_sym, @method_options.merge(:cursor => cursor)) collection += cursor.collection cursor.last? ? collection.flatten : all(collection, cursor.next_cursor) end |
#each ⇒ Enumerable
58 59 60 61 62 |
# File 'lib/hackeroo/cursor.rb', line 58 def each all(collection, next_cursor).each do |element| yield element end end |
#first? ⇒ Boolean Also known as: first
75 76 77 |
# File 'lib/hackeroo/cursor.rb', line 75 def first? previous_cursor.zero? end |
#last? ⇒ Boolean Also known as: last
81 82 83 |
# File 'lib/hackeroo/cursor.rb', line 81 def last? next_cursor.zero? end |
#next_cursor ⇒ Object Also known as: next
64 65 66 |
# File 'lib/hackeroo/cursor.rb', line 64 def next_cursor @attrs[:next_cursor] || -1 end |
#previous_cursor ⇒ Object Also known as: previous
69 70 71 |
# File 'lib/hackeroo/cursor.rb', line 69 def previous_cursor @attrs[:previous_cursor] end |