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

+ (String?) [](code)

Retrieves the country name given a country code.

Examples:

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

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


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

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

+ (Hash) all

Gives all countries in a Hash.



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

def all
  @all
end

+ (Symbol) random

For use with seeding dummy data.



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

def random
  all.keys.shuffle.first
end

+ (Array) to_select

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"]]

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