formatting 0.0.8 → 0.0.9

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: 35af947641623b8cf80b8323106412901717087e
4
- data.tar.gz: 41ff76fcf71cac124cd77f1b58d0c70a21fdf0a1
3
+ metadata.gz: 8491d72d7e09b9a42a8c97708417a144262245a4
4
+ data.tar.gz: 9a2be257d8fef3a05d78945223bde008c89ca9b4
5
5
  SHA512:
6
- metadata.gz: 4c49acfa52b48b08f6309e3e5eb2db8d55705c7c67b2aabc8ca9245084ecaadf3b55e5b641c3b983cc4b1fe7b665e3abb7fc16467c4bb9b336e468f04e48dd18
7
- data.tar.gz: 9a223c7ed6d58d67f74eb0a5cfb0898b0f68980ff1f31f2e59888b0c454cadaea6052a72b1ccd4481922fd6e8cebb2182090b777fdbdf6dfe01a3921eb597d8d
6
+ metadata.gz: d603a5393ff8b04fa33a0a9c9752e8b43f4d5a7dd0334702e6fbeafa32261a9a13ed887ac111740268561029af2a0e2da9411c0ceb51b659b6ffc672f21c7e1c
7
+ data.tar.gz: 20f56e657f8898923ee5f218f448e45bd6a4f65ad7da863725dcb6eaa7a42733d24acac6f4b5fc7a572c95b090c49ca6a02c94c47010f3663100019aa1045d5a
data/README.md CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  Rails-less formatting for your unit-testable code.
4
4
 
5
- *Does* currently depend on the `i18n` library for number separators.
6
-
7
- Very much a work in progress currently.
5
+ Will use the `i18n` library for formatting defaults if present, but it's not a requirement.
8
6
 
9
7
  Formats:
10
8
  * Numbers
@@ -23,10 +21,10 @@ Or include the modules you want:
23
21
 
24
22
  ``` ruby
25
23
  include Formatting::Number
26
- format_number(1234) # => "1,234"
24
+ format_number(1234) # => "1,234.00"
27
25
 
28
26
  include Formatting::Currency
29
- format_currency("SEK", 1234) # => "1,234 SEK"
27
+ format_currency(item, :price) # => "1,234.00 SEK"
30
28
  ```
31
29
 
32
30
 
@@ -37,7 +35,7 @@ format_currency("SEK", 1234) # => "1,234 SEK"
37
35
  ``` ruby
38
36
  Formatting.format_number(1234.567) # => "1,234.57"
39
37
  Formatting.format_number(0, blank_when_zero: true) # => ""
40
- Formatting.format_number(1, explicit_sign: true) # => "+1"
38
+ Formatting.format_number(1, explicit_sign: true) # => "+1.00"
41
39
  ```
42
40
 
43
41
  #### Options
@@ -58,6 +56,9 @@ The currency formatter should usually be passed some object that
58
56
  the currency can be determined from. The idea is that even if you
59
57
  only have one currency now, you may add more later.
60
58
 
59
+ But you can also pass an explicit currency, as the first argument
60
+ or as the `currency` option.
61
+
61
62
  #### Example usage
62
63
 
63
64
  ``` ruby
@@ -65,19 +66,20 @@ item = Item.new(price: 1234, currency: "SEK")
65
66
  Formatting.format_currency(item, :price) # => "1,234.00 SEK"
66
67
  Formatting.format_currency(company, item.price) # => "1,234.00 SEK"
67
68
  Formatting.format_currency(company, 4567) # => "4,567.00 SEK"
68
-
69
69
  Formatting.format_currency(company, 4567, currency: false) # => "4,567.00"
70
+ Formatting.format_currency("SEK", 4567) # => "4,567.00 SEK"
70
71
  ```
71
72
 
73
+
72
74
  #### Options
73
75
 
74
76
  Passes on all the number options and also takes these:
75
77
 
76
- name | default | explanation
77
- ----------------|----------------------------------------|------------
78
- currency | `first_argument.currency` if available | E.g. `"USD"`. Can be `false`.
79
- format | `"<amount> <currency>"` | A format string. Any spaces become non-breaking spaces.
80
- skip_currency | `false` | If `true`, doesn't add the currency to the amount.
78
+ name | default | explanation
79
+ ----------------|--------------------------------------------------------------------------------|------------
80
+ currency | `first_argument.currency` if available, or `first_argument` if it's a currency | E.g. `"USD"`. Can be `false`.
81
+ format | `"<amount> <currency>"` | A format string. Any spaces become non-breaking spaces.
82
+ skip_currency | `false` | If `true`, doesn't add the currency to the amount.
81
83
 
82
84
 
83
85
  ## Installation
@@ -98,5 +100,4 @@ Or install it yourself as:
98
100
  ## TODO
99
101
 
100
102
  * Use real i18n in specs so they're less fragile and ugly
101
- * Document options
102
103
  * Rename? This name is boring and also generic enough that collisions seem likely.
@@ -1,26 +1,25 @@
1
1
  module Formatting
2
- class NotARecordError < StandardError; end
3
-
4
2
  module Currency
5
3
  include Number
6
4
 
7
- def format_currency(record, amount_or_method, opts = {})
8
- if record.is_a?(Symbol)
9
- raise NotARecordError, "Expected an object that could tell us its currency; got #{record.inspect}"
10
- end
11
-
5
+ def format_currency(record_or_currency, amount_or_method, opts = {})
12
6
  format_string = opts.fetch(:format, "<amount> <currency>")
13
7
  skip_currency = opts.fetch(:skip_currency, false)
14
8
 
15
9
  unless skip_currency
16
10
  currency = opts.fetch(:currency) {
17
- record.respond_to?(:currency) ? record.currency: nil
11
+ case record_or_currency
12
+ when String, Symbol
13
+ record_or_currency
14
+ else
15
+ record_or_currency.respond_to?(:currency) ? record_or_currency.currency : nil
16
+ end
18
17
  }
19
18
  currency = nil if currency == false
20
19
  end
21
20
 
22
21
  if amount_or_method.is_a?(Symbol)
23
- amount = record.public_send(amount_or_method)
22
+ amount = record_or_currency.public_send(amount_or_method)
24
23
  else
25
24
  amount = amount_or_method
26
25
  end
@@ -1,3 +1,3 @@
1
1
  module Formatting
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -22,8 +22,9 @@ describe Formatting do
22
22
  expect_formatted(item, :price).to eq space_to_nbsp("2.00")
23
23
  end
24
24
 
25
- it "complains if the 'record' looks like a method name (a likely mistake)" do
26
- expect { Formatting.format_currency(:item, 123) }.to raise_error(Formatting::NotARecordError)
25
+ it "can take a currency string or symbol and a value" do
26
+ expect_formatted("SEK", 2).to eq space_to_nbsp("2.00 SEK")
27
+ expect_formatted(:SEK, 2).to eq space_to_nbsp("2.00 SEK")
27
28
  end
28
29
  end
29
30
 
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.8
4
+ version: 0.0.9
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-24 00:00:00.000000000 Z
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,28 +28,28 @@ dependencies:
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: '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: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
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: '0'
55
55
  description:
@@ -82,17 +82,17 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ! '>='
85
+ - - '>='
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ! '>='
90
+ - - '>='
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.0.3
95
+ rubygems_version: 2.2.0
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Rails-less formatting for your unit-testable code.
@@ -100,4 +100,3 @@ test_files:
100
100
  - spec/currency_spec.rb
101
101
  - spec/number_spec.rb
102
102
  - spec/spec_helper.rb
103
- has_rdoc: