Class: SwissMatch::Districts
- Inherits:
-
Object
- Object
- SwissMatch::Districts
- Includes:
- Enumerable
- Defined in:
- lib/swissmatch/districts.rb
Overview
Represents a collection of swiss districts and provides a query interface.
Instance Method Summary collapse
-
#[](district_number_or_name) ⇒ SwissMatch::District
The district with the given district_number or name.
-
#by_district_number(tag) ⇒ SwissMatch::District
The district with the given license tag.
-
#by_name(name) ⇒ SwissMatch::District
The district with the given name (any language).
-
#each {|district| ... } ⇒ self
Calls the block once for every SwissMatch::District in this SwissMatch::Districts instance, passing that district as a parameter.
-
#initialize(districts) ⇒ Districts
constructor
A new instance of Districts.
- #inspect ⇒ Object
-
#reject(*args, &block) ⇒ SwissMatch::Districts
A SwissMatch::Districts collection with all SwissMatch::District objects for which the block returned false (or a falseish value).
-
#reverse_each {|district| ... } ⇒ self
Calls the block once for every SwissMatch::District in this SwissMatch::Districts instance, passing that district as a parameter.
-
#select(*args, &block) ⇒ SwissMatch::Districts
A SwissMatch::Districts collection with all SwissMatch::District objects for which the block returned true (or a trueish value).
-
#size ⇒ Integer
The number of SwissMatch::District objects in this collection.
-
#sort(*args, &block) ⇒ SwissMatch::Districts
A SwissMatch::Districts collection sorted by the block.
-
#sort_by(*args, &block) ⇒ SwissMatch::Districts
A SwissMatch::Districts collection sorted by the block.
-
#to_a ⇒ Array<SwissMatch::District>
An Array with all SwissMatch::District objects in this SwissMatch::Districts.
Methods included from Enumerable
Constructor Details
#initialize(districts) ⇒ Districts
Returns a new instance of Districts.
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.
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.
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).
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.
36 37 38 39 |
# File 'lib/swissmatch/districts.rb', line 36 def each(&block) @districts.each(&block) self end |
#inspect ⇒ Object
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)
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.
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)
57 58 59 |
# File 'lib/swissmatch/districts.rb', line 57 def select(*args, &block) Districts.new(@districts.select(*args, &block)) end |
#size ⇒ Integer
Returns 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.
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.
80 81 82 |
# File 'lib/swissmatch/districts.rb', line 80 def sort_by(*args, &block) Districts.new(@districts.sort_by(*args, &block)) end |
#to_a ⇒ Array<SwissMatch::District>
Returns 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 |