deordinalize 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +32 -0
- data/Rakefile +5 -0
- data/deordinalize.gemspec +24 -0
- data/lib/deordinalize/version.rb +3 -0
- data/lib/deordinalize.rb +80 -0
- data/spec/deordinalize/deordinalize_spec.rb +30 -0
- data/spec/spec_helper.rb +2 -0
- metadata +104 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
deordinalize
|
2
|
+
============
|
3
|
+
|
4
|
+
A small gem to 'deordinalize' strings into the integers they reference.
|
5
|
+
|
6
|
+
Installation:
|
7
|
+
-------------
|
8
|
+
|
9
|
+
`gem install deordinalize` and require it. `String` now has the `deordinalize` method.
|
10
|
+
|
11
|
+
Usage:
|
12
|
+
------
|
13
|
+
|
14
|
+
**deordinalize**, for the time being, only supports ordinals from 1 to 100.
|
15
|
+
|
16
|
+
We can deordinalize numeric ordinals:
|
17
|
+
|
18
|
+
'1st' # => 1
|
19
|
+
'11th' # => 11
|
20
|
+
'99th' # => 99
|
21
|
+
|
22
|
+
Or we can deordinalize more verbose ordinals:
|
23
|
+
|
24
|
+
'first'.deordinalize # => 1
|
25
|
+
'eleventh'.deordinalize # => 11
|
26
|
+
'ninety-ninth'.deordinalize # => 99
|
27
|
+
|
28
|
+
This is meant to be hella useful in cucumber steps, like:
|
29
|
+
|
30
|
+
When /^I click on the (.+) item in the list$/ do |ordinal|
|
31
|
+
page.find( :xpath, "//li[ordinal.deordinalize - 1]" ).click
|
32
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "deordinalize/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "deordinalize"
|
7
|
+
s.version = Deordinalize::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jeremy Ruppel"]
|
10
|
+
s.email = ["jeremy.ruppel@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/jeremyruppel/deordinalize"
|
12
|
+
s.summary = %q{Small gem to 'deordinalize' strings into the integers they reference.}
|
13
|
+
s.description = %q{Small gem to 'deordinalize' strings into the integers they reference. Intended to be hella useful in cucumber steps.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "deordinalize"
|
16
|
+
|
17
|
+
s.add_development_dependency "rspec"
|
18
|
+
s.add_development_dependency "linguistics"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
data/lib/deordinalize.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
module Deordinalize
|
2
|
+
|
3
|
+
EXPLICITS = {
|
4
|
+
'first' => 1,
|
5
|
+
'second' => 2,
|
6
|
+
'third' => 3,
|
7
|
+
'ninth' => 9,
|
8
|
+
'eleventh' => 11,
|
9
|
+
'twelfth' => 12,
|
10
|
+
'twentieth' => 20,
|
11
|
+
'thirtieth' => 30,
|
12
|
+
'fortieth' => 40,
|
13
|
+
'fiftieth' => 50,
|
14
|
+
'sixtieth' => 60,
|
15
|
+
'seventieth' => 70,
|
16
|
+
'eightieth' => 80,
|
17
|
+
'ninetieth' => 90,
|
18
|
+
'one hundredth' => 100
|
19
|
+
}
|
20
|
+
|
21
|
+
REGULARS = {
|
22
|
+
'thir' => 3,
|
23
|
+
'four' => 4,
|
24
|
+
'fif' => 5,
|
25
|
+
'six' => 6,
|
26
|
+
'seven' => 7,
|
27
|
+
'eigh' => 8,
|
28
|
+
'nine' => 9,
|
29
|
+
'ten' => 10
|
30
|
+
}
|
31
|
+
|
32
|
+
TENS = {
|
33
|
+
'twenty' => 20,
|
34
|
+
'thirty' => 30,
|
35
|
+
'forty' => 40,
|
36
|
+
'fifty' => 50,
|
37
|
+
'sixty' => 60,
|
38
|
+
'seventy' => 70,
|
39
|
+
'eighty' => 80,
|
40
|
+
'ninety' => 90
|
41
|
+
}
|
42
|
+
|
43
|
+
TENS_MATCH = /(#{TENS.keys.join '|'})-/
|
44
|
+
|
45
|
+
ORDINAL = /^(\d+)(?:st|nd|rd|th)$/
|
46
|
+
|
47
|
+
def deordinalize
|
48
|
+
|
49
|
+
sum = 0
|
50
|
+
|
51
|
+
# if we have a tens prefix, assign it to the sum and remove it from our self
|
52
|
+
if tens = self[TENS_MATCH, 1]
|
53
|
+
sum = TENS[tens]
|
54
|
+
self.sub! "#{tens}-", ''
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO try refactoring this block out into its own method
|
58
|
+
|
59
|
+
# if we have a numeric ordinal, pretty much just need to strip out the number
|
60
|
+
if ordinal = self[ORDINAL, 1]
|
61
|
+
ordinal.to_i
|
62
|
+
|
63
|
+
# if we have an explicit ordinal, find it in the map
|
64
|
+
elsif explicit = EXPLICITS[self]
|
65
|
+
sum + explicit
|
66
|
+
|
67
|
+
# if we have a teen, find the regular prefix and add ten
|
68
|
+
elsif regular = self[/^(.+)teenth$/, 1]
|
69
|
+
10 + REGULARS[regular]
|
70
|
+
|
71
|
+
# if we have a regular prefix, add it to the sum
|
72
|
+
elsif regular = self[/^(.+)th$/, 1]
|
73
|
+
sum + REGULARS[ regular ]
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
String.send :include, Deordinalize
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "a string" do
|
4
|
+
it "should respond to deordinalize" do
|
5
|
+
"hello".should respond_to(:deordinalize)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "when not an ordinal" do
|
9
|
+
it "should return nil" do
|
10
|
+
"hello".deordinalize.should be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Linguistics.use :en
|
15
|
+
|
16
|
+
cases = (1..100).to_a
|
17
|
+
|
18
|
+
cases.map { |i| [ i.en.ordinal, i ] }.each do |ordinal, number|
|
19
|
+
it "should deordinalize '#{ordinal}' to #{number}" do
|
20
|
+
ordinal.deordinalize.should == number
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
cases.map { |i| [ i.en.numwords.en.ordinal, i ] }.each do |ordinal, number|
|
25
|
+
it "should deordinalize '#{ordinal}' to #{number}" do
|
26
|
+
ordinal.deordinalize.should == number
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deordinalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeremy Ruppel
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-22 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: linguistics
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Small gem to 'deordinalize' strings into the integers they reference. Intended to be hella useful in cucumber steps.
|
50
|
+
email:
|
51
|
+
- jeremy.ruppel@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- deordinalize.gemspec
|
64
|
+
- lib/deordinalize.rb
|
65
|
+
- lib/deordinalize/version.rb
|
66
|
+
- spec/deordinalize/deordinalize_spec.rb
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: https://github.com/jeremyruppel/deordinalize
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: deordinalize
|
98
|
+
rubygems_version: 1.5.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Small gem to 'deordinalize' strings into the integers they reference.
|
102
|
+
test_files:
|
103
|
+
- spec/deordinalize/deordinalize_spec.rb
|
104
|
+
- spec/spec_helper.rb
|