Class: Mack::Localization::FormatEngineRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack-localization/format_engine/engine_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeFormatEngineRegistry

Returns a new instance of FormatEngineRegistry.



6
7
8
9
10
11
12
# File 'lib/mack-localization/format_engine/engine_registry.rb', line 6

def initialize
  # { 
  #    :en => {:date => {...}, :num_currency => {...}},
  #    :fr => ...
  # }
  @reg = {}
end

Instance Method Details

#get_engine(lang, type) ⇒ Object

Retrieve a registered engine

params:

lang - the language
type - either l10n_formatter.date_format_registry_key or l10n_formatter.currency_format_registry_key

returns the registered engine, or nil if no engine is registered for that type/lang



42
43
44
45
46
47
48
# File 'lib/mack-localization/format_engine/engine_registry.rb', line 42

def get_engine(lang, type)
  lang = lang.to_sym
  type = type.to_sym
  type_reg = @reg[lang]
  return nil if type_reg.nil?
  return type_reg[type]
end

#register(lang, type, obj, overwrite = false) ⇒ Object

Register a format engine

params:

lang - the language of the engine
type - either l10n_formatter.date_format_registry_key or l10n_formatter.currency_format_registry_key
overwrite - whether or not to overwrite the formatter if it's already registered. default == false


22
23
24
25
26
27
28
29
30
31
# File 'lib/mack-localization/format_engine/engine_registry.rb', line 22

def register(lang, type, obj, overwrite = false)
  lang = lang.to_sym
  type = type.to_sym
  if @reg[lang].nil?
    @reg[lang] = {type => obj}
  else
    type_reg = @reg[lang]
    type_reg[type] = obj if type_reg[type].nil? or (overwrite and !type_reg[type].nil?)
  end
end