From b4b9594cc724052e049a1c8dc06b1a93e2e173dc Mon Sep 17 00:00:00 2001 From: edudepetris Date: Sun, 14 Dec 2014 12:16:48 -0300 Subject: [PATCH 1/4] Refactor of the structure following best practises --- lib/to_cardinal.rb | 71 +----------------------------- lib/to_cardinal/cardinalize.rb | 68 ++++++++++++++++++++++++++++ lib/to_cardinal/core_ext/string.rb | 7 +++ spec/spec_helper.rb | 4 +- 4 files changed, 79 insertions(+), 71 deletions(-) create mode 100644 lib/to_cardinal/cardinalize.rb create mode 100644 lib/to_cardinal/core_ext/string.rb diff --git a/lib/to_cardinal.rb b/lib/to_cardinal.rb index 5341976..9dd395b 100644 --- a/lib/to_cardinal.rb +++ b/lib/to_cardinal.rb @@ -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' diff --git a/lib/to_cardinal/cardinalize.rb b/lib/to_cardinal/cardinalize.rb new file mode 100644 index 0000000..1a68a2c --- /dev/null +++ b/lib/to_cardinal/cardinalize.rb @@ -0,0 +1,68 @@ +module ToCardinal + def self.cardinalize(str) + case str.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 + + def to_cardinal + ToCardinal.cardinalize self + end +end diff --git a/lib/to_cardinal/core_ext/string.rb b/lib/to_cardinal/core_ext/string.rb new file mode 100644 index 0000000..375d31f --- /dev/null +++ b/lib/to_cardinal/core_ext/string.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a8d291a..2b55799 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ -require 'to_cardinal' require 'coveralls' Coveralls.wear! - +require 'to_cardinal' +require 'to_cardinal/core_ext/string' From fa2e6162478ceebf08b24eac2b303c12a0c93770 Mon Sep 17 00:00:00 2001 From: edudepetris Date: Mon, 15 Dec 2014 01:08:52 -0300 Subject: [PATCH 2/4] [Refactor] super case-when --- lib/to_cardinal/cardinalize.rb | 117 ++++++++++++++++----------------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/lib/to_cardinal/cardinalize.rb b/lib/to_cardinal/cardinalize.rb index 1a68a2c..f1cb519 100644 --- a/lib/to_cardinal/cardinalize.rb +++ b/lib/to_cardinal/cardinalize.rb @@ -1,64 +1,63 @@ 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) - case str.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 + 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 From 1dd8f43760b15d1dfd1ca24b3166d9fb9832f646 Mon Sep 17 00:00:00 2001 From: edudepetris Date: Mon, 15 Dec 2014 11:26:17 -0300 Subject: [PATCH 3/4] Add changelog and update readme --- CHANGELOG.md | 12 ++++++++++++ README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7b9a8a5 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 0925c40..aa227bb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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: From a2dd3a9f001a61c050347adf8c670171b02e86aa Mon Sep 17 00:00:00 2001 From: edudepetris Date: Mon, 15 Dec 2014 11:34:24 -0300 Subject: [PATCH 4/4] Bump to 0.0.2 --- lib/to_cardinal/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/to_cardinal/version.rb b/lib/to_cardinal/version.rb index 2fcb8db..9968067 100644 --- a/lib/to_cardinal/version.rb +++ b/lib/to_cardinal/version.rb @@ -1,3 +1,3 @@ module ToCardinal - VERSION = "0.0.1" + VERSION = "0.0.2" end