Class: AWeber::Collection
- Includes:
- Enumerable
- Defined in:
- lib/aweber/collection.rb
Overview
Collection objects are groups of Resources. Collections imitate regular Hashes in most ways. You can access Resource by id
via the []
method.
lists #=> #<AWeber::Collection ...>
lists[1] #=> #<AWeber::Resources::List @id=1 ...>
Also like Hashes, you can iterate over all of it’s entries
lists.each { |id, list| puts list.name }
Collections support dynamic find_by_*
methods, where * is an attribute and the first and only parameter is the value.
lists.find_by_name("testlist")
#=> #<AWeber::Resources::List @id=123, @name="testlist" ...>
find_by_*
methods will also return a Hash of entries if more than one matches the criteria. The hash will be keyed by resource ID.
.find_by_total_opens(0)
#=> { "45697" => #<Message>, "12345" => #<Message> }
Collections are paginated in groups of 20.
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#next_collection_link ⇒ Object
readonly
Returns the value of attribute next_collection_link.
-
#prev_collection_link ⇒ Object
readonly
Returns the value of attribute prev_collection_link.
-
#resource_type_link ⇒ Object
readonly
Returns the value of attribute resource_type_link.
-
#total_size ⇒ Object
(also: #size, #length)
readonly
Returns the value of attribute total_size.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #each ⇒ Object
-
#initialize(client, klass, data = {}) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
Methods inherited from Resource
#<=>, alias_attribute, api_attr, #delete, has_many, has_one, #save, #writable_attrs
Constructor Details
#initialize(client, klass, data = {}) ⇒ Collection
Returns a new instance of Collection.
43 44 45 46 47 48 49 50 |
# File 'lib/aweber/collection.rb', line 43 def initialize(client, klass, data={}) super client, data @client = client @klass = klass @entries = {} create_entries(data["entries"]) @_entries = @entries.to_a end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
98 99 100 101 102 103 |
# File 'lib/aweber/collection.rb', line 98 def method_missing(method, *args) method.to_s.scan /^find_by_(.+)/ do |_attr| return find_by(_attr.first, *args) end super end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
30 31 32 |
# File 'lib/aweber/collection.rb', line 30 def entries @entries end |
#next_collection_link ⇒ Object (readonly)
Returns the value of attribute next_collection_link.
31 32 33 |
# File 'lib/aweber/collection.rb', line 31 def next_collection_link @next_collection_link end |
#prev_collection_link ⇒ Object (readonly)
Returns the value of attribute prev_collection_link.
32 33 34 |
# File 'lib/aweber/collection.rb', line 32 def prev_collection_link @prev_collection_link end |
#resource_type_link ⇒ Object (readonly)
Returns the value of attribute resource_type_link.
33 34 35 |
# File 'lib/aweber/collection.rb', line 33 def resource_type_link @resource_type_link end |
#total_size ⇒ Object (readonly) Also known as: size, length
Returns the value of attribute total_size.
34 35 36 |
# File 'lib/aweber/collection.rb', line 34 def total_size @total_size end |
Instance Method Details
#[](id) ⇒ Object
52 53 54 |
# File 'lib/aweber/collection.rb', line 52 def [](id) @entries[id] ||= fetch_entry(id) end |
#each ⇒ Object
56 57 58 |
# File 'lib/aweber/collection.rb', line 56 def each (1..@total_size).each { |n| yield get_entry(n) } end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/aweber/collection.rb', line 60 def inspect "#<AW::Collection(#{@klass.to_s}) size=\"#{size}\">" end |