nutella 0.12 → 0.13

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: ef8876f7754fa5b0b422e9ab7ca092e9298ee436
4
- data.tar.gz: 63636995f8ba8417326f52026f07ff0c5fc33272
3
+ metadata.gz: e07f31e4f5a8e7dd0f25336e3980dadc7bd5f132
4
+ data.tar.gz: 586916ef3fd71c40f40fd0ebf339b2520cd8928d
5
5
  SHA512:
6
- metadata.gz: 77c51d26b2d0af83884a6e188993e5c4e58d3b4e71bd3bb59310660820c37082dbcdba45e31529c02c50c0346d6927ea09374c9aaa29c5e3d4b2e250e50ed68b
7
- data.tar.gz: bd3e50152263964033c4cf070c8d7c4c2208b2f32d319f56f47164026cf85d07aabace679a7816d7bd264c5acfb7e58d7d8a79ef3795c7d40611720295915ece
6
+ metadata.gz: 912c094376824a9e09dff1e203c37d5b3989364d29cae9606d07e09ccef0c717089f8a35a764b6701e36de5120d220bb510c49a0344a6af49c8da81130759a10
7
+ data.tar.gz: 8b5a3d9c7803bb86c8b8ee86817f7bfdf549235fd3f9c31e8fc30e4a4f00deaf17f549980dc26bd4e2a3c80a149402635fa2f2d7d1f87c5e991499664dfe0350
@@ -0,0 +1,37 @@
1
+ class Numeric
2
+ # Formats a number as a currency string.
3
+ #
4
+ # @param [Hash] opts the options hash
5
+ # @option opts [String] :sign the currency sign
6
+ # @option opts [Symbol] :position the position of the currency sign
7
+ # @option opts [Integer] :precision amount of decimal places to display
8
+ # @option opts [String] :separator the separator between the ones and tenths
9
+ # @option opts [String] :delimiter the thousands delimiter
10
+ # @option opts [Boolean] :pretty sets 0 precision for whole amounts
11
+ #
12
+ # @return [String] the number in a currency format
13
+ def to_currency(opts = {})
14
+ opts = {
15
+ sign: "$",
16
+ position: :left,
17
+ precision: 2,
18
+ separator: ".",
19
+ delimiter: ",",
20
+ pretty: false
21
+ }.merge(opts)
22
+
23
+ opts[:precision] = 0 if opts[:pretty] && (self - self.floor).round(3) < 0.005
24
+
25
+ # Must round it outside also as the %f formatter always rounds down
26
+ ("%.#{opts[:precision]}f" % self.round(opts[:precision])).
27
+ # Apply thousands delimiter
28
+ gsub(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{opts[:delimiter]}").
29
+ # Apply unit separator
30
+ gsub(/\.(\d{#{opts[:precision]}})$/, "#{opts[:separator]}\\1").
31
+ # Now put the sign on with #prepend or #concat depending on which side
32
+ send(
33
+ {left: :prepend, right: :concat}[opts[:position]],
34
+ opts[:sign] || ""
35
+ )
36
+ end
37
+ end
@@ -13,12 +13,8 @@ class String
13
13
  #
14
14
  # @return [String] the string with excess leading whitespace stripped
15
15
  def heredoc
16
- gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size || 0}}/, ""
16
+ gsub /^[ \t]{#{scan(/^[ \t]*(?=\S)/).min.size}}/, ""
17
17
  end
18
18
  alias format_heredoc heredoc
19
19
  alias strip_heredoc heredoc
20
-
21
- def in_heredoc indentation
22
- lines[1..-1].map { |line| line.prepend " " * indentation }.join "\n"
23
- end
24
20
  end
@@ -1,4 +1,4 @@
1
1
  module Nutella
2
2
  # The current version of Nutella.
3
- VERSION = "0.12"
3
+ VERSION = "0.13"
4
4
  end
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
 
7
7
  gem.author = "Vinny Diehl"
8
8
  gem.email = "vinny.diehl@gmail.com"
9
- gem.homepage = "https://github.com/gbchaosmaster/nutella"
9
+ gem.homepage = "https://github.com/vinnydiehl/nutella"
10
10
 
11
11
  gem.license = "MIT"
12
12
 
@@ -37,6 +37,60 @@ describe Numeric do
37
37
  end
38
38
  end
39
39
 
40
+ describe "#to_currency" do
41
+ context "when used with no options" do
42
+ it "returns a USD style string with 2 decimals precision" do
43
+ expect(5.to_currency).to eq("$5.00")
44
+ expect(5.25.to_currency).to eq("$5.25")
45
+ end
46
+
47
+ it "rounds properly" do
48
+ expect(5.254.to_currency).to eq("$5.25")
49
+ expect(5.255.to_currency).to eq("$5.26")
50
+ end
51
+
52
+ it "uses ',' as a thousands separator" do
53
+ expect(5000.to_currency).to eq("$5,000.00")
54
+ expect(5e6.to_currency).to eq("$5,000,000.00")
55
+ expect(5e5.to_currency).to eq("$500,000.00")
56
+ expect(5e4.to_currency).to eq("$50,000.00")
57
+ end
58
+ end
59
+
60
+ context "when using pretty mode" do
61
+ it "strips decimals from whole numbers" do
62
+ expect(1.to_currency pretty: true).to eq("$1")
63
+ expect(1.to_currency pretty: true).to eq("$1")
64
+ end
65
+
66
+ it "keeps decimals if there are any cents" do
67
+ expect(1.01.to_currency pretty: true).to eq("$1.01")
68
+ expect(1.20.to_currency pretty: true).to eq("$1.20")
69
+ expect(1.21.to_currency pretty: true).to eq("$1.21")
70
+ end
71
+
72
+ it "only checks cents out to the hundredths" do
73
+ expect(5.004.to_currency pretty: true).to eq("$5")
74
+ expect(5.005.to_currency pretty: true).to eq("$5.01")
75
+ end
76
+ end
77
+
78
+ it "has an adjustable sign" do
79
+ expect(5.to_currency sign: "£").to eq("£5.00")
80
+ end
81
+
82
+ it "has adjustable unit separator and thousands delimiters" do
83
+ expect(5.to_currency separator: ",").to eq("$5,00")
84
+ expect(5e3.to_currency separator: ",", delimiter: ".").to eq("$5.000,00")
85
+ end
86
+
87
+ it "has adjustable sign position" do
88
+ expect(5000.5.to_currency sign: " €", position: :right,
89
+ separator: ",", delimiter: " ").
90
+ to eq("5 000,50 €")
91
+ end
92
+ end
93
+
40
94
  describe "#sanity_check_min" do
41
95
  it "returns the parameter if the number is lower" do
42
96
  expect(5.sanity_check_min(7)).to eq(7)
@@ -45,32 +45,5 @@ This text should be left-aligned.
45
45
  Left-aligned again.
46
46
  EOS
47
47
  end
48
-
49
- =begin
50
- it "works with string interpolation" do
51
- short = "Test"
52
- inner = <<-EOS.heredoc.strip
53
- This text should be left-aligned.
54
- This is indented by two.
55
- Last line of the inner string, left-aligned.
56
- EOS
57
-
58
- test = <<-EOS.heredoc
59
- Testing short in-line: #{short}
60
- Testing next-line:
61
- #{inner.in_heredoc 8}
62
- Ending interpolation test.
63
- EOS
64
-
65
- test.should == <<EOS
66
- Testing short in-line: Test
67
- Testing next-line:
68
- This text should be left-aligned.
69
- This is indented by two.
70
- Last line of the inner string, left-aligned.
71
- Ending interpolation test.
72
- EOS
73
- end
74
- =end
75
48
  end
76
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nutella
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.12'
4
+ version: '0.13'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinny Diehl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-04 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fuubar
@@ -121,6 +121,7 @@ files:
121
121
  - lib/nutella/core_ext/numeric/sanity_check.rb
122
122
  - lib/nutella/core_ext/numeric/bytes.rb
123
123
  - lib/nutella/core_ext/numeric/percent.rb
124
+ - lib/nutella/core_ext/numeric/format.rb
124
125
  - lib/nutella/core_ext/object.rb
125
126
  - lib/nutella/core_ext/file.rb
126
127
  - lib/nutella/core_ext/enumerable/map.rb
@@ -148,7 +149,7 @@ files:
148
149
  - Rakefile
149
150
  - README.md
150
151
  - nutella.gemspec
151
- homepage: https://github.com/gbchaosmaster/nutella
152
+ homepage: https://github.com/vinnydiehl/nutella
152
153
  licenses:
153
154
  - MIT
154
155
  metadata: {}