Sha256: 51cc51b58c304447c30293f463b3b45291dc1988635a594f2bb699019e36bdbe

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module ActsAsIcontact
  class ResourceCollection < Enumerator
    attr_reader :total, :retrieved, :offset, :collection_name
    
    def initialize(klass, collection, forwardTo=nil)
      @klass = klass
      @forwardTo = forwardTo
      
      @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
    
    def [](index)
      resource(@collection[index]) if @collection[index]
    end
    
    # Calls "next" to kick off the enumerator.  This is more in line with what users would expect.
    def first
      self.rewind
      self.next
    end
    
    # Returns a nice formatted string for command line use.
    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
    
    private 
    def resource(properties)
      if @forwardTo
        id = @forwardTo.primary_key
        @forwardTo.find(properties[id])
      else
        @klass.new(properties)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
SFEley-acts_as_icontact-0.3.2 lib/acts_as_icontact/resource_collection.rb
acts_as_icontact-0.3.2 lib/acts_as_icontact/resource_collection.rb