formatting 0.0.6 → 0.0.7
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 +4 -4
- data/README.md +32 -9
- data/lib/formatting/currency.rb +7 -3
- data/lib/formatting/version.rb +1 -1
- data/spec/currency_spec.rb +24 -0
- data/spec/number_spec.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f41d31f291d939056af4b95f837c90e6eccc94ae
|
4
|
+
data.tar.gz: 3061bd63d93f6c7b9d0082fff54dac279b072c34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf1788bf1924016ef909aa7f4fb51c5ee6205d6ad6700d50b32a44f3f5c7798d3edc6c854cf4d4a1c0ff44942cff344c72c4a68e635f10cb31845db488deb19d
|
7
|
+
data.tar.gz: 638b6e2ec4d7ad0eb97a5a53cd0afa208b2fbe819606dc92748f60b8768f4d082520f79868a59a914a5be5fce0e60684dafe01be7a7aeabc11c5fc2111d8cb1b
|
data/README.md
CHANGED
@@ -32,35 +32,58 @@ format_currency("SEK", 1234) # => "1,234 SEK"
|
|
32
32
|
|
33
33
|
### Number
|
34
34
|
|
35
|
+
#### Example usage
|
36
|
+
|
35
37
|
``` ruby
|
36
38
|
Formatting.format_number(1234.567) # => "1,234.57"
|
37
39
|
Formatting.format_number(0, blank_when_zero: true) # => ""
|
38
40
|
Formatting.format_number(1, explicit_sign: true) # => "+1"
|
39
41
|
```
|
40
42
|
|
43
|
+
#### Options
|
44
|
+
|
45
|
+
name | default | explanation
|
46
|
+
--------------------|----------------------------------------------------------------------------------|------------
|
47
|
+
thousands_separator | `I18n.t("number.format.delimiter")` if available, otherwise a non-breaking space |
|
48
|
+
decimal_separator | `I18n.t("number.format.separator")` if available, otherwise `"."` |
|
49
|
+
round | `2` | Round to the given number of decimals. Don't round if given `false`.
|
50
|
+
blank_when_zero | `false` | If `true`, returns `""` for a zero value.
|
51
|
+
min_decimals | `2` | Show at least that number of decimals. Don't enforce if given `false`.
|
52
|
+
explicit_sign | `false` | If `true`, prefixes positive values with a `"+"`. Doesn't prefix `0`.
|
53
|
+
|
54
|
+
|
41
55
|
### Currency
|
42
56
|
|
43
57
|
The currency formatter should usually be passed some object that
|
44
58
|
the currency can be determined from. The idea is that even if you
|
45
59
|
only have one currency now, you may add more later.
|
46
60
|
|
47
|
-
|
48
|
-
Formatting.format_currency("SEK", 1234) # => "1,234 SEK"
|
61
|
+
#### Example usage
|
49
62
|
|
63
|
+
``` ruby
|
50
64
|
item = Item.new(price: 1234, currency: "SEK")
|
51
|
-
Formatting.format_currency(item, :price) # => "1,234 SEK"
|
52
|
-
Formatting.format_currency(company, item.price) # => "1,234 SEK"
|
65
|
+
Formatting.format_currency(item, :price) # => "1,234.00 SEK"
|
66
|
+
Formatting.format_currency(company, item.price) # => "1,234.00 SEK"
|
67
|
+
Formatting.format_currency(company, 4567) # => "4,567.00 SEK"
|
68
|
+
|
69
|
+
Formatting.format_currency(company, 4567, currency: false) # => "4,567.00"
|
53
70
|
|
54
71
|
Formatting.defaults[:currency] = "SEK"
|
55
72
|
item = Item.new(price: 1234) # Does not respond to "currency"
|
56
|
-
Formatting.format_currency(item, :price) # => "1,234 SEK"
|
73
|
+
Formatting.format_currency(item, :price) # => "1,234.00 SEK"
|
57
74
|
|
58
|
-
item = Item.new(price: 1234, currency: "SEK")
|
59
|
-
Formatting.format_currency(item, :price, show_currency: false) # => "1,234"
|
60
|
-
Formatting.defaults[:show_currency] = false
|
61
|
-
Formatting.format_currency(item, :price) # => "1,234"
|
62
75
|
```
|
63
76
|
|
77
|
+
#### Options
|
78
|
+
|
79
|
+
Passes on all the number options and also takes these:
|
80
|
+
|
81
|
+
name | default | explanation
|
82
|
+
----------------|----------------------------------------|------------
|
83
|
+
currency | `first_argument.currency` if available | E.g. `"USD"`. Can be `false`.
|
84
|
+
format | `"<amount> <currency>"` | A format string. Any spaces become non-breaking spaces.
|
85
|
+
skip_currency | `false` | If `true`, doesn't add the currency to the amount.
|
86
|
+
|
64
87
|
|
65
88
|
## Installation
|
66
89
|
|
data/lib/formatting/currency.rb
CHANGED
@@ -12,10 +12,14 @@ module Formatting
|
|
12
12
|
opts = Formatting.defaults.merge(opts)
|
13
13
|
|
14
14
|
format_string = opts.fetch(:format, "<amount> <currency>")
|
15
|
+
skip_currency = opts.fetch(:skip_currency, false)
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
unless skip_currency
|
18
|
+
currency = opts.fetch(:currency) {
|
19
|
+
record.respond_to?(:currency) ? record.currency: nil
|
20
|
+
}
|
21
|
+
currency = nil if currency == false
|
22
|
+
end
|
19
23
|
|
20
24
|
if amount_or_method.is_a?(Symbol)
|
21
25
|
amount = record.public_send(amount_or_method)
|
data/lib/formatting/version.rb
CHANGED
data/spec/currency_spec.rb
CHANGED
@@ -64,6 +64,10 @@ describe Formatting do
|
|
64
64
|
it "is not added if the record does not respond to #currency" do
|
65
65
|
expect_formatted(item, 1).to eq space_to_nbsp("1.00")
|
66
66
|
end
|
67
|
+
|
68
|
+
it "is not added if given as 'false'" do
|
69
|
+
expect_formatted(item, 1, currency: false).to eq space_to_nbsp("1.00")
|
70
|
+
end
|
67
71
|
end
|
68
72
|
|
69
73
|
context "format string option" do
|
@@ -83,6 +87,26 @@ describe Formatting do
|
|
83
87
|
to eq space_to_nbsp("123.00")
|
84
88
|
end
|
85
89
|
end
|
90
|
+
|
91
|
+
context "skip_currency option" do
|
92
|
+
context "set to 'true'" do
|
93
|
+
it "doesn't add the currency" do
|
94
|
+
expect_formatted(item, 123, currency: "SEK", skip_currency: true).to eq space_to_nbsp("123.00")
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "set to 'false'" do
|
99
|
+
it "adds the currency" do
|
100
|
+
expect_formatted(item, 123, currency: "SEK", skip_currency: false).to eq space_to_nbsp("123.00 SEK")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "by default" do
|
105
|
+
it "adds the currency" do
|
106
|
+
expect_formatted(item, 123, currency: "SEK").to eq space_to_nbsp("123.00 SEK")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
86
110
|
end
|
87
111
|
|
88
112
|
def expect_formatted(record, value, opts = {})
|
data/spec/number_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|