nutella 0.12 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/lib/nutella/core_ext/module.rb +0 -0
- data/lib/nutella/core_ext/numeric/format.rb +37 -0
- data/lib/nutella/core_ext/string/heredoc.rb +1 -5
- data/lib/nutella/version.rb +1 -1
- data/nutella.gemspec +2 -2
- data/spec/nutella/core_ext/numeric_spec.rb +54 -0
- data/spec/nutella/core_ext/string_spec.rb +0 -27
- data/spec/support/initializer_testers.rb +0 -0
- data/spec/support/matchers.rb +0 -0
- metadata +60 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5717428d738f3f8e6978218c059285a055c95bd3754a56050583673b23c81ccf
|
4
|
+
data.tar.gz: 653d131499dbbea4275a404f90cf95bcd82afc5b5bb37d658cac69be0b9a88d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fe6c2c8e7fa6a4efb2c56fcb8ed37b29544dd8f4dbbe57a23f403e9e420a138dd2675306dc191b6ab4d5565b691978b6ac25f46bf33e9f57f928765a1eb369c
|
7
|
+
data.tar.gz: e940732a999beeee930874960329c1382ac6bcf49db26bb847b24508a13ab5224db3249c8af96fce6714a3ddd7a00a483aa42c76adad46786f92530d0be6e611
|
File without changes
|
@@ -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
|
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
|
data/lib/nutella/version.rb
CHANGED
data/nutella.gemspec
CHANGED
@@ -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/
|
9
|
+
gem.homepage = "https://github.com/vinnydiehl/nutella"
|
10
10
|
|
11
11
|
gem.license = "MIT"
|
12
12
|
|
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
.rspec LICENSE Rakefile README.md nutella.gemspec
|
20
20
|
]
|
21
21
|
|
22
|
-
gem.required_ruby_version = "
|
22
|
+
gem.required_ruby_version = ">= 2.0"
|
23
23
|
gem.add_development_dependency "fuubar", "~> 2.0"
|
24
24
|
gem.add_development_dependency "rake", "~> 10.0"
|
25
25
|
gem.add_development_dependency "redcarpet", "~> 2.2"
|
@@ -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
|
File without changes
|
data/spec/support/matchers.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.13.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vinny Diehl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fuubar
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: redcarpet
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '2.2'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rdoc
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '3.12'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.12'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '3.1'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.1'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: yard
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - ~>
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0.8'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - ~>
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.8'
|
97
97
|
description: My collection of Ruby utilities, mainly core extensions.
|
@@ -100,55 +100,56 @@ executables: []
|
|
100
100
|
extensions: []
|
101
101
|
extra_rdoc_files: []
|
102
102
|
files:
|
103
|
+
- ".rspec"
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/nutella.rb
|
103
108
|
- lib/nutella/core_ext.rb
|
104
|
-
- lib/nutella/version.rb
|
105
|
-
- lib/nutella/input.rb
|
106
109
|
- lib/nutella/core_ext/enumerable.rb
|
107
|
-
- lib/nutella/core_ext/
|
108
|
-
- lib/nutella/core_ext/
|
109
|
-
- lib/nutella/core_ext/
|
110
|
-
- lib/nutella/core_ext/
|
111
|
-
- lib/nutella/core_ext/
|
112
|
-
- lib/nutella/core_ext/
|
113
|
-
- lib/nutella/core_ext/integer/multiples.rb
|
114
|
-
- lib/nutella/core_ext/integer/format.rb
|
115
|
-
- lib/nutella/core_ext/integer/digits.rb
|
116
|
-
- lib/nutella/core_ext/integer.rb
|
117
|
-
- lib/nutella/core_ext/object/blank.rb
|
118
|
-
- lib/nutella/core_ext/object/aliases.rb
|
110
|
+
- lib/nutella/core_ext/enumerable/apply.rb
|
111
|
+
- lib/nutella/core_ext/enumerable/group.rb
|
112
|
+
- lib/nutella/core_ext/enumerable/map.rb
|
113
|
+
- lib/nutella/core_ext/enumerable/search.rb
|
114
|
+
- lib/nutella/core_ext/enumerable/sum.rb
|
115
|
+
- lib/nutella/core_ext/file.rb
|
119
116
|
- lib/nutella/core_ext/hash.rb
|
117
|
+
- lib/nutella/core_ext/integer.rb
|
118
|
+
- lib/nutella/core_ext/integer/digits.rb
|
119
|
+
- lib/nutella/core_ext/integer/format.rb
|
120
|
+
- lib/nutella/core_ext/integer/multiples.rb
|
121
|
+
- lib/nutella/core_ext/module.rb
|
120
122
|
- lib/nutella/core_ext/numeric.rb
|
121
|
-
- lib/nutella/core_ext/numeric/sanity_check.rb
|
122
123
|
- lib/nutella/core_ext/numeric/bytes.rb
|
124
|
+
- lib/nutella/core_ext/numeric/format.rb
|
123
125
|
- lib/nutella/core_ext/numeric/percent.rb
|
126
|
+
- lib/nutella/core_ext/numeric/sanity_check.rb
|
124
127
|
- lib/nutella/core_ext/object.rb
|
125
|
-
- lib/nutella/core_ext/
|
126
|
-
- lib/nutella/core_ext/
|
127
|
-
- lib/nutella/core_ext/
|
128
|
-
- lib/nutella/core_ext/
|
129
|
-
- lib/nutella/core_ext/
|
130
|
-
- lib/nutella/core_ext/
|
131
|
-
- lib/nutella.rb
|
132
|
-
-
|
133
|
-
-
|
134
|
-
-
|
128
|
+
- lib/nutella/core_ext/object/aliases.rb
|
129
|
+
- lib/nutella/core_ext/object/blank.rb
|
130
|
+
- lib/nutella/core_ext/string.rb
|
131
|
+
- lib/nutella/core_ext/string/aliases.rb
|
132
|
+
- lib/nutella/core_ext/string/colour.rb
|
133
|
+
- lib/nutella/core_ext/string/exclude.rb
|
134
|
+
- lib/nutella/core_ext/string/heredoc.rb
|
135
|
+
- lib/nutella/input.rb
|
136
|
+
- lib/nutella/version.rb
|
137
|
+
- nutella.gemspec
|
135
138
|
- spec/nutella/core_ext/enumerable_spec.rb
|
136
|
-
- spec/nutella/core_ext/
|
139
|
+
- spec/nutella/core_ext/file_spec.rb
|
137
140
|
- spec/nutella/core_ext/hash_spec.rb
|
138
141
|
- spec/nutella/core_ext/integer_spec.rb
|
142
|
+
- spec/nutella/core_ext/module_spec.rb
|
143
|
+
- spec/nutella/core_ext/numeric_spec.rb
|
139
144
|
- spec/nutella/core_ext/object_spec.rb
|
145
|
+
- spec/nutella/core_ext/string_spec.rb
|
146
|
+
- spec/spec_helper.rb
|
140
147
|
- spec/support/alias.rb
|
141
|
-
- spec/support/number_formats.rb
|
142
148
|
- spec/support/apply_tester.rb
|
143
|
-
- spec/support/matchers.rb
|
144
149
|
- spec/support/initializer_testers.rb
|
145
|
-
- spec/
|
146
|
-
- .
|
147
|
-
|
148
|
-
- Rakefile
|
149
|
-
- README.md
|
150
|
-
- nutella.gemspec
|
151
|
-
homepage: https://github.com/gbchaosmaster/nutella
|
150
|
+
- spec/support/matchers.rb
|
151
|
+
- spec/support/number_formats.rb
|
152
|
+
homepage: https://github.com/vinnydiehl/nutella
|
152
153
|
licenses:
|
153
154
|
- MIT
|
154
155
|
metadata: {}
|
@@ -158,33 +159,31 @@ require_paths:
|
|
158
159
|
- lib
|
159
160
|
required_ruby_version: !ruby/object:Gem::Requirement
|
160
161
|
requirements:
|
161
|
-
- -
|
162
|
+
- - ">="
|
162
163
|
- !ruby/object:Gem::Version
|
163
164
|
version: '2.0'
|
164
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
166
|
requirements:
|
166
|
-
- -
|
167
|
+
- - ">="
|
167
168
|
- !ruby/object:Gem::Version
|
168
169
|
version: '0'
|
169
170
|
requirements: []
|
170
|
-
|
171
|
-
rubygems_version: 2.0.14
|
171
|
+
rubygems_version: 3.3.5
|
172
172
|
signing_key:
|
173
173
|
specification_version: 4
|
174
174
|
summary: Spread some Nutella on Ruby to sweeten up its functionality.
|
175
175
|
test_files:
|
176
|
-
- spec/nutella/core_ext/file_spec.rb
|
177
|
-
- spec/nutella/core_ext/string_spec.rb
|
178
|
-
- spec/nutella/core_ext/module_spec.rb
|
179
176
|
- spec/nutella/core_ext/enumerable_spec.rb
|
180
|
-
- spec/nutella/core_ext/
|
177
|
+
- spec/nutella/core_ext/file_spec.rb
|
181
178
|
- spec/nutella/core_ext/hash_spec.rb
|
182
179
|
- spec/nutella/core_ext/integer_spec.rb
|
180
|
+
- spec/nutella/core_ext/module_spec.rb
|
181
|
+
- spec/nutella/core_ext/numeric_spec.rb
|
183
182
|
- spec/nutella/core_ext/object_spec.rb
|
183
|
+
- spec/nutella/core_ext/string_spec.rb
|
184
|
+
- spec/spec_helper.rb
|
184
185
|
- spec/support/alias.rb
|
185
|
-
- spec/support/number_formats.rb
|
186
186
|
- spec/support/apply_tester.rb
|
187
|
-
- spec/support/matchers.rb
|
188
187
|
- spec/support/initializer_testers.rb
|
189
|
-
- spec/
|
190
|
-
|
188
|
+
- spec/support/matchers.rb
|
189
|
+
- spec/support/number_formats.rb
|