Sha256: f69ea411fcf4363b8f9aea3e32a028e1e4861ca8f9a410666e0c4fefa9a469f5

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

module TheCity

  class UserRoleList 

    include Enumerable

    attr_reader :total_entries, :total_pages, :per_page, :current_page

    # Constructor.
    #
    # @param options A hash of options for loading the list.
    # 
    # Options:
    #   :user_id - The ID of the user to load the roles for. (required)
    #   :page - The page number to get.
    #   :reader - The Reader to use to load the data.
    #
    #
    # Examples:
    #   UserRoleList.new({:user_id => 12345})
    #
    #   UserRoleList.new({:user_id => 12345, :page => 2})
    #    
    def initialize(options = {}) 
      options[:page] ||= 1
      reader = options[:reader] || TheCity::UserRoleListReader.new(options)  
      @json_data = reader.load_feed

      @user_id = options[:user_id] || nil
      @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 roles in the list.
    #
    # @return array of role names.
    def all_roles
      return [] if @json_data['roles'].nil?
      @json_data['roles'].collect { |role| role['title'] }
    end
    alias :roles :all_roles
        
    
    # Get the specified user role.
    #
    # @param index The index of the user role to get.
    #
    # @return [UserRole]
    def [](index)
      UserRole.new( @json_data['roles'][index].merge({:user_id => @user_id}) ) if @json_data['roles'][index]
    end


    # This method is needed for Enumerable.
    def each &block
      @json_data['roles'].each{ |role| yield( UserRole.new(role.merge({:user_id => @user_id})) )}
    end    
  
    # Alias the count method
    alias :size :count

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

  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
the-city-admin-0.5.2 lib/api/user_role_list.rb
the-city-admin-0.5.1 lib/api/user_role_list.rb
the-city-admin-0.5.0 lib/api/user_role_list.rb
the-city-admin-0.4.0 lib/api/user_role_list.rb
the-city-admin-0.3.1 lib/api/user_role_list.rb
the-city-admin-0.3.0 lib/api/user_role_list.rb
the-city-admin-0.2.1 lib/api/user_role_list.rb
the-city-admin-0.2.0 lib/api/user_role_list.rb
the-city-admin-0.1.5 lib/api/user_role_list.rb
the-city-admin-0.1.4 lib/api/user_role_list.rb
the-city-admin-0.1.3 lib/api/user_role_list.rb
the-city-admin-0.1.2 lib/api/user_role_list.rb
the-city-admin-0.1.1 lib/api/user_role_list.rb
the-city-admin-0.1.0 lib/api/user_role_list.rb