Skip to content

Commit

Permalink
Merge a2dd3a9 into 70ebdcb
Browse files Browse the repository at this point in the history
  • Loading branch information
edudepetris committed Dec 15, 2014
2 parents 70ebdcb + a2dd3a9 commit ace2247
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 73 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 0.0.2

* added CHANGELOG
* [ENHANCEMENT] returns default value nil
* [ENHANCEMENT] added more tests
* [ENHANCEMENT] supports conversions from 1 to 100
* [ENHANCEMENT] changes super case-when by more programmatic way
* [ENHANCEMENT] supports different kind of uses

# 0.0.1

* initial version
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


Ruby gem which will allow to convert from ordinal numbers to cardinal numbers.

It only supports ordinals from 1 to 100.


## Installation
Expand All @@ -25,15 +25,62 @@ Or install it yourself as:

## Usage

Please use which ever is most comfortable:

You can use _ToCardinal_ without include it in a class.
Just require ```to_cardinal``` into your Ruby code and use ```cardinalize``` method from Module

```ruby
require 'to_cardinal'

# some ordinal numbers to cardinals
ToCardinal.cardinalize 'first' # => 1
ToCardinal.cardinalize '1st' # => 1
ToCardinal.cardinalize 'fourth' # => 4
ToCardinal.cardinalize '4th' # => 4
```
You can include _ToCardinal_ in String Ruby’s core library.
Just require ```to_cardinal/core_ext/string``` into your Ruby code and use ```String#to_cardinal``` method.

```ruby
require 'to_cardinal/core_ext/string'

# some ordinal numbers to cardinals
'first'.to_cardinal # => 1
'1st'.to_cardinal # => 1
'fourth'.to_cardinal # => 4
'4th'.to_cardinal # => 4

```
You can include _ToCardinal_ in a String's sub-class.
Just require ```to_cardinal``` into your Ruby code and then ```include``` it in your _sub-class_
```ruby
require 'to_cardinal'

class A < String
include ToCardinal
...
end

# some ordinal numbers to cardinals
A.new('first').to_cardinal # => 1
A.new('1st').to_cardinal # => 1
A.new('fourth').to_cardinal # => 4
A.new('4th').to_cardinal # => 4

```

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:

```ruby
# your_project/config/initializers/to_cardinal.rb
require 'to_cardinal/core_ext/string'

# And then you use it
'first'.to_cardinal # => 1
'1st'.to_cardinal # => 1
'fourth'.to_cardinal # => 4
'4th'.to_cardinal # => 4
```

## Contributing
Expand All @@ -43,3 +90,9 @@ require 'to_cardinal'
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## License
Distributed under the [MIT license](https://github.com/edudepetris/to_cardinal/blob/master/LICENSE.txt)

-------
First of all, thank ruby community for your help! :punch:
71 changes: 2 additions & 69 deletions lib/to_cardinal.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,2 @@
require "to_cardinal/version"

module ToCardinal

class ::String
def to_cardinal
case self.downcase
when 'first', '1st'
1
when 'second', '2nd'
2
when 'third', '3rd'
3
when 'fourth', '4th'
4
when 'fifth', '5th'
5
when 'sixth', '6th'
6
when 'seventh', '7th'
7
when 'eighth', '8th'
8
when 'ninth', '9th'
9
when 'tenth', '10th'
10
when 'eleventh', '11th'
11
when 'twelfth', '12th'
12
when 'thirteenth', '13th'
13
when 'fourteenth', '14th'
14
when 'fifteenth', '15th'
15
when 'sixteenth', '16th'
16
when 'seventeenth', '17th'
17
when 'eighteenth', '18th'
18
when 'nineteenth', '19th'
19
when 'twentieth', '20th'
20
when 'thirtieth', '30th'
30
when 'fortieth', '40th'
40
when 'fiftieth', '50th'
50
when 'sixtieth', '60th'
60
when 'seventieth', '70th'
70
when 'eightieth', '80th'
80
when 'ninetieth', '90th'
90
when 'hundredth', '100th'
100
else
-1
end
end
end
end
require 'to_cardinal/version'
require 'to_cardinal/cardinalize'
67 changes: 67 additions & 0 deletions lib/to_cardinal/cardinalize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module ToCardinal
EXPLICITS = {
'first' => 1,
'second' => 2,
'third' => 3,
'ninth' => 9,
'eleventh' => 11,
'twelfth' => 12,
'twentieth' => 20,
'thirtieth' => 30,
'fortieth' => 40,
'fiftieth' => 50,
'sixtieth' => 60,
'seventieth' => 70,
'eightieth' => 80,
'ninetieth' => 90,
'one hundredth' => 100
}

REGULARS = {
'thir' => 3,
'four' => 4,
'fif' => 5,
'six' => 6,
'seven' => 7,
'eigh' => 8,
'nine' => 9,
'ten' => 10
}

TENS = {
'twenty' => 20,
'thirty' => 30,
'forty' => 40,
'fifty' => 50,
'sixty' => 60,
'seventy' => 70,
'eighty' => 80,
'ninety' => 90
}

EXPLICIT_MATCHES = Hash[REGULARS.map {|k, v| [k.to_s + 'th', v] }].merge EXPLICITS
TENS_MATCH = /(#{TENS.keys.join '|'})-/
ORDINAL = /^(\d+)(?:st|nd|rd|th)$/

def self.cardinalize(str)
str.downcase!

ordinal = str[ORDINAL, 1]
explicit_matches = EXPLICIT_MATCHES[str]
regular_match = str[/^(.+)teenth$/, 1]

return ordinal.to_i if ordinal
return explicit_matches if explicit_matches
return 10 + REGULARS[regular_match] if regular_match

if tens = str[TENS_MATCH, 1]
sum = TENS[tens]
str.sub! "#{tens}-", ''
EXPLICIT_MATCHES.has_key?(str) ? sum + EXPLICIT_MATCHES[str] : nil
end
end

def to_cardinal
ToCardinal.cardinalize self
end
end
7 changes: 7 additions & 0 deletions lib/to_cardinal/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'to_cardinal'

if String.method_defined? :to_cardinal
$stderr.puts " **WARNING: Possible conflict with ToCardinal extension:\ \b
String#to_cardinal already exits."
end
String.send :include, ToCardinal
2 changes: 1 addition & 1 deletion lib/to_cardinal/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ToCardinal
VERSION = "0.0.1"
VERSION = "0.0.2"
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'to_cardinal'
require 'coveralls'
Coveralls.wear!

require 'to_cardinal'
require 'to_cardinal/core_ext/string'

0 comments on commit ace2247

Please sign in to comment.