Class: Maguire::Locale
- Inherits:
-
Object
- Object
- Maguire::Locale
- Defined in:
- lib/maguire/locale.rb
Defined Under Namespace
Classes: NotSupportedError
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #format(value, currency_code, options = {}) ⇒ Object
-
#initialize(locale, locale_data = {}) ⇒ Locale
constructor
A new instance of Locale.
- #inspect ⇒ Object
- #localized_currency(currency_code) ⇒ Object
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/maguire/locale.rb', line 7 def code @code end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
7 8 9 |
# File 'lib/maguire/locale.rb', line 7 def locale @locale end |
#symbol ⇒ Object (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() locale = "#{[:lang].downcase}-#{[: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_json ⇒ Object
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, ={}) 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 [:html] && currency.symbol_html symbol = currency.symbol_html end strip_insignificant_zeros = [:strip_insignificant_zeros] if [:no_minor_units] || currency.minor_units == 0 minor_value = 0 strip_insignificant_zeros = true end if major_value == 0 && minor_value == 0 && [:free] return [: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 |
#inspect ⇒ Object
22 23 24 |
# File 'lib/maguire/locale.rb', line 22 def inspect "<##{self.class} locale=#{locale}>" end |