Permalink
Browse files

First version of ordinalize!

  • Loading branch information...
1 parent bc77789 commit 4c343d56ce5761971cce7e5bc6c35ef4f6db2817 @radar committed Dec 26, 2009
Showing with 208 additions and 31 deletions.
  1. +18 −0 README.markdown
  2. +52 −29 Rakefile
  3. +1 −0 VERSION
  4. +49 −0 humanize.gemspec
  5. +49 −0 lib/ordinalize.rb
  6. +39 −2 spec/ordinalize_spec.rb
View
18 README.markdown
@@ -0,0 +1,18 @@
+# Ordinali(z|s)e
+
+From the creator who brought you the wildly popular [humanize](http://github.com/radar/humanize), comes ordinali(s|z)e!
+
+# Install
+
+ sudo gem install ordinalize
+
+# Usage
+
+ >> 1.ordinalize
+ => "first"
+
+ >> 1024.ordinalize
+ => "one thousand and twenty-fourth"
+
+ >> 1.ordinalise
+ => "first"
View
81 Rakefile
@@ -1,48 +1,71 @@
-require 'rubygems'
-require 'rake'
-
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
- gem.name = "ordinalize"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.name = "humanize"
+ gem.summary = %Q{Generates long winded string versions of numbers}
+ gem.description = %Q{Generates long winded string versions of numbers}
gem.email = "radarlistener@gmail.com"
- gem.homepage = "http://github.com/radar/ordinalize"
+ gem.homepage = "http://github.com/radar/"
gem.authors = ["Ryan Bigg"]
gem.add_development_dependency "rspec"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 fo r additional settings
end
+ Jeweler::GemcutterTasks.new
rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
-require 'spec/rake/spectask'
-Spec::Rake::SpecTask.new(:spec) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.spec_files = FileList['spec/**/*_spec.rb']
+
+begin
+ require 'spec'
+rescue LoadError
+ require 'rubygems'
+ require 'spec'
end
-Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.pattern = 'spec/**/*_spec.rb'
- spec.rcov = true
+require 'rake/rdoctask'
+require 'spec/rake/spectask'
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc "Run the specs under spec"
+Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.libs = %w(lib spec)
+ t.spec_opts << "-c"
+ t.ruby_opts << "-rubygems"
end
-task :spec => :check_dependencies
+desc 'Generate documentation for the humanize plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ByStar'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
-task :default => :spec
+task :bump => ["version:bump", :gemspec]
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- if File.exist?('VERSION')
- version = File.read('VERSION')
- else
- version = ""
+namespace :version do
+ task :read do
+ unless defined? GEM_VERSION
+ GEM_VERSION = File.read("VERSION")
+ end
end
+
+ task :bump => :read do
+ if ENV['VERSION']
+ GEM_VERSION.replace ENV['VERSION']
+ else
+ GEM_VERSION.sub!(/\d+$/) { |num| num.to_i + 1 }
+ end
+
+ File.open("VERSION", 'w') { |v| v.write GEM_VERSION }
+ end
+end
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "ordinalize #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
+task :release => :bump do
+ system %(git add VERSION *.gemspec && git commit -m "release v#{GEM_VERSION}")
+ system %(git tag -am "release v#{GEM_VERSION}" v#{GEM_VERSION})
end
View
1 VERSION
@@ -0,0 +1 @@
+0.0.0
View
49 humanize.gemspec
@@ -0,0 +1,49 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE
+# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{humanize}
+ s.version = "0.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Ryan Bigg"]
+ s.date = %q{2009-12-26}
+ s.description = %q{Generates long winded string versions of numbers}
+ s.email = %q{radarlistener@gmail.com}
+ s.extra_rdoc_files = [
+ "README.markdown"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "Rakefile",
+ "VERSION",
+ "lib/ordinalize.rb",
+ "spec/ordinalize_spec.rb",
+ "spec/spec_helper.rb"
+ ]
+ s.homepage = %q{http://github.com/radar/}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Generates long winded string versions of numbers}
+ s.test_files = [
+ "spec/ordinalize_spec.rb",
+ "spec/spec_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rspec>, [">= 0"])
+ else
+ s.add_dependency(%q<rspec>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<rspec>, [">= 0"])
+ end
+end
View
49 lib/ordinalize.rb
@@ -0,0 +1,49 @@
+begin
+ require 'rubygems'
+rescue LoadError; end
+require 'humanize'
+
+module Ordinalize
+ def ordinalize
+ humanized = self.humanize
+ # 13-19
+ humanized += "th" if humanized =~ /teen$/
+
+ # 20, 30, 40, etc.
+ humanized.gsub!(/ty$/, 'tieth')
+
+ output = conversions[humanized] if conversions[humanized]
+ output ||= split if split != ""
+ output ||= humanized
+ end
+
+ alias_method :ordinalise, :ordinalize
+
+ def split
+ humanized = self.humanize
+ parts = humanized.split("-")
+ parts[-1] = conversions[parts.last]
+ parts.join("-")
+ end
+
+ def conversions
+ {
+ "one" => "first",
+ "two" => "second",
+ "three" => "third",
+ "four" => "fourth",
+ "five" => "fifth",
+ "six" => "sixth",
+ "seven" => "seventh",
+ "eight" => "eighth",
+ "nine" => "ninth",
+ "ten" => "tenth",
+ "eleven" => "eleventh",
+ "twelve" => "twelveth",
+ }
+ end
+end
+
+class Numeric
+ include Ordinalize
+end
View
41 spec/ordinalize_spec.rb
@@ -1,7 +1,44 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Ordinalize" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+ [
+ [1, "first"],
+ [2, "second"],
+ [3, "third"],
+ [4, "fourth"],
+ [5, "fifth"],
+ [6, "sixth"],
+ [7, "seventh"],
+ [8, "eighth"],
+ [9, "ninth"],
+ [10, "tenth"],
+ [11, "eleventh"],
+ [12, "twelveth"],
+ [13, "thirteenth"],
+ [14, "fourteenth"],
+ [15, "fifteenth"],
+ [16, "sixteenth"],
+ [17, "seventeenth"],
+ [18, "eighteenth"],
+ [19, "nineteenth"],
+ [20, "twentieth"],
+ [21, "twenty-first"],
+ [22, "twenty-second"],
+ [23, "twenty-third"],
+ [24, "twenty-fourth"],
+ [25, "twenty-fifth"],
+ [26, "twenty-sixth"],
+ [27, "twenty-seventh"],
+ [28, "twenty-eighth"],
+ [29, "twenty-ninth"],
+ [1024, "one thousand and twenty-fourth"]
+ ].each do |num, expected|
+ it "#{num} should be #{expected}" do
+ num.ordinalize.should eql(expected)
+ end
+ end
+
+ it "alternative spelling" do
+ 1.ordinalise.should eql("first")
end
end

0 comments on commit 4c343d5

Please sign in to comment.