Sha256: dde1a8d825a7dfac09445b635cca9de5846dc438e07c10267a8b87042a9a9266

Contents?: true

Size: 1013 Bytes

Versions: 4

Compression:

Stored size: 1013 Bytes

Contents

require 'geminabox/gem_version'

class Geminabox::GemVersionCollection
  include Enumerable

  attr_reader :gems

  def initialize(initial_gems=[])
    @gems = []
    initial_gems.each { |gemdef| self << gemdef }
  end

  def <<(version_or_def)
    version = if version_or_def.is_a?(Geminabox::GemVersion)
                version_or_def
              else
                Geminabox::GemVersion.new(*version_or_def)
              end

    @gems << version
    @gems.sort!
  end

  def oldest
    @gems.first
  end

  def newest
    @gems.last
  end

  def |(other)
    self.class.new(self.gems | other.gems)
  end

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

  def by_name
    grouped = @gems.inject(hash_of_collections) do |grouped, gem|
      grouped[gem.name] << gem
      grouped
    end.sort_by{|name, gems| name.downcase }

    if block_given?
      grouped.each(&Proc.new)
    else
      grouped
    end
  end

  private
  def hash_of_collections
    Hash.new { |h,k| h[k] = self.class.new }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geminabox-0.6.1pre1 lib/geminabox/gem_version_collection.rb
geminabox-0.6.0 lib/geminabox/gem_version_collection.rb
geminabox-0.5.2 lib/geminabox/gem_version_collection.rb
geminabox-0.5.1 lib/geminabox/gem_version_collection.rb