Class: AppStore::List
- Inherits:
-
Object
- Object
- AppStore::List
- Includes:
- Enumerable
- Defined in:
- lib/app_store/list.rb
Overview
Represents a list based on data from Apple AppStore. If a list contains too much elements (> 25), the Apple AppStore sends only 24 elements followed by a link for the next 24 elements. This class represents an abstraction of Apple AppStore lists, have a real count attribute and is enumerable over the entire list.
Example
list = Category.featured.first.items
list.count # => 23453
list.elements # => [AppStore::Category, AppStore::Category, ...]
list.collect {|item| item.item_id} # => [9843509, 9028423, 8975435, 987345, ...]
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
All the elements already gathered.
Instance Method Summary collapse
-
#collect ⇒ Object
Iterates the given block using each object in the list, returns an array containing the execution block result for each element.
-
#count ⇒ Object
Returns the real elements count, not a count based on the elements already fetched.
-
#each(&block) ⇒ Object
Iterates the given block using each object in the list.
-
#initialize(attrs = {}) ⇒ List
constructor
A required attribute
:element_initializercan be used to specify a block used for the initialization of each element of the list.
Constructor Details
#initialize(attrs = {}) ⇒ List
A required attribute :element_initializer can be used to specify a block used for the initialization of each element of the list
23 24 25 26 27 28 29 30 |
# File 'lib/app_store/list.rb', line 23 def initialize(attrs = {}) @element_initializer = attrs[:element_initializer] @element_type = attrs[:element_type] @client = attrs[:client] || AppStore::Client.new @elements ||= [] process_new_elements attrs[:list] end |
Instance Attribute Details
#elements ⇒ Object (readonly)
All the elements already gathered.
18 19 20 |
# File 'lib/app_store/list.rb', line 18 def elements @elements end |
Instance Method Details
#collect ⇒ Object
Iterates the given block using each object in the list, returns an array containing the execution block result for each element.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/app_store/list.rb', line 46 def collect # First, iterate through already fetched elements result = @elements.collect {|element| yield element} # Then, iterate until we have no more links to follow while (last_elements = fetch_next_elements) do result += last_elements.collect {|element| yield element} end # Returns full array result end |
#count ⇒ Object
Returns the real elements count, not a count based on the elements already fetched.
34 35 36 |
# File 'lib/app_store/list.rb', line 34 def count @count ||= @elements.count end |
#each(&block) ⇒ Object
Iterates the given block using each object in the list.
39 40 41 42 |
# File 'lib/app_store/list.rb', line 39 def each(&block) collect(&block) @elements end |