formatting 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8a92f61e13526876dc609e99cabc25252e14303
4
- data.tar.gz: 804ee6b74f3f1521075dd601a8d90b169c2fa0f8
3
+ metadata.gz: 31a50e279bf40d7bd3e6a284ab518d258d0f1120
4
+ data.tar.gz: 66448a804ff7bc12a5dfaba0dbcfe9643221b66a
5
5
  SHA512:
6
- metadata.gz: c3d5b1625686c2dc865841a1800d2b971604f951086570c80961acf75a72154dcd0747c54b54a11380fcd45f0be29163510a971ec99f9346846300d4093322c7
7
- data.tar.gz: 591e74fcd72914b2f3afe535e13c0d31e6935b5930495f373ce1fd16194e158bd8204153b6cb7c5dc10992dfcd2f9e253814f6dca37c9afd91db888a33a42fb6
6
+ metadata.gz: f19832f4814387e8397459f2d7c038684832235987d7c2d05c8ddc00a66f76358adfc9d2052f605a5b280796f1adb4907e328d09deb5f671be7ca3fdea529862
7
+ data.tar.gz: e95ab8b878a6f1b081ee28fd261385aa13e7229019467ce9ea92bde2ae0b02ef3ac7d71bc1b0c8a9a53c3aa3abc7f7e843cce167fd47b931a840874b48baa0de
data/README.md CHANGED
@@ -99,7 +99,11 @@ Formatting.format_percent(12.3) # => "12.3%"
99
99
 
100
100
  #### Options
101
101
 
102
- Passes on all the number options.
102
+ Passes on all the number options and also takes these:
103
+
104
+ name | default | explanation
105
+ ----------------|---------------------------------------------------|------------
106
+ format | `"<number>%"` or `<number> %` depending on locale | A format string.
103
107
 
104
108
 
105
109
  ## Installation
@@ -73,7 +73,7 @@ module Formatting
73
73
  end
74
74
 
75
75
  def t_format(key, default)
76
- if defined?(I18n)
76
+ if defined?(I18n) && I18n.respond_to?(:t)
77
77
  I18n.t(key, scope: "number.format", default: default)
78
78
  else
79
79
  default
@@ -1,12 +1,26 @@
1
- # http://en.wikipedia.org/wiki/Percent_sign#Spacing
2
-
3
1
  require "attr_extras"
4
2
 
5
3
  module Formatting
6
4
  module Percent
7
5
  def format_percent(number, opts = {})
6
+ format_string = opts.fetch(:format) { default_percent_format_string }
8
7
  formatted_number = Formatting.format_number(number, opts)
9
- "#{formatted_number}%"
8
+ format_string.gsub("<number>", formatted_number)
9
+ end
10
+
11
+ private
12
+
13
+ # http://en.wikipedia.org/wiki/Percent_sign#Spacing
14
+ # Rails i18n doesn't have a conventional format string for this.
15
+ def default_percent_format_string
16
+ locale = defined?(I18n) && I18n.locale
17
+
18
+ case locale
19
+ when :sv, :fi, :fr, :de
20
+ "<number>#{Formatting::NON_BREAKING_SPACE}%"
21
+ else
22
+ "<number>%"
23
+ end
10
24
  end
11
25
  end
12
26
  end
@@ -1,3 +1,3 @@
1
1
  module Formatting
2
- VERSION = "0.0.16"
2
+ VERSION = "0.0.17"
3
3
  end
@@ -14,7 +14,22 @@ describe Formatting, ".format_percent" do
14
14
  end
15
15
 
16
16
  it "passes on number formatting options" do
17
- expect_formatted(1.567, round: 2).to include "1.57"
17
+ expect_formatted(1.567, round: 1).to include "1.6"
18
+ end
19
+
20
+ context "format string option" do
21
+ it "is used if provided" do
22
+ expect_formatted(1, format: "%<number>%").to eq "%1.00%"
23
+ end
24
+
25
+ context "if I18n.locale is available" do
26
+ let(:i18n) { stub_const("I18n", double) }
27
+
28
+ it "defaults sensibly for some locales that require spacing the sign" do
29
+ i18n.stub(locale: :sv)
30
+ expect_formatted(1).to eq space_to_nbsp("1.00 %")
31
+ end
32
+ end
18
33
  end
19
34
 
20
35
  def expect_formatted(number, opts = {})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh