Class: ActsAsIcontact::ResourceCollection
- Inherits:
-
Enumerator
- Object
- Enumerator
- ActsAsIcontact::ResourceCollection
- Defined in:
- lib/acts_as_icontact/resource_collection.rb
Instance Attribute Summary collapse
-
#collection_name ⇒ Object
readonly
Returns the value of attribute collection_name.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#retrieved ⇒ Object
readonly
Returns the value of attribute retrieved.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#first ⇒ Object
Calls “next” to kick off the enumerator.
-
#initialize(klass, collection, options = {}) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
-
#inspect ⇒ Object
Returns a nice formatted string for command line use.
Constructor Details
#initialize(klass, collection, options = {}) ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 5 def initialize(klass, collection, ={}) @klass = klass @forwardTo = .delete(:forwardTo) @parent = .delete(:parent) @collection_name = klass.collection_name @collection = collection[collection_name] # Get number of elements @retrieved = @collection.size @total = collection["total"] @offset = collection["offset"] enumcode = Proc.new do |yielder| counter = 0 while counter < @retrieved yielder.yield resource(@collection[counter]) counter += 1 end end super(&enumcode) end |
Instance Attribute Details
#collection_name ⇒ Object (readonly)
Returns the value of attribute collection_name.
3 4 5 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 3 def collection_name @collection_name end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
3 4 5 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 3 def offset @offset end |
#retrieved ⇒ Object (readonly)
Returns the value of attribute retrieved.
3 4 5 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 3 def retrieved @retrieved end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
3 4 5 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 3 def total @total end |
Instance Method Details
#[](index) ⇒ Object
28 29 30 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 28 def [](index) resource(@collection[index]) if @collection[index] end |
#first ⇒ Object
Calls “next” to kick off the enumerator. This is more in line with what users would expect.
33 34 35 36 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 33 def first self.rewind self.next end |
#inspect ⇒ Object
Returns a nice formatted string for command line use.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/acts_as_icontact/resource_collection.rb', line 39 def inspect if offset.to_i > 0 "#{retrieved} out of #{total} #{collection_name} (offset #{offset})" elsif retrieved != total "#{retrieved} out of #{total} #{collection_name}" else "#{total} #{collection_name}" end end |