to_cardinal 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +55 -1
- data/lib/to_cardinal.rb +2 -69
- data/lib/to_cardinal/cardinalize.rb +67 -0
- data/lib/to_cardinal/core_ext/string.rb +7 -0
- data/lib/to_cardinal/version.rb +1 -1
- data/spec/lib/to_cardinal_spec.rb +6 -15
- data/spec/spec_helper.rb +3 -2
- data/to_cardinal.gemspec +2 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25caab36cc87fd2583617c4d1fd2a7aed431646f
|
4
|
+
data.tar.gz: d1f5eb949841efb33065650a94b86298f405af04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72f239da2537de7160f90fba0d1621c86107fa1c01e0556035feb7c5a0717507aa8e3644d9bfac4ae7235a7200e9909a4b9d12d996fad3872297f426ca849c04
|
7
|
+
data.tar.gz: cb7867b578188f6274f84bdfe2f0e5fd584fcb47ecf9557e96421114e605957981715d93ac00bf27ad2838e3e81bd61b3ff190e6466d59522f72fa924ca3f922
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# 0.0.2
|
2
|
+
|
3
|
+
* added CHANGELOG
|
4
|
+
* [ENHANCEMENT] returns default value nil
|
5
|
+
* [ENHANCEMENT] added more tests
|
6
|
+
* [ENHANCEMENT] supports conversions from 1 to 100
|
7
|
+
* [ENHANCEMENT] changes super case-when by more programmatic way
|
8
|
+
* [ENHANCEMENT] supports different kind of uses
|
9
|
+
|
10
|
+
# 0.0.1
|
11
|
+
|
12
|
+
* initial version
|
data/README.md
CHANGED
@@ -2,10 +2,11 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/edudepetris/to_cardinal.svg?branch=master)](https://travis-ci.org/edudepetris/to_cardinal)
|
4
4
|
[![Coverage Status](https://img.shields.io/coveralls/edudepetris/to_cardinal.svg)](https://coveralls.io/r/edudepetris/to_cardinal)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/to_cardinal.svg)](http://badge.fury.io/rb/to_cardinal)
|
5
6
|
|
6
7
|
|
7
8
|
Ruby gem which will allow to convert from ordinal numbers to cardinal numbers.
|
8
|
-
|
9
|
+
It only supports ordinals from 1 to 100.
|
9
10
|
|
10
11
|
|
11
12
|
## Installation
|
@@ -24,15 +25,62 @@ Or install it yourself as:
|
|
24
25
|
|
25
26
|
## Usage
|
26
27
|
|
28
|
+
Please use which ever is most comfortable:
|
29
|
+
|
30
|
+
You can use _ToCardinal_ without include it in a class.
|
31
|
+
Just require ```to_cardinal``` into your Ruby code and use ```cardinalize``` method from Module
|
32
|
+
|
27
33
|
```ruby
|
28
34
|
require 'to_cardinal'
|
29
35
|
|
36
|
+
# some ordinal numbers to cardinals
|
37
|
+
ToCardinal.cardinalize 'first' # => 1
|
38
|
+
ToCardinal.cardinalize '1st' # => 1
|
39
|
+
ToCardinal.cardinalize 'fourth' # => 4
|
40
|
+
ToCardinal.cardinalize '4th' # => 4
|
41
|
+
```
|
42
|
+
You can include _ToCardinal_ in String Ruby’s core library.
|
43
|
+
Just require ```to_cardinal/core_ext/string``` into your Ruby code and use ```String#to_cardinal``` method.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'to_cardinal/core_ext/string'
|
47
|
+
|
30
48
|
# some ordinal numbers to cardinals
|
31
49
|
'first'.to_cardinal # => 1
|
32
50
|
'1st'.to_cardinal # => 1
|
33
51
|
'fourth'.to_cardinal # => 4
|
34
52
|
'4th'.to_cardinal # => 4
|
35
53
|
|
54
|
+
```
|
55
|
+
You can include _ToCardinal_ in a String's sub-class.
|
56
|
+
Just require ```to_cardinal``` into your Ruby code and then ```include``` it in your _sub-class_
|
57
|
+
```ruby
|
58
|
+
require 'to_cardinal'
|
59
|
+
|
60
|
+
class A < String
|
61
|
+
include ToCardinal
|
62
|
+
...
|
63
|
+
end
|
64
|
+
|
65
|
+
# some ordinal numbers to cardinals
|
66
|
+
A.new('first').to_cardinal # => 1
|
67
|
+
A.new('1st').to_cardinal # => 1
|
68
|
+
A.new('fourth').to_cardinal # => 4
|
69
|
+
A.new('4th').to_cardinal # => 4
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
If you are in **Ruby on Rails** and you want to use _ToCardinal_ like ```String#to_cardinal```. You can create an ```initializer``` file like this:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
# your_project/config/initializers/to_cardinal.rb
|
77
|
+
require 'to_cardinal/core_ext/string'
|
78
|
+
|
79
|
+
# And then you use it
|
80
|
+
'first'.to_cardinal # => 1
|
81
|
+
'1st'.to_cardinal # => 1
|
82
|
+
'fourth'.to_cardinal # => 4
|
83
|
+
'4th'.to_cardinal # => 4
|
36
84
|
```
|
37
85
|
|
38
86
|
## Contributing
|
@@ -42,3 +90,9 @@ require 'to_cardinal'
|
|
42
90
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
91
|
4. Push to the branch (`git push origin my-new-feature`)
|
44
92
|
5. Create a new Pull Request
|
93
|
+
|
94
|
+
## License
|
95
|
+
Distributed under the [MIT license](https://github.com/edudepetris/to_cardinal/blob/master/LICENSE.txt)
|
96
|
+
|
97
|
+
-------
|
98
|
+
First of all, thank ruby community for your help! :punch:
|
data/lib/to_cardinal.rb
CHANGED
@@ -1,69 +1,2 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
module ToCardinal
|
4
|
-
|
5
|
-
class ::String
|
6
|
-
def to_cardinal
|
7
|
-
case self.downcase
|
8
|
-
when 'first', '1st'
|
9
|
-
1
|
10
|
-
when 'second', '2nd'
|
11
|
-
2
|
12
|
-
when 'third', '3rd'
|
13
|
-
3
|
14
|
-
when 'fourth', '4th'
|
15
|
-
4
|
16
|
-
when 'fifth', '5th'
|
17
|
-
5
|
18
|
-
when 'sixth', '6th'
|
19
|
-
6
|
20
|
-
when 'seventh', '7th'
|
21
|
-
7
|
22
|
-
when 'eighth', '8th'
|
23
|
-
8
|
24
|
-
when 'ninth', '9th'
|
25
|
-
9
|
26
|
-
when 'tenth', '10th'
|
27
|
-
10
|
28
|
-
when 'eleventh', '11th'
|
29
|
-
11
|
30
|
-
when 'twelfth', '12th'
|
31
|
-
12
|
32
|
-
when 'thirteenth', '13th'
|
33
|
-
13
|
34
|
-
when 'fourteenth', '14th'
|
35
|
-
14
|
36
|
-
when 'fifteenth', '15th'
|
37
|
-
15
|
38
|
-
when 'sixteenth', '16th'
|
39
|
-
16
|
40
|
-
when 'seventeenth', '17th'
|
41
|
-
17
|
42
|
-
when 'eighteenth', '18th'
|
43
|
-
18
|
44
|
-
when 'nineteenth', '19th'
|
45
|
-
19
|
46
|
-
when 'twentieth', '20th'
|
47
|
-
20
|
48
|
-
when 'thirtieth', '30th'
|
49
|
-
30
|
50
|
-
when 'fortieth', '40th'
|
51
|
-
40
|
52
|
-
when 'fiftieth', '50th'
|
53
|
-
50
|
54
|
-
when 'sixtieth', '60th'
|
55
|
-
60
|
56
|
-
when 'seventieth', '70th'
|
57
|
-
70
|
58
|
-
when 'eightieth', '80th'
|
59
|
-
80
|
60
|
-
when 'ninetieth', '90th'
|
61
|
-
90
|
62
|
-
when 'hundredth', '100th'
|
63
|
-
100
|
64
|
-
else
|
65
|
-
-1
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
1
|
+
require 'to_cardinal/version'
|
2
|
+
require 'to_cardinal/cardinalize'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module ToCardinal
|
2
|
+
EXPLICITS = {
|
3
|
+
'first' => 1,
|
4
|
+
'second' => 2,
|
5
|
+
'third' => 3,
|
6
|
+
'ninth' => 9,
|
7
|
+
'eleventh' => 11,
|
8
|
+
'twelfth' => 12,
|
9
|
+
'twentieth' => 20,
|
10
|
+
'thirtieth' => 30,
|
11
|
+
'fortieth' => 40,
|
12
|
+
'fiftieth' => 50,
|
13
|
+
'sixtieth' => 60,
|
14
|
+
'seventieth' => 70,
|
15
|
+
'eightieth' => 80,
|
16
|
+
'ninetieth' => 90,
|
17
|
+
'one hundredth' => 100
|
18
|
+
}
|
19
|
+
|
20
|
+
REGULARS = {
|
21
|
+
'thir' => 3,
|
22
|
+
'four' => 4,
|
23
|
+
'fif' => 5,
|
24
|
+
'six' => 6,
|
25
|
+
'seven' => 7,
|
26
|
+
'eigh' => 8,
|
27
|
+
'nine' => 9,
|
28
|
+
'ten' => 10
|
29
|
+
}
|
30
|
+
|
31
|
+
TENS = {
|
32
|
+
'twenty' => 20,
|
33
|
+
'thirty' => 30,
|
34
|
+
'forty' => 40,
|
35
|
+
'fifty' => 50,
|
36
|
+
'sixty' => 60,
|
37
|
+
'seventy' => 70,
|
38
|
+
'eighty' => 80,
|
39
|
+
'ninety' => 90
|
40
|
+
}
|
41
|
+
|
42
|
+
EXPLICIT_MATCHES = Hash[REGULARS.map {|k, v| [k.to_s + 'th', v] }].merge EXPLICITS
|
43
|
+
TENS_MATCH = /(#{TENS.keys.join '|'})-/
|
44
|
+
ORDINAL = /^(\d+)(?:st|nd|rd|th)$/
|
45
|
+
|
46
|
+
def self.cardinalize(str)
|
47
|
+
str.downcase!
|
48
|
+
|
49
|
+
ordinal = str[ORDINAL, 1]
|
50
|
+
explicit_matches = EXPLICIT_MATCHES[str]
|
51
|
+
regular_match = str[/^(.+)teenth$/, 1]
|
52
|
+
|
53
|
+
return ordinal.to_i if ordinal
|
54
|
+
return explicit_matches if explicit_matches
|
55
|
+
return 10 + REGULARS[regular_match] if regular_match
|
56
|
+
|
57
|
+
if tens = str[TENS_MATCH, 1]
|
58
|
+
sum = TENS[tens]
|
59
|
+
str.sub! "#{tens}-", ''
|
60
|
+
EXPLICIT_MATCHES.has_key?(str) ? sum + EXPLICIT_MATCHES[str] : nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_cardinal
|
65
|
+
ToCardinal.cardinalize self
|
66
|
+
end
|
67
|
+
end
|
data/lib/to_cardinal/version.rb
CHANGED
@@ -1,19 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ToCardinal do
|
4
|
-
|
5
|
-
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
6
|
-
19, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|
4
|
+
Linguistics.use :en
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
seventeenth eighteenth nineteenth twentieth thirtieth fortieth fiftieth
|
12
|
-
sixtieth seventieth eightieth ninetieth hundredth'
|
13
|
-
|
14
|
-
abbreviation_ordinal = %w'
|
15
|
-
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
|
16
|
-
16th 17th 18th 19th 20th 30th 40th 50th 60th 70th 80th 90th 100th'
|
6
|
+
cardinals = (1..100).to_a
|
7
|
+
full_ordinals = cardinals.map { |c| c.en.numwords.en.ordinal }
|
8
|
+
abbreviation_ordinal = cardinals.map { |c| c.en.ordinal }
|
17
9
|
|
18
10
|
full_ordinals.each_with_index do |ordinal, index|
|
19
11
|
cardinal = cardinals[index]
|
@@ -31,8 +23,7 @@ describe ToCardinal do
|
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
34
|
-
it 'converts
|
35
|
-
expect('anything'.to_cardinal).to
|
26
|
+
it 'converts to nil when it is not an ordinal' do
|
27
|
+
expect('anything'.to_cardinal).to be_nil
|
36
28
|
end
|
37
|
-
|
38
29
|
end
|
data/spec/spec_helper.rb
CHANGED
data/to_cardinal.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'to_cardinal/version'
|
@@ -18,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
20
|
spec.require_paths = ["lib"]
|
20
21
|
|
22
|
+
spec.add_development_dependency "linguistics", "2.0.2"
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
24
|
spec.add_development_dependency "rake", "~> 10.4"
|
23
25
|
spec.add_development_dependency "rspec", "~> 3.1"
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_cardinal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- edu depetris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: linguistics
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.2
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,11 +90,14 @@ files:
|
|
76
90
|
- ".coveralls.yml"
|
77
91
|
- ".gitignore"
|
78
92
|
- ".travis.yml"
|
93
|
+
- CHANGELOG.md
|
79
94
|
- Gemfile
|
80
95
|
- LICENSE.txt
|
81
96
|
- README.md
|
82
97
|
- Rakefile
|
83
98
|
- lib/to_cardinal.rb
|
99
|
+
- lib/to_cardinal/cardinalize.rb
|
100
|
+
- lib/to_cardinal/core_ext/string.rb
|
84
101
|
- lib/to_cardinal/version.rb
|
85
102
|
- spec/lib/to_cardinal_spec.rb
|
86
103
|
- spec/spec_helper.rb
|