lib/icu_tournament/federation.rb in icu_tournament-1.2.2 vs lib/icu_tournament/federation.rb in icu_tournament-1.2.3

- old
+ new

@@ -54,11 +54,11 @@ # # To supply an extra "None" item at the top, specify its label with the _:none_ option: # # ICU::Federation.menu(:none => 'None') # => [['None', ''], ['Afghanistan', 'AFG], ...] # - # The "None" option's code is the empty string and it come above the "top" option if both are specified. + # The "None" option's code is the empty string and it comes above the "top" option if both are specified. # class Federation attr_reader :code, :name private_class_method :new @@ -66,11 +66,11 @@ # If there is no match or more than one match, _nil_ is returned. def self.find(str=nil) return nil unless str str = str.to_s return nil if str.length < 3 - compile unless @@objects + compile str = str.strip.squeeze(' ').downcase return @@codes[str] if str.length == 3 return @@names[str] if @@names[str] matches = Array.new @@names.each_key do |name| @@ -80,16 +80,21 @@ return nil unless matches.length == 1 matches[0] end def self.menu(opts = {}) - compile unless @@objects; + compile top, menu = nil, [] @@objects.each {|o| opts[:top] == o.code ? top = [o.name, o.code] : menu.push([o.name, o.code]) } opts[:order] == 'code' ? menu.sort!{|a,b| a.last <=> b.last} : menu.sort!{|a,b| a.first <=> b.first} menu.unshift(top) if top menu.unshift([opts[:none], '']) if opts[:none] menu + end + + def self.codes + compile + @@objects.map(&:code).sort end def initialize(code, name) # :nodoc: because new is private @code = code @name = name