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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad3aca4a46aa2509add50b4e3ccb3cc48c58059f
4
- data.tar.gz: ff6d913c3efef11eb3f53fea9dea99fc553dedd5
3
+ metadata.gz: d4594f97093bb7853d4c0e15b7e5aa6bf72905ec
4
+ data.tar.gz: f9c7b05ae930ab830f1cc1a8ae3697eefc7bd768
5
5
  SHA512:
6
- metadata.gz: 6942fb0227b949b39f4882e74932c39d1b47c46e98d6e9e60afb5494a6af86d13ae27a1516b6af62e3cc2093389b66881cb1bd7cd427c7d770b34e9f5a9d3576
7
- data.tar.gz: f05e00a98fc180c0ac7f5730ced2de96cccdbcce36904e5aa1f0e3f7e0ff2b6ba659eae7f7fda136d1aea8a8bc101d0c88c90edd8ddfd53008aee756fe77e79c
6
+ metadata.gz: 36ad759eb60b6cc789ec181cd4b8a1641d7eaaabd7117b53fda9f00cea93c0dc0fff404c6b6f45d1f607ecfc28174e7af1619f5817ac43fb20f33e1f2f40f8ff
7
+ data.tar.gz: 61b6b76999c5bc5b2759e9664b91464059c5691038a0ef77d94e206511b0b804d5e9bc2a0b60657f04dc03609828bb2609488dbd004a91b5cf7f0f8f854bc6f1
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
3
  - 2.1
5
4
  notifications:
6
5
  email:
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 25 lines of code
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
@@ -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
- if in_full
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
- I18n.t("ordinalize_full.n_#{self}")
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
- alias_method :ordinalize_full, :ordinalize_in_full
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
 
@@ -1,6 +1,6 @@
1
1
  require "ordinalize_full"
2
2
 
3
3
  class Integer
4
- include OrdinalizeFull
4
+ prepend OrdinalizeFull
5
5
  end
6
6
 
@@ -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.0.0"
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.0.0
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-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n