Class: SwissMatch::Cantons

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

Overview

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

Instance Method Summary (collapse)

Methods included from Enumerable

#last

Constructor Details

- (Cantons) initialize(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

- (SwissMatch::Canton) [](key)



58
59
60
# File 'lib/swissmatch/cantons.rb', line 58

def [](key)
  @by_license_tag[key] || @by_name[key]
end

- (SwissMatch::Canton) by_license_tag(tag)



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

def by_license_tag(tag)
  @by_license_tag[tag]
end

- (SwissMatch::Canton) by_name(name)



70
71
72
# File 'lib/swissmatch/cantons.rb', line 70

def by_name(name)
  @by_name[name]
end

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

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.

Yields:

Yield Parameters:



38
39
40
41
# File 'lib/swissmatch/cantons.rb', line 38

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

- (Object) inspect

See Also:

  • Object#inspect


87
88
89
# File 'lib/swissmatch/cantons.rb', line 87

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

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

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.

Yields:

Yield Parameters:



51
52
53
54
# File 'lib/swissmatch/cantons.rb', line 51

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

- (Integer) size



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

def size
  @cantons.size
end

- (Array<SwissMatch::Canton>) to_a



81
82
83
# File 'lib/swissmatch/cantons.rb', line 81

def to_a
  @cantons.dup
end