formatting 0.0.14 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.md +28 -9
- data/lib/formatting.rb +2 -0
- data/lib/formatting/number.rb +6 -1
- data/lib/formatting/percent.rb +12 -0
- data/lib/formatting/version.rb +1 -1
- data/spec/number_spec.rb +10 -0
- data/spec/percent_spec.rb +23 -0
- metadata +14 -11
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
NmM1M2JjNjQ1NDA0N2FjYzI0NmU3ZTc1YWQ4M2FlYTgxZGJlNWEyNQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f8a92f61e13526876dc609e99cabc25252e14303
|
4
|
+
data.tar.gz: 804ee6b74f3f1521075dd601a8d90b169c2fa0f8
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MWU4YmNiNDcyODNhZjgwZDNmYzhiNDE3ZmI0OGQ5ZTNiMzU5OWYzNmNlNDBh
|
11
|
-
Y2YzYTQ3YTlhZjdkNDMzY2Y0MWM1ODllN2Y1NzllYzAxY2JmMzU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
YjI2YWRiYTBlZTY4ZjBlOWZkZDdiYTgwYTlhNDQ1NTA0YjE3MmI4MTM4Mjhk
|
14
|
-
NzhhMTk1OGUzYzNhM2NmNjc3MGJhNzEyMmFhYTE3OWZhYWNkYzZjYjZkNzQw
|
15
|
-
YWM1MzYzMDA5N2IyOWQxNDhkOGQwNzFkOTM3NzQxZTA4YmY4NDI=
|
6
|
+
metadata.gz: c3d5b1625686c2dc865841a1800d2b971604f951086570c80961acf75a72154dcd0747c54b54a11380fcd45f0be29163510a971ec99f9346846300d4093322c7
|
7
|
+
data.tar.gz: 591e74fcd72914b2f3afe535e13c0d31e6935b5930495f373ce1fd16194e158bd8204153b6cb7c5dc10992dfcd2f9e253814f6dca37c9afd91db888a33a42fb6
|
data/README.md
CHANGED
@@ -7,6 +7,7 @@ Will use the `i18n` library for formatting defaults if present, but it's not a r
|
|
7
7
|
Formats:
|
8
8
|
* Numbers
|
9
9
|
* Currency
|
10
|
+
* Percent
|
10
11
|
|
11
12
|
|
12
13
|
## Usage
|
@@ -25,6 +26,9 @@ format_number(1234) # => "1,234.00"
|
|
25
26
|
|
26
27
|
include Formatting::Currency
|
27
28
|
format_currency(item, :price) # => "1,234.00 SEK"
|
29
|
+
|
30
|
+
include Formatting::Percent
|
31
|
+
format_percent(12.3) # => "12.30%"
|
28
32
|
```
|
29
33
|
|
30
34
|
|
@@ -40,15 +44,16 @@ Formatting.format_number(1, explicit_sign: true) # => "+1.00"
|
|
40
44
|
|
41
45
|
#### Options
|
42
46
|
|
43
|
-
name
|
44
|
-
|
45
|
-
thousands_separator
|
46
|
-
decimal_separator
|
47
|
-
round
|
48
|
-
blank_when_zero
|
49
|
-
min_decimals
|
50
|
-
explicit_sign
|
51
|
-
decimals_on_integers
|
47
|
+
name | default | explanation
|
48
|
+
-------------------------------|----------------------------------------------------------------------------------|------------
|
49
|
+
thousands_separator | `I18n.t("number.format.delimiter")` if available, otherwise a non-breaking space |
|
50
|
+
decimal_separator | `I18n.t("number.format.separator")` if available, otherwise `"."` |
|
51
|
+
round | `2` | Round to the given number of decimals. Don't round if given `false`.
|
52
|
+
blank_when_zero | `false` | If `true`, returns `""` for a zero value.
|
53
|
+
min_decimals | `2` | Show at least that number of decimals. Don't enforce if given `false`.
|
54
|
+
explicit_sign | `false` | If `true`, prefixes positive values with a `"+"`. Doesn't prefix `0`.
|
55
|
+
decimals_on_integers | `true` | If `false`, integers won't include decimals.
|
56
|
+
treat_zero_decimals_as_integer | `false` | If `true`, a zero value in the decimal column formats the number like an integer.
|
52
57
|
|
53
58
|
|
54
59
|
### Currency
|
@@ -83,6 +88,20 @@ format | `"<amount> <currency>"`
|
|
83
88
|
skip_currency | `false` | If `true`, doesn't add the currency to the amount.
|
84
89
|
|
85
90
|
|
91
|
+
### Percent
|
92
|
+
|
93
|
+
#### Example usage
|
94
|
+
|
95
|
+
``` ruby
|
96
|
+
Formatting.format_percent(12.3) # => "12.3%"
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
#### Options
|
101
|
+
|
102
|
+
Passes on all the number options.
|
103
|
+
|
104
|
+
|
86
105
|
## Installation
|
87
106
|
|
88
107
|
Add this line to your application's Gemfile:
|
data/lib/formatting.rb
CHANGED
data/lib/formatting/number.rb
CHANGED
@@ -10,7 +10,7 @@ module Formatting
|
|
10
10
|
class FormatNumber
|
11
11
|
attr_private :input_number,
|
12
12
|
:thousands_separator, :decimal_separator,
|
13
|
-
:round, :min_decimals, :decimals_on_integers, :explicit_sign, :blank_when_zero
|
13
|
+
:round, :min_decimals, :decimals_on_integers, :treat_zero_decimals_as_integer, :explicit_sign, :blank_when_zero
|
14
14
|
|
15
15
|
def initialize(input_number, opts)
|
16
16
|
@input_number = input_number
|
@@ -20,6 +20,7 @@ module Formatting
|
|
20
20
|
@round = opts.fetch(:round, 2)
|
21
21
|
@min_decimals = opts.fetch(:min_decimals, 2)
|
22
22
|
@decimals_on_integers = opts.fetch(:decimals_on_integers, true)
|
23
|
+
@treat_zero_decimals_as_integer = opts.fetch(:treat_zero_decimals_as_integer, false)
|
23
24
|
@explicit_sign = opts.fetch(:explicit_sign, false)
|
24
25
|
@blank_when_zero = opts.fetch(:blank_when_zero, false)
|
25
26
|
end
|
@@ -27,6 +28,10 @@ module Formatting
|
|
27
28
|
def format
|
28
29
|
number = input_number
|
29
30
|
|
31
|
+
if treat_zero_decimals_as_integer && number.modulo(1).zero?
|
32
|
+
number = number.to_i
|
33
|
+
end
|
34
|
+
|
30
35
|
has_decimals = number.to_s.include?(".")
|
31
36
|
|
32
37
|
if blank_when_zero
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# http://en.wikipedia.org/wiki/Percent_sign#Spacing
|
2
|
+
|
3
|
+
require "attr_extras"
|
4
|
+
|
5
|
+
module Formatting
|
6
|
+
module Percent
|
7
|
+
def format_percent(number, opts = {})
|
8
|
+
formatted_number = Formatting.format_number(number, opts)
|
9
|
+
"#{formatted_number}%"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/formatting/version.rb
CHANGED
data/spec/number_spec.rb
CHANGED
@@ -120,6 +120,16 @@ describe Formatting do
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
+
context "treat_zero_decimals_as_integer" do
|
124
|
+
it "defaults to false" do
|
125
|
+
expect_formatted(12.0, decimals_on_integers: false).to eq "12.00"
|
126
|
+
end
|
127
|
+
|
128
|
+
it "can be turned off" do
|
129
|
+
expect_formatted(12.0, decimals_on_integers: false, treat_zero_decimals_as_integer: true).to eq "12"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
123
133
|
context "explicit sign" do
|
124
134
|
it "is not included by default" do
|
125
135
|
expect_formatted(1).to eq "1.00"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Formatting::Percent do
|
4
|
+
it "can be included as a module" do
|
5
|
+
object = Object.new
|
6
|
+
object.extend Formatting::Percent
|
7
|
+
expect(object).to respond_to(:format_percent)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Formatting, ".format_percent" do
|
12
|
+
it "formats a number" do
|
13
|
+
expect_formatted(1.2).to eq "1.20%"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "passes on number formatting options" do
|
17
|
+
expect_formatted(1.567, round: 2).to include "1.57"
|
18
|
+
end
|
19
|
+
|
20
|
+
def expect_formatted(number, opts = {})
|
21
|
+
expect(Formatting.format_percent(number, opts))
|
22
|
+
end
|
23
|
+
end
|
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.
|
4
|
+
version: 0.0.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -9,20 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-03-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: attr_extras
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - '>='
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - '>='
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
@@ -43,28 +43,28 @@ dependencies:
|
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - '>='
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - '>='
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rspec
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - '>='
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - '>='
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
description:
|
@@ -84,9 +84,11 @@ files:
|
|
84
84
|
- lib/formatting.rb
|
85
85
|
- lib/formatting/currency.rb
|
86
86
|
- lib/formatting/number.rb
|
87
|
+
- lib/formatting/percent.rb
|
87
88
|
- lib/formatting/version.rb
|
88
89
|
- spec/currency_spec.rb
|
89
90
|
- spec/number_spec.rb
|
91
|
+
- spec/percent_spec.rb
|
90
92
|
- spec/spec_helper.rb
|
91
93
|
homepage: ''
|
92
94
|
licenses:
|
@@ -98,21 +100,22 @@ require_paths:
|
|
98
100
|
- lib
|
99
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
100
102
|
requirements:
|
101
|
-
- -
|
103
|
+
- - '>='
|
102
104
|
- !ruby/object:Gem::Version
|
103
105
|
version: '0'
|
104
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
107
|
requirements:
|
106
|
-
- -
|
108
|
+
- - '>='
|
107
109
|
- !ruby/object:Gem::Version
|
108
110
|
version: '0'
|
109
111
|
requirements: []
|
110
112
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.2.
|
113
|
+
rubygems_version: 2.2.0
|
112
114
|
signing_key:
|
113
115
|
specification_version: 4
|
114
116
|
summary: Rails-less formatting for your unit-testable code.
|
115
117
|
test_files:
|
116
118
|
- spec/currency_spec.rb
|
117
119
|
- spec/number_spec.rb
|
120
|
+
- spec/percent_spec.rb
|
118
121
|
- spec/spec_helper.rb
|