Class: SwissMatch::Communities

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/swissmatch/communities.rb

Overview

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

Instance Method Summary collapse

Methods included from Enumerable

#last

Constructor Details

#initialize(communities) ⇒ Communities

Returns a new instance of Communities.

Parameters:

  • communities (Array<SwissMatch::Community>)

    The SwissMatch::Community objects this SwissMatch::Communities should contain



14
15
16
17
# File 'lib/swissmatch/communities.rb', line 14

def initialize(communities)
  @communities          = communities
  reset!
end

Instance Method Details

#[](key) ⇒ SwissMatch::Community

Returns The community with the given name or community number.

Returns:



97
98
99
# File 'lib/swissmatch/communities.rb', line 97

def [](key)
  @by_name[key] || @by_community_number[key.to_i]
end

#by_community_number(number) ⇒ SwissMatch::Community

Returns The community with the given community number (also known as BFSNR).

Returns:



103
104
105
# File 'lib/swissmatch/communities.rb', line 103

def by_community_number(number)
  @by_community_number[number]
end

#by_community_numbers(*numbers) ⇒ Array<SwissMatch::Community>

Returns The communities with the given community numbers (also known as BFSNR).

Returns:

  • (Array<SwissMatch::Community>)

    The communities with the given community numbers (also known as BFSNR).



109
110
111
# File 'lib/swissmatch/communities.rb', line 109

def by_community_numbers(*numbers)
  @by_community_number.values_at(*numbers)
end

#by_name(name) ⇒ SwissMatch::Community

Returns The community with the given name.

Returns:



115
116
117
# File 'lib/swissmatch/communities.rb', line 115

def by_name(name)
  @by_name[name]
end

#each {|community| ... } ⇒ self

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.

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self



47
48
49
50
# File 'lib/swissmatch/communities.rb', line 47

def each(&block)
  @communities.each(&block)
  self
end

#inspectObject

See Also:

  • Object#inspect


132
133
134
# File 'lib/swissmatch/communities.rb', line 132

def inspect
  sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size
end

#reject(*args, &block) ⇒ SwissMatch::Communities

A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block returned false (or a falseish value)

Returns:

  • (SwissMatch::Communities)

    A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block returned false (or a falseish value)



75
76
77
# File 'lib/swissmatch/communities.rb', line 75

def reject(*args, &block)
  Communities.new(@communities.reject(*args, &block))
end

#reset!Object

Reinitialize all caching instance variables



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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 {|community| ... } ⇒ self

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.

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self



60
61
62
63
# File 'lib/swissmatch/communities.rb', line 60

def reverse_each(&block)
  @communities.reverse_each(&block)
  self
end

#select(*args, &block) ⇒ SwissMatch::Communities

A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block returned true (or a trueish value)

Returns:

  • (SwissMatch::Communities)

    A SwissMatch::Communities collection with all SwissMatch::Community objects for which the block returned true (or a trueish value)



68
69
70
# File 'lib/swissmatch/communities.rb', line 68

def select(*args, &block)
  Communities.new(@communities.select(*args, &block))
end

#sizeInteger

Returns The number of SwissMatch::Community objects in this collection.

Returns:

  • (Integer)

    The number of SwissMatch::Community objects in this collection.



120
121
122
# File 'lib/swissmatch/communities.rb', line 120

def size
  @communities.size
end

#sort(*args, &block) ⇒ SwissMatch::Communities

Returns A SwissMatch::Communities collection sorted by the block.

Returns:

See Also:

  • Enumerable#sort


83
84
85
# File 'lib/swissmatch/communities.rb', line 83

def sort(*args, &block)
  Communities.new(@communities.sort(*args, &block))
end

#sort_by(*args, &block) ⇒ SwissMatch::Communities

Returns A SwissMatch::Communities collection sorted by the block.

Returns:

See Also:

  • Enumerable#sort_by


91
92
93
# File 'lib/swissmatch/communities.rb', line 91

def sort_by(*args, &block)
  Communities.new(@communities.sort_by(*args, &block))
end

#to_aArray<SwissMatch::Community>

Returns An Array with all SwissMatch::Community objects in this SwissMatch::Communities.

Returns:

  • (Array<SwissMatch::Community>)

    An Array with all SwissMatch::Community objects in this SwissMatch::Communities.



126
127
128
# File 'lib/swissmatch/communities.rb', line 126

def to_a
  @communities.dup
end