ordinalize_full 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/README.md +3 -1
- data/lib/ordinalize_full.rb +32 -8
- data/lib/ordinalize_full/integer.rb +1 -1
- data/ordinalize_full.gemspec +1 -1
- data/spec/ordinalize_full_spec.rb +28 -0
- 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: d4594f97093bb7853d4c0e15b7e5aa6bf72905ec
|
4
|
+
data.tar.gz: f9c7b05ae930ab830f1cc1a8ae3697eefc7bd768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36ad759eb60b6cc789ec181cd4b8a1641d7eaaabd7117b53fda9f00cea93c0dc0fff404c6b6f45d1f607ecfc28174e7af1619f5817ac43fb20f33e1f2f40f8ff
|
7
|
+
data.tar.gz: 61b6b76999c5bc5b2759e9664b91464059c5691038a0ef77d94e206511b0b804d5e9bc2a0b60657f04dc03609828bb2609488dbd004a91b5cf7f0f8f854bc6f1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Features:
|
|
7
7
|
- i18n support
|
8
8
|
- doesn't monkey-patch
|
9
9
|
- easy to integrate with Rails but doesn't require Rails
|
10
|
-
- less than
|
10
|
+
- less than 50 lines of code
|
11
11
|
|
12
12
|
## Usage
|
13
13
|
|
@@ -19,9 +19,11 @@ require "ordinalize_full/integer"
|
|
19
19
|
42.ordinalize_in_full #=> "forty second"
|
20
20
|
42.ordinalize_full #=> "forty second"
|
21
21
|
42.ordinalize(in_full: true) #=> "forty second"
|
22
|
+
42.ordinalize #=> "42nd"
|
22
23
|
|
23
24
|
I18n.locale = :fr
|
24
25
|
42.ordinalize_in_full #=> "quarante-deuxième"
|
26
|
+
42.ordinalize #=> "42ème"
|
25
27
|
```
|
26
28
|
|
27
29
|
### Without monkey-patching
|
data/lib/ordinalize_full.rb
CHANGED
@@ -4,18 +4,42 @@ module OrdinalizeFull
|
|
4
4
|
I18n.load_path << Dir[File.join(__dir__, "ordinalize_full/locales/*.yml")]
|
5
5
|
|
6
6
|
def ordinalize(in_full: false)
|
7
|
-
|
8
|
-
self.ordinalize_in_full
|
9
|
-
else
|
10
|
-
require "active_support/inflector"
|
11
|
-
ActiveSupport::Inflector.ordinalize(self)
|
12
|
-
end
|
7
|
+
in_full ? ordinalize_in_full : ordinalize_in_short
|
13
8
|
end
|
14
9
|
|
10
|
+
alias_method :ordinalize_full, \
|
15
11
|
def ordinalize_in_full
|
16
|
-
|
12
|
+
begin
|
13
|
+
I18n.t("ordinalize_full.n_#{self}", throw: true)
|
14
|
+
rescue ArgumentError
|
15
|
+
raise NotImplementedError, "Unknown locale #{I18n.locale}"
|
16
|
+
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
private
|
20
|
+
|
21
|
+
def ordinalize_in_short
|
22
|
+
abs_number = self.to_i.abs
|
23
|
+
|
24
|
+
suffix = case I18n.locale
|
25
|
+
when :en
|
26
|
+
if (11..13).include?(abs_number % 100)
|
27
|
+
"th"
|
28
|
+
else
|
29
|
+
case abs_number % 10
|
30
|
+
when 1; "st"
|
31
|
+
when 2; "nd"
|
32
|
+
when 3; "rd"
|
33
|
+
else "th"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
when :fr
|
37
|
+
self == 1 ? "er" : "ème"
|
38
|
+
else
|
39
|
+
raise NotImplementedError, "Unknown locale #{I18n.locale}"
|
40
|
+
end
|
41
|
+
|
42
|
+
[self, suffix].join
|
43
|
+
end
|
20
44
|
end
|
21
45
|
|
data/ordinalize_full.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "ordinalize_full"
|
7
|
-
spec.version = "1.
|
7
|
+
spec.version = "1.1.0"
|
8
8
|
spec.authors = ["Cédric Félizard"]
|
9
9
|
spec.email = ["cedric@felizard.fr"]
|
10
10
|
spec.summary = %q{Turns a number into an ordinal string such as first, second, third or 1st, 2nd, 3rd.}
|
@@ -3,6 +3,8 @@ require "ordinalize_full/integer"
|
|
3
3
|
describe OrdinalizeFull do
|
4
4
|
describe "#ordinalize_in_full" do
|
5
5
|
context "with the default locale (:en)" do
|
6
|
+
before { I18n.locale = :en }
|
7
|
+
|
6
8
|
specify { expect(1.ordinalize_in_full).to eq("first") }
|
7
9
|
specify { expect(42.ordinalize_in_full).to eq("forty second") }
|
8
10
|
end
|
@@ -13,10 +15,36 @@ describe OrdinalizeFull do
|
|
13
15
|
specify { expect(1.ordinalize_in_full).to eq("premier") }
|
14
16
|
specify { expect(42.ordinalize_in_full).to eq("quarante-deuxième") }
|
15
17
|
end
|
18
|
+
|
19
|
+
it "raises for unknown locales" do
|
20
|
+
I18n.locale = :zz
|
21
|
+
expect { 1.ordinalize_in_full }.to raise_error(NotImplementedError)
|
22
|
+
end
|
16
23
|
end
|
17
24
|
|
18
25
|
describe "#ordinalize_full" do
|
19
26
|
specify { expect(1.ordinalize_full).to eq(1.ordinalize_in_full) }
|
20
27
|
end
|
28
|
+
|
29
|
+
describe "#ordinalize" do
|
30
|
+
context "with the default locale (:en)" do
|
31
|
+
before { I18n.locale = :en }
|
32
|
+
|
33
|
+
specify { expect(1.ordinalize(in_full: true)).to eq("first") }
|
34
|
+
specify { expect(1.ordinalize(in_full: false)).to eq("1st") }
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with locale = :fr" do
|
38
|
+
before { I18n.locale = :fr }
|
39
|
+
|
40
|
+
specify { expect(1.ordinalize(in_full: true)).to eq("premier") }
|
41
|
+
specify { expect(1.ordinalize(in_full: false)).to eq("1er") }
|
42
|
+
end
|
43
|
+
|
44
|
+
it "raises for unknown locales" do
|
45
|
+
I18n.locale = :zz
|
46
|
+
expect { 1.ordinalize(in_full: false) }.to raise_error(NotImplementedError)
|
47
|
+
end
|
48
|
+
end
|
21
49
|
end
|
22
50
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ordinalize_full
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cédric Félizard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|