Class: Maguire::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/maguire/locale.rb

Defined Under Namespace

Classes: NotSupportedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, locale_data = {}) ⇒ Locale

Returns a new instance of Locale.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/maguire/locale.rb', line 9

def initialize(locale, locale_data={})
  @locale = locale

  layouts = locale_data[:layouts]
  @positive_formatting = parse_layout(layouts[:positive])
  @negative_formatting = parse_layout(layouts[:negative])
  if layouts[:zero]
    @zero_formatting = parse_zero_layout(layouts[:zero])
  end

  @currency_overlays = locale_data
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/maguire/locale.rb', line 7

def code
  @code
end

#localeObject (readonly)

Returns the value of attribute locale.



7
8
9
# File 'lib/maguire/locale.rb', line 7

def locale
  @locale
end

#symbolObject (readonly)

Returns the value of attribute symbol.



7
8
9
# File 'lib/maguire/locale.rb', line 7

def symbol
  @symbol
end

Class Method Details

.lookup(options) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/maguire/locale.rb', line 205

def lookup(options)
  locale = "#{options[:lang].downcase}-#{options[:country].upcase}"
  data = Maguire.locale_paths.load(locale)
  if data.nil?
    raise Locale::NotSupportedError.new("The locale #{locale} isn't supported")
  else
    self.new(locale, data)
  end
end

Instance Method Details

#as_jsonObject



85
86
87
88
89
90
91
92
# File 'lib/maguire/locale.rb', line 85

def as_json
  {
    id: @locale,
    positive: @positive_formatting,
    negative: @negative_formatting,
    zero: @zero_formatting
  }
end

#format(value, currency_code, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/maguire/locale.rb', line 37

def format(value, currency_code, options={})
  currency = localized_currency(currency_code)

  major_value = value.abs / currency.precision
  minor_value = round(value.abs - major_value * currency.precision)

  formatting = value >= 0 ?
    @positive_formatting : @negative_formatting

  symbol = currency.symbol
  if options[:html] && currency.symbol_html
    symbol = currency.symbol_html
  end

  strip_insignificant_zeros = options[:strip_insignificant_zeros]
  if options[:no_minor_units] || currency.minor_units == 0
    minor_value = 0
    strip_insignificant_zeros = true
  end

  if major_value == 0 && minor_value == 0 && options[:free]
    return options[:free]
  end

  if strip_insignificant_zeros && minor_value == 0
    minor_value = ""
    decimal_symbol = ""
  else
    decimal_symbol = formatting[:decimal_symbol]

    if minor_value == 0 && @zero_formatting
      formatting = @zero_formatting
    else
      minor_value = minor_value.to_s.rjust(currency.minor_units, "0")
    end
  end

  groups = split_value_into_groups(major_value, formatting[:digit_grouping_style])

  formatting[:layout] % {
    symbol: symbol,
    code: currency.code,
    decimal: decimal_symbol,
    major_value: groups.join(formatting[:digit_grouping_symbol]),
    minor_value: minor_value
  }
end

#inspectObject



22
23
24
# File 'lib/maguire/locale.rb', line 22

def inspect
  "<##{self.class} locale=#{locale}>"
end

#localized_currency(currency_code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/maguire/locale.rb', line 26

def localized_currency(currency_code)
  currency_code.downcase!
  currency = Currency.coded(currency_code)
  overlay = @currency_overlays[currency_code.to_sym]
  if overlay
    currency.overlay(overlay)
  else
    currency
  end
end