class SwissMatch::Communities

Represents a collection of SwissMatch::Community objects and provides a query interface.

Public Class Methods

new(communities) click to toggle source

@param [Array<SwissMatch::Community>] communities

The SwissMatch::Community objects this SwissMatch::Communities should contain
# File lib/swissmatch/communities.rb, line 14
def initialize(communities)
  @communities          = communities
  reset!
end

Public Instance Methods

[](key) click to toggle source

@return [SwissMatch::Community]

The community with the given name or community number.
# File lib/swissmatch/communities.rb, line 97
def [](key)
  @by_name[key] || @by_community_number[key.to_i]
end
by_community_number(number) click to toggle source

@return [SwissMatch::Community]

The community with the given community number (also known as BFSNR).
# File lib/swissmatch/communities.rb, line 103
def by_community_number(number)
  @by_community_number[number]
end
by_community_numbers(*numbers) click to toggle source

@return [Array<SwissMatch::Community>]

The communities with the given community numbers (also known as BFSNR).
# File lib/swissmatch/communities.rb, line 109
def by_community_numbers(*numbers)
  @by_community_number.values_at(*numbers)
end
by_name(name) click to toggle source

@return [SwissMatch::Community]

The community with the given name.
# File lib/swissmatch/communities.rb, line 115
def by_name(name)
  @by_name[name]
end
each(&block) click to toggle source

Calls the block once for every SwissMatch::Community in this SwissMatch::Communities instance, passing that community as a parameter. The order is the same as the instance was constructed.

@yield [community] @yieldparam [SwissMatch::Community] community

@return [self] Returns self

# File lib/swissmatch/communities.rb, line 47
def each(&block)
  @communities.each(&block)
  self
end
inspect() click to toggle source

@private @see Object#inspect

# File lib/swissmatch/communities.rb, line 132
def inspect
  sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size
end
reject(*args, &block) click to toggle source

@return [SwissMatch::Communities]

A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block
returned false (or a falseish value)
# File lib/swissmatch/communities.rb, line 75
def reject(*args, &block)
  Communities.new(@communities.reject(*args, &block))
end
reset!() click to toggle source

@private Reinitialize all caching instance variables

# File lib/swissmatch/communities.rb, line 21
def reset!
  @by_community_number  = Hash[@communities.map { |c| [c.community_number, c] }]
  @by_name              = {}
  @communities.each do |community|
    @by_name[community.name.to_s] = community
  end

  unless @communities.size == @by_name.size
    count=Hash.new(0)
    @communities.each do |community| count[community.name.to_s] += 1 end
    non_unique = count.select { |k,v| v > 1 }.map(&:first)

    raise "ImplementationError: The author assumed communities to have a unique name, which doesn't seem to be the case anymore. Non-unique names: #{non_unique.inspect}"
  end

  self
end
reverse_each(&block) click to toggle source

Calls the block once for every SwissMatch::Community in this SwissMatch::Communities instance, passing that community as a parameter. The order is the reverse of what the instance was constructed.

@yield [community] @yieldparam [SwissMatch::Community] community

@return [self] Returns self

# File lib/swissmatch/communities.rb, line 60
def reverse_each(&block)
  @communities.reverse_each(&block)
  self
end
select(*args, &block) click to toggle source

@return [SwissMatch::Communities]

A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block
returned true (or a trueish value)
# File lib/swissmatch/communities.rb, line 68
def select(*args, &block)
  Communities.new(@communities.select(*args, &block))
end
size() click to toggle source

@return [Integer] The number of SwissMatch::Community objects in this collection.

# File lib/swissmatch/communities.rb, line 120
def size
  @communities.size
end
sort(*args, &block) click to toggle source

@see Enumerable#sort

@return [SwissMatch::Communities]

A SwissMatch::Communities collection sorted by the block
# File lib/swissmatch/communities.rb, line 83
def sort(*args, &block)
  Communities.new(@communities.sort(*args, &block))
end
sort_by(*args, &block) click to toggle source

@see Enumerable#sort_by

@return [SwissMatch::Communities]

A SwissMatch::Communities collection sorted by the block
# File lib/swissmatch/communities.rb, line 91
def sort_by(*args, &block)
  Communities.new(@communities.sort_by(*args, &block))
end
to_a() click to toggle source

@return [Array<SwissMatch::Community>]

An Array with all SwissMatch::Community objects in this SwissMatch::Communities.
# File lib/swissmatch/communities.rb, line 126
def to_a
  @communities.dup
end