Class: Geminabox::GemVersionCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/geminabox/gem_version_collection.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(initial_gems = []) ⇒ GemVersionCollection

Array of Geminabox::GemVersion objects, or an array of [name, version, platform] triples.


10
11
12
13
14
# File 'lib/geminabox/gem_version_collection.rb', line 10

def initialize(initial_gems=[])
  @gems = initial_gems.map{|object|
    coerce_to_gem_version(object)
  }.sort
end

Instance Method Details

#by_name(&block) ⇒ Object

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


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/geminabox/gem_version_collection.rb', line 41

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

#each(&block) ⇒ Object


32
33
34
# File 'lib/geminabox/gem_version_collection.rb', line 32

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

#newestObject

FIXME: Terminology makes no sense when the version are not all of the same name


24
25
26
# File 'lib/geminabox/gem_version_collection.rb', line 24

def newest
  @gems.last
end

#oldestObject

FIXME: Terminology makes no sense when the version are not all of the same name


18
19
20
# File 'lib/geminabox/gem_version_collection.rb', line 18

def oldest
  @gems.first
end

#sizeObject


28
29
30
# File 'lib/geminabox/gem_version_collection.rb', line 28

def size
  @gems.size
end