Module: Sinatra::Helpers::Country

Defined in:
lib/sinatra/helpers/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::Helpers::Country["US"] == "United States"
# => true

Sinatra::Helpers::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.



283
284
285
# File 'lib/sinatra/helpers/country.rb', line 283

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.



296
297
298
# File 'lib/sinatra/helpers/country.rb', line 296

def all
  @all
end

.randomSymbol

For use with seeding dummy data.

Returns:

  • (Symbol)

    a randomized country code.



289
290
291
# File 'lib/sinatra/helpers/country.rb', line 289

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:



267
268
269
# File 'lib/sinatra/helpers/country.rb', line 267

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