Module: Cldr::Format

Defined in:
lib/cldr/format.rb,
lib/cldr/format/date.rb,
lib/cldr/format/time.rb,
lib/cldr/format/decimal.rb,
lib/cldr/format/percent.rb,
lib/cldr/format/currency.rb,
lib/cldr/format/datetime.rb,
lib/cldr/format/decimal/base.rb,
lib/cldr/format/datetime/base.rb,
lib/cldr/format/decimal/number.rb,
lib/cldr/format/decimal/integer.rb,
lib/cldr/format/decimal/fraction.rb

Defined Under Namespace

Classes: Currency, Date, Datetime, Decimal, Percent, Time

Instance Method Summary collapse

Instance Method Details

#format(locale, object, options = {}) ⇒ Object



17
18
19
20
21
22
# File 'lib/cldr/format.rb', line 17

def format(locale, object, options = {})
  type = options.has_key?(:currency) ? :currency : options.delete(:as)
  type ||= guess_type(object)

  send(:"format_#{type}", locale, object, options)
end

#format_currency(locale, number, options = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/cldr/format.rb', line 34

def format_currency(locale, number, options = {})
  if options[:currency].is_a?(Symbol)
    options.merge!(:currency => lookup_currency(locale, options[:currency], number))
  end
  formatter(locale, :currency, options.delete(:format)).apply(number, options)
end

#format_date(locale, date, options = {}) ⇒ Object



45
46
47
# File 'lib/cldr/format.rb', line 45

def format_date(locale, date, options = {})
  formatter(locale, :date, options.delete(:format)).apply(date, options)
end

#format_datetime(locale, datetime, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/cldr/format.rb', line 53

def format_datetime(locale, datetime, options = {})
  format = options.delete(:format) || :default
  (@formatters ||= {})[:"#{locale}.datetime.#{format}"] ||= begin
    date = formatter(locale, :date, options.delete(:date_format) || format)
    time = formatter(locale, :time, options.delete(:time_format) || format)
    format = lookup_format(locale, :datetime, format)
    Cldr::Format::Datetime.new(format, date, time).apply(datetime, options)
  end
end

#format_decimal(locale, number, options = {}) ⇒ Object Also known as: format_number



24
25
26
# File 'lib/cldr/format.rb', line 24

def format_decimal(locale, number, options = {})
  formatter(locale, :decimal, options.delete(:format)).apply(number, options)
end

#format_integer(locale, number, options = {}) ⇒ Object Also known as: format_int



29
30
31
# File 'lib/cldr/format.rb', line 29

def format_integer(locale, number, options = {})
  format_number(number, options.merge(:precision => 0))
end

#format_percent(locale, number, options = {}) ⇒ Object



41
42
43
# File 'lib/cldr/format.rb', line 41

def format_percent(locale, number, options = {})
  formatter(locale, :percent, options.delete(:format)).apply(number, options)
end

#format_time(locale, time, options = {}) ⇒ Object



49
50
51
# File 'lib/cldr/format.rb', line 49

def format_time(locale, time, options = {})
  formatter(locale, :time, options.delete(:format)).apply(time, options)
end