Class: SwissMatch::Districts

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

Overview

Represents a collection of swiss districts and provides a query interface.

Instance Method Summary collapse

Methods included from Enumerable

#last

Constructor Details

#initialize(districts) ⇒ Districts

Returns a new instance of Districts.

Parameters:

  • districts (Array<SwissMatch::District>)

    The SwissMatch::District objects this SwissMatch::Districts should contain


17
18
19
20
21
22
23
24
25
26
# File 'lib/swissmatch/districts.rb', line 17

def initialize(districts)
  @districts          = districts
  @by_district_number = {}
  @by_name            = {}

  districts.each do |district|
    @by_district_number[district.district_number] = district
    @by_name[district.name]                       = district
  end
end

Instance Method Details

#[](district_number_or_name) ⇒ SwissMatch::District

Returns The district with the given district_number or name.

Returns:


86
87
88
# File 'lib/swissmatch/districts.rb', line 86

def [](district_number_or_name)
  @by_district_number[district_number_or_name] || @by_name[district_number_or_name]
end

#by_district_number(tag) ⇒ SwissMatch::District

Returns The district with the given license tag.

Returns:


92
93
94
# File 'lib/swissmatch/districts.rb', line 92

def by_district_number(tag)
  @by_district_number[tag]
end

#by_name(name) ⇒ SwissMatch::District

Returns The district with the given name (any language).

Returns:


98
99
100
# File 'lib/swissmatch/districts.rb', line 98

def by_name(name)
  @by_name[name]
end

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

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

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self


36
37
38
39
# File 'lib/swissmatch/districts.rb', line 36

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

#inspectObject

See Also:

  • Object#inspect

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

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

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

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

Returns:

  • (SwissMatch::Districts)

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


64
65
66
# File 'lib/swissmatch/districts.rb', line 64

def reject(*args, &block)
  Districts.new(@districts.reject(*args, &block))
end

#reverse_each {|district| ... } ⇒ self

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

Yields:

Yield Parameters:

Returns:

  • (self)

    Returns self


49
50
51
52
# File 'lib/swissmatch/districts.rb', line 49

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

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

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

Returns:

  • (SwissMatch::Districts)

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


57
58
59
# File 'lib/swissmatch/districts.rb', line 57

def select(*args, &block)
  Districts.new(@districts.select(*args, &block))
end

#sizeInteger

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

Returns:

  • (Integer)

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


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

def size
  @districts.size
end

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

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

Returns:

See Also:

  • Enumerable#sort

72
73
74
# File 'lib/swissmatch/districts.rb', line 72

def sort(*args, &block)
  Districts.new(@districts.sort(*args, &block))
end

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

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

Returns:

See Also:

  • Enumerable#sort_by

80
81
82
# File 'lib/swissmatch/districts.rb', line 80

def sort_by(*args, &block)
  Districts.new(@districts.sort_by(*args, &block))
end

#to_aArray<SwissMatch::District>

Returns An Array with all SwissMatch::District objects in this SwissMatch::Districts.

Returns:

  • (Array<SwissMatch::District>)

    An Array with all SwissMatch::District objects in this SwissMatch::Districts.


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

def to_a
  @districts.dup
end