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

- (Communities) initialize(communities)



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

def initialize(communities)
  @all                  = communities
  @by_community_number  = Hash[communities.map { |c| [c.community_number, c] }]
  @by_name              = communities.group_by(&:name)
end

Instance Method Details

- (Array<SwissMatch::Community>, SwissMatch::Community) [](key)



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

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

- (SwissMatch::Community) by_community_number(number)



54
55
56
# File 'lib/swissmatch/communities.rb', line 54

def by_community_number(number)
  @by_community_number[number]
end

- (Array<SwissMatch::Community>) by_name(name)



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

def by_name(name)
  @by_name[name]
end

- (self) each {|community| ... }

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:



28
29
30
31
# File 'lib/swissmatch/communities.rb', line 28

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

- (Object) inspect

See Also:

  • Object#inspect


77
78
79
# File 'lib/swissmatch/communities.rb', line 77

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

- (self) reverse_each {|community| ... }

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:



41
42
43
44
# File 'lib/swissmatch/communities.rb', line 41

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

- (Integer) size



65
66
67
# File 'lib/swissmatch/communities.rb', line 65

def size
  @all.size
end

- (Array<SwissMatch::Community>) to_a



71
72
73
# File 'lib/swissmatch/communities.rb', line 71

def to_a
  @all.dup
end