Sha256: d3279f12af560b1a5694ce24ea84d8a6408dcf1076dd3c4e802500bc4f88caea

Contents?: true

Size: 1.45 KB

Versions: 9

Compression:

Stored size: 1.45 KB

Contents

require 'geminabox/gem_version'

# This class represents a sorted collection of Geminabox::GemVersion objects.
# It it used widely throughout the system for displaying and filtering gems.
class Geminabox::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

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
geminabox-0.11.1 lib/geminabox/gem_version_collection.rb
geminabox-inflection-0.12.0 lib/geminabox/gem_version_collection.rb
geminabox-inflection-0.11.0 lib/geminabox/gem_version_collection.rb
geminabox-0.11.0 lib/geminabox/gem_version_collection.rb
geminabox-bootstrap-0.10.3 lib/geminabox/gem_version_collection.rb
geminabox-0.10.1 lib/geminabox/gem_version_collection.rb
geminabox-0.10.0 lib/geminabox/gem_version_collection.rb
geminabox-0.9.0 lib/geminabox/gem_version_collection.rb
geminabox-0.8.0 lib/geminabox/gem_version_collection.rb