Module: Sinatra::Country

Defined in:
lib/sinatra/support/country.rb

Overview

A simple module containing all countries as of 2010.

Class Method Summary collapse

Class Method Details

.[](code) ⇒ String?

Retrieves the country name given a country code.

Examples:

Sinatra::Country["US"] == "United States"
# => true

Sinatra::Country[:US] == "United States"
# => true

Parameters:

  • code (#to_sym)

    The country code in 2 letter all caps format.

Returns:

  • (String)

    The corresponding country name given the code.

  • (nil)

    nil if no matching country code.



282
283
284
# File 'lib/sinatra/support/country.rb', line 282

def [](code)
  all[code.to_sym]  if not code.to_s.empty?
end

.allHash

Gives all countries in a Hash.

Returns:

  • (Hash)

    the code => name pairs of all countries.



295
296
297
# File 'lib/sinatra/support/country.rb', line 295

def all
  @all
end

.randomSymbol

For use with seeding dummy data.

Returns:

  • (Symbol)

    a randomized country code.



288
289
290
# File 'lib/sinatra/support/country.rb', line 288

def random
  all.keys.shuffle.first
end

.to_selectArray

Returns a collection of pairs with the first element being country name and the last element being the code.

Examples:

p Country.to_select
[["Afghanistan", "AF"], ["Ă…land Islands", "AX"], ["Albania", "AL"], 
["Algeria", "DZ"], ["American Samoa", "AS"], ... ["Zimbabwe", "ZW"]]

Returns:

  • (Array)

    a collection of pairs with the first element being country name and the last element being the code.

See Also:

  • Sinatra#country_choices


266
267
268
# File 'lib/sinatra/support/country.rb', line 266

def to_select
  all.map { |code, name| [name, code.to_s] }
end