Sha256: 5a3b1ef72d00031e6345374dcfa4c7d97c336b697e8e244ae41e706cde7cd65d

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

module TheCity

  class UserList < ApiList

    include Enumerable

    # Constructor.
    #
    # @param options A hash of options for loading the list.
    # 
    # Options:
    #   :page - The page number to get.
    #   :reader - The Reader to use to load the data.
    #
    #
    # Examples:
    #   UserList.new
    #
    #   UserList.new({:page => 2})
    #    
    def initialize(options = {}) 
      @options = options.clone
      @options[:page] ||= 1
      @options[:reader] = TheCity::UserListReader.new(@options) if @options[:reader].nil?
      @json_data = @options[:reader].load_feed 

      @total_entries = @json_data['total_entries']
      @total_pages = @json_data['total_pages']
      @per_page = @json_data['per_page']
      @current_page = @json_data['current_page'] 
    end
    
    
    # All the users in the list.
    #
    # @return array of names (first last).
    def all_names
      return [] unless @json_data['users']
      @json_data['users'].collect { |user| [user['first'], user['last']].join(' ') }
    end
    alias :names :all_names
    
    
    # Get the specified user.
    #
    # @param index The index of the user to get.
    #
    # @return [User]
    def [](index)  
      User.new( @json_data['users'][index] ) if @json_data['users'] and @json_data['users'][index]
    end


    # This method is needed for Enumerable.
    def each &block
      @json_data['users'].each{ |user| yield( User.new(user) )}
    end    
  
    # Alias the count method
    alias :size :count

    # Checks if the list is empty.
    #
    # @return True on empty, false otherwise.
    def empty?
      @json_data['users'].empty?
    end   
  
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
the-city-admin-0.6.5 lib/api/user_list.rb
the-city-admin-0.6.4 lib/api/user_list.rb
the-city-admin-0.6.3 lib/api/user_list.rb
the-city-admin-0.6.2 lib/api/user_list.rb
the-city-admin-0.6.1 lib/api/user_list.rb
the-city-admin-0.6.0 lib/api/user_list.rb