maguire 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,7 +12,7 @@ Maguire.locale_paths << "path/to/my/locales"
12
12
 
13
13
  If you're adding a customization for French-speaking Canadians, the JSON file should be named `fr_CA.json`.
14
14
 
15
- The file should *at least* have a layouts hash for providing information on how to format the currencies. The layout format should always be in US Dollars (using the code `USD`, and symbol `$`) and use the monetary value of `1234567890.12`.
15
+ The file should *at least* have a layouts hash for providing information on how to format the currencies. The layout format should always be in US Dollars (using the code `USD`, and symbol `$`) and use the monetary value of `1234567890.12`. In addition, there's an optional layout if there's a custom format when there's no minor units (cf. '$1.-"). The monetary value for the zero layout is `1.00`.
16
16
 
17
17
  So a layout for use in Quebec might be:
18
18
 
@@ -20,7 +20,7 @@ So a layout for use in Quebec might be:
20
20
  {
21
21
  "layouts": {
22
22
  "positive": "1.234.567.890,12 $",
23
- "negative": "-1.234.567.890,12"
23
+ "negative": "-1.234.567.890,12 $"
24
24
  }
25
25
  }
26
26
  ```
@@ -54,6 +54,6 @@ A file for the Bitcoin currency would look like:
54
54
  After doing this, you can format your Bitcoins:
55
55
 
56
56
  ```ruby
57
- Maguire.format({ value: 400_000_00000000, currency: "BTC" })
57
+ Maguire.format({ value: 400_000_00000000, currency: "BTC" }, { strip_insignificant_zeros: true })
58
58
  # ฿400,000
59
59
  ```
@@ -9,8 +9,12 @@ module Maguire
9
9
  def initialize(locale, locale_data={})
10
10
  @locale = locale
11
11
 
12
- @positive_formatting = parse_layout(locale_data[:layouts][:positive])
13
- @negative_formatting = parse_layout(locale_data[:layouts][:negative])
12
+ layouts = locale_data[:layouts]
13
+ @positive_formatting = parse_layout(layouts[:positive])
14
+ @negative_formatting = parse_layout(layouts[:negative])
15
+ if layouts[:zero]
16
+ @zero_formatting = parse_zero_layout(layouts[:zero])
17
+ end
14
18
 
15
19
  @currency_overlays = locale_data
16
20
  end
@@ -46,8 +50,13 @@ module Maguire
46
50
  minor_value = ""
47
51
  decimal_symbol = ""
48
52
  else
49
- minor_value = minor_value.to_s.rjust(currency.minor_units, "0")
50
53
  decimal_symbol = formatting[:decimal_symbol]
54
+
55
+ if minor_value == 0 && @zero_formatting
56
+ formatting = @zero_formatting
57
+ else
58
+ minor_value = minor_value.to_s.rjust(currency.minor_units, "0")
59
+ end
51
60
  end
52
61
 
53
62
  formatting[:layout] % {
@@ -115,6 +124,22 @@ module Maguire
115
124
  }
116
125
  end
117
126
 
127
+ def parse_zero_layout(layout)
128
+ layout = layout.dup
129
+
130
+ layout.gsub!("USD", "%{code}")
131
+ layout.gsub!("$", "%{symbol}")
132
+ layout.gsub!("1", "%{major_value}")
133
+ decimal_symbol = @positive_formatting[:decimal_symbol]
134
+ layout.gsub!(decimal_symbol, "%{decimal}")
135
+
136
+ {
137
+ layout: layout,
138
+ decimal_symbol: decimal_symbol,
139
+ digit_grouping_symbol: @positive_formatting[:digit_grouping_symbol]
140
+ }
141
+ end
142
+
118
143
  def round(value)
119
144
  value.to_i
120
145
  end
@@ -1,5 +1,4 @@
1
1
  require 'json'
2
- require 'pry'
3
2
 
4
3
  module Maguire
5
4
  class Locale
@@ -10,8 +9,12 @@ module Maguire
10
9
  def initialize(locale, locale_data={})
11
10
  @locale = locale
12
11
 
13
- @positive_formatting = parse_layout(locale_data[:layouts][:positive])
14
- @negative_formatting = parse_layout(locale_data[:layouts][:negative])
12
+ layouts = locale_data[:layouts]
13
+ @positive_formatting = parse_layout(layouts[:positive])
14
+ @negative_formatting = parse_layout(layouts[:negative])
15
+ if layouts[:zero]
16
+ @zero_formatting = parse_zero_layout([:zero])
17
+ end
15
18
 
16
19
  @currency_overlays = locale_data
17
20
  end
@@ -32,11 +35,35 @@ module Maguire
32
35
  formatting = value >= 0 ?
33
36
  @positive_formatting : @negative_formatting
34
37
 
38
+ symbol = currency.symbol
39
+ if options[:html] && currency.symbol_html
40
+ symbol = currency.symbol_html
41
+ end
42
+
43
+ strip_insignificant_zeros = options[:strip_insignificant_zeros]
44
+ if options[:no_minor_units] || currency.minor_units == 0
45
+ minor_value = 0
46
+ strip_insignificant_zeros = true
47
+ end
48
+
49
+ if strip_insignificant_zeros && minor_value == 0
50
+ minor_value = ""
51
+ decimal_symbol = ""
52
+ else
53
+ minor_value = minor_value.to_s.rjust(currency.minor_units, "0")
54
+ decimal_symbol = formatting[:decimal_symbol]
55
+
56
+ if minor_value == 0 && @zero_formatting
57
+ formatting = @zero_formatting
58
+ end
59
+ end
60
+
35
61
  formatting[:layout] % {
36
- symbol: currency.symbol,
62
+ symbol: symbol,
37
63
  code: currency.code,
64
+ decimal: decimal_symbol,
38
65
  major_value: groups.join(formatting[:digit_grouping_symbol]),
39
- minor_value: minor_value.to_s.rjust(currency.minor_units, "0")
66
+ minor_value: minor_value
40
67
  }
41
68
  end
42
69
 
@@ -46,7 +73,7 @@ module Maguire
46
73
  GROUPS_OF_FOUR_RE = /([0-9]{4}[^0-9])+/
47
74
  GROUPS_OF_THREE_RE = /([0-9]{3}[^0-9]){3}+/
48
75
 
49
- def parse_south_asian_grouping(layout)
76
+ def parse_groups_in_south_asian_style(layout)
50
77
  @group_numbers_in_south_asian_style = true
51
78
  digit_grouping_symbol = layout.match(/1([^0-9]*)23/)[1]
52
79
  layout.gsub!(["1", "23", "45", "67", "890"].join(digit_grouping_symbol), "%{major_value}")
@@ -76,7 +103,7 @@ module Maguire
76
103
 
77
104
  digit_grouping_symbol =
78
105
  if layout =~ SOUTH_ASIAN_GROUPING_RE
79
- parse_south_asian_style(layout)
106
+ parse_groups_in_south_asian_style(layout)
80
107
  elsif layout =~ GROUPS_OF_FOUR_RE
81
108
  parse_groups_of_four(layout)
82
109
  elsif layout =~ GROUPS_OF_THREE_RE
@@ -86,19 +113,40 @@ module Maguire
86
113
  layout.gsub!("USD", "%{code}")
87
114
  layout.gsub!("$", "%{symbol}")
88
115
  layout.gsub!("12", "%{minor_value}")
116
+ decimal_symbol = layout.match(/major_value}(.*)%{minor_value/)[1]
117
+ layout.gsub!(decimal_symbol, "%{decimal}")
89
118
 
90
119
  {
91
120
  layout: layout,
121
+ decimal_symbol: decimal_symbol,
92
122
  digit_grouping_symbol: digit_grouping_symbol
93
123
  }
94
124
  end
95
125
 
126
+ def parse_zero_layout(layout)
127
+ layout = layout.dup
128
+
129
+ layout.gsub!("USD", "%{code}")
130
+ layout.gsub!("$", "%{symbol}")
131
+ layout.gsub!("1", "%{major_value}")
132
+ decimal_symbol = @positive_layout[:decimal_symbol]
133
+ layout.gsub!(decimal_symbol, "%{decimal}")
134
+
135
+ {
136
+ layout: layout,
137
+ decimal_symbol: decimal_symbol,
138
+ digit_grouping_symbol: @positive_layout[:digit_grouping_symbol]
139
+ }
140
+ end
141
+
96
142
  def round(value)
97
143
  value.to_i
98
144
  end
99
145
 
100
146
  def break_off(value, number)
101
- [value.slice(0, value.length - number), value.slice(value.length - number, value.length)]
147
+ len = value.length
148
+ number = len if number > len
149
+ [value.slice(0, len - number), value.slice(len - number, len)]
102
150
  end
103
151
 
104
152
  def split_value_into_groups_of(value, number)
@@ -1,3 +1,3 @@
1
1
  module Maguire
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module Maguire
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maguire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-10 00:00:00.000000000 Z
12
+ date: 2014-04-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri