Sha256: 5fc8753795e3fcb0e77d14b4e97f956acb357c0451b437deac45539f94179a5f

Contents?: true

Size: 1.53 KB

Versions: 34

Compression:

Stored size: 1.53 KB

Contents

module Geminabox

  # This class represents a sorted collection of Geminabox::GemVersion objects.
  # It it used widely throughout the system for displaying and filtering gems.
  class GemVersionCollection
    include Enumerable

    # Array of Geminabox::GemVersion objects, or an array of [name, version,
    # platform] triples.
    def initialize(initial_gems=[])
      @gems = initial_gems.map{|object|
        coerce_to_gem_version(object)
      }.sort
    end

    # FIXME: Terminology makes no sense when the version are not all of the same
    # name
    def oldest
      @gems.first
    end

    # FIXME: Terminology makes no sense when the version are not all of the same
    # name
    def newest
      @gems.last
    end

    def size
      @gems.size
    end

    def each(&block)
      @gems.each(&block)
    end

    # The collection can contain gems of different names, this method groups them
    # by name, and then sorts the different version of each name by version and
    # platform.
    #
    # yields 'foo_gem', version_collection
    def by_name(&block)
      @grouped ||= @gems.group_by(&:name).map{|name, collection|
        [name, Geminabox::GemVersionCollection.new(collection)]
      }.sort_by{|name, collection|
        name.downcase
      }

      if block_given?
        @grouped.each(&block)
      else
        @grouped
      end
    end

  private
    def coerce_to_gem_version(object)
      if object.is_a?(Geminabox::GemVersion)
        object
      else
        Geminabox::GemVersion.new(*object)
      end
    end
  end
  
end

Version data entries

34 entries across 34 versions & 3 rubygems

Version Path
geminabox-1.4.1 lib/geminabox/gem_version_collection.rb
geminabox-1.4.0 lib/geminabox/gem_version_collection.rb
geminabox-1.3.1 lib/geminabox/gem_version_collection.rb
geminabox-1.3.0 lib/geminabox/gem_version_collection.rb
geminabox-1.2.0 lib/geminabox/gem_version_collection.rb
geminabox-1.1.1 lib/geminabox/gem_version_collection.rb
geminabox-1.1.0 lib/geminabox/gem_version_collection.rb
geminabox-1.0.1 lib/geminabox/gem_version_collection.rb
geminabox-1.0.0 lib/geminabox/gem_version_collection.rb
geminabox-0.13.15 lib/geminabox/gem_version_collection.rb
geminabox-0.13.14 lib/geminabox/gem_version_collection.rb
geminabox-0.13.13 lib/geminabox/gem_version_collection.rb
geminabox-0.13.11 lib/geminabox/gem_version_collection.rb
geminabox-0.13.10 lib/geminabox/gem_version_collection.rb
geminabox-0.13.9 lib/geminabox/gem_version_collection.rb
geminabox-0.13.8 lib/geminabox/gem_version_collection.rb
geminabox-0.13.7 lib/geminabox/gem_version_collection.rb
geminabox-0.13.6 lib/geminabox/gem_version_collection.rb
geminabox-0.13.5 lib/geminabox/gem_version_collection.rb
geminabox-0.13.4 lib/geminabox/gem_version_collection.rb