Class: SwissMatch::Cantons
- Inherits:
-
Object
- Object
- SwissMatch::Cantons
- Includes:
- Enumerable
- Defined in:
- lib/swissmatch/cantons.rb
Overview
Represents a collection of swiss cantons and provides a query interface.
Instance Method Summary collapse
-
#[](key) ⇒ SwissMatch::Canton
The canton with the given license tag or name (in any language).
-
#by_license_tag(tag) ⇒ SwissMatch::Canton
The canton with the given license tag.
-
#by_name(name) ⇒ SwissMatch::Canton
The canton with the given name (any language).
-
#each {|canton| ... } ⇒ self
Calls the block once for every SwissMatch::Canton in this SwissMatch::Cantons instance, passing that canton as a parameter.
-
#initialize(cantons) ⇒ Cantons
constructor
A new instance of Cantons.
- #inspect ⇒ Object
-
#reject(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection with all SwissMatch::Canton objects for which the block returned false (or a falseish value).
-
#reverse_each {|canton| ... } ⇒ self
Calls the block once for every SwissMatch::Canton in this SwissMatch::Cantons instance, passing that canton as a parameter.
-
#select(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection with all SwissMatch::Canton objects for which the block returned true (or a trueish value).
-
#size ⇒ Integer
The number of SwissMatch::Canton objects in this collection.
-
#sort(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection sorted by the block.
-
#sort_by(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection sorted by the block.
-
#to_a ⇒ Array<SwissMatch::Canton>
An Array with all SwissMatch::Canton objects in this SwissMatch::Cantons.
Methods included from Enumerable
Constructor Details
#initialize(cantons) ⇒ Cantons
Returns a new instance of Cantons.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/swissmatch/cantons.rb', line 17 def initialize(cantons) @cantons = cantons @by_license_tag = {} @by_name = {} cantons.each do |canton| @by_license_tag[canton.license_tag] = canton canton.names.each do |name| @by_name[name] = canton end end end |
Instance Method Details
#[](key) ⇒ SwissMatch::Canton
Returns The canton with the given license tag or name (in any language).
88 89 90 |
# File 'lib/swissmatch/cantons.rb', line 88 def [](key) @by_license_tag[key] || @by_name[key] end |
#by_license_tag(tag) ⇒ SwissMatch::Canton
Returns The canton with the given license tag.
94 95 96 |
# File 'lib/swissmatch/cantons.rb', line 94 def by_license_tag(tag) @by_license_tag[tag] end |
#by_name(name) ⇒ SwissMatch::Canton
Returns The canton with the given name (any language).
100 101 102 |
# File 'lib/swissmatch/cantons.rb', line 100 def by_name(name) @by_name[name] end |
#each {|canton| ... } ⇒ self
Calls the block once for every SwissMatch::Canton in this SwissMatch::Cantons instance, passing that canton as a parameter. The order is the same as the instance was constructed.
38 39 40 41 |
# File 'lib/swissmatch/cantons.rb', line 38 def each(&block) @cantons.each(&block) self end |
#inspect ⇒ Object
117 118 119 |
# File 'lib/swissmatch/cantons.rb', line 117 def inspect sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size end |
#reject(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection with all SwissMatch::Canton objects for which the block returned false (or a falseish value)
66 67 68 |
# File 'lib/swissmatch/cantons.rb', line 66 def reject(*args, &block) Cantons.new(@cantons.reject(*args, &block)) end |
#reverse_each {|canton| ... } ⇒ self
Calls the block once for every SwissMatch::Canton in this SwissMatch::Cantons instance, passing that canton as a parameter. The order is the reverse of what the instance was constructed.
51 52 53 54 |
# File 'lib/swissmatch/cantons.rb', line 51 def reverse_each(&block) @cantons.reverse_each(&block) self end |
#select(*args, &block) ⇒ SwissMatch::Cantons
A SwissMatch::Cantons collection with all SwissMatch::Canton objects for which the block returned true (or a trueish value)
59 60 61 |
# File 'lib/swissmatch/cantons.rb', line 59 def select(*args, &block) Cantons.new(@cantons.select(*args, &block)) end |
#size ⇒ Integer
Returns The number of SwissMatch::Canton objects in this collection.
105 106 107 |
# File 'lib/swissmatch/cantons.rb', line 105 def size @cantons.size end |
#sort(*args, &block) ⇒ SwissMatch::Cantons
Returns A SwissMatch::Cantons collection sorted by the block.
74 75 76 |
# File 'lib/swissmatch/cantons.rb', line 74 def sort(*args, &block) Cantons.new(@cantons.sort(*args, &block)) end |
#sort_by(*args, &block) ⇒ SwissMatch::Cantons
Returns A SwissMatch::Cantons collection sorted by the block.
82 83 84 |
# File 'lib/swissmatch/cantons.rb', line 82 def sort_by(*args, &block) Cantons.new(@cantons.sort_by(*args, &block)) end |
#to_a ⇒ Array<SwissMatch::Canton>
Returns An Array with all SwissMatch::Canton objects in this SwissMatch::Cantons.
111 112 113 |
# File 'lib/swissmatch/cantons.rb', line 111 def to_a @cantons.dup end |