numeral 0.1.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## PROJECT::GENERAL
5
+ coverage
6
+ rdoc
7
+ pkg
8
+
9
+ ## PROJECT::SPECIFIC
10
+ numeral.gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ben Rady
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = numeral
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Ben Rady. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "numeral"
8
+ gem.summary = %Q{Converts natural text to numbers}
9
+ gem.description = %Q{Converts natural text like 'twelve', '1/3' and '15' to Rational ruby numbers}
10
+ gem.email = "brady@drw.com"
11
+ gem.homepage = "http://github.com/benrady/numeral"
12
+ gem.authors = ["Ben Rady"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "numeral #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/numeral.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Numeral
2
+ def parse(text)
3
+ return parse_natural(text) if text[/[a-z|A-Z]/]
4
+ numerator, denominator = text.split('/')
5
+ return numerator.to_f if numerator.include?('.')
6
+ denominator ||= 1
7
+ Rational(numerator.to_i, denominator.to_i)
8
+ end
9
+
10
+ def parse_natural(text)
11
+ [
12
+ 'zero',
13
+ 'one',
14
+ 'two',
15
+ 'three',
16
+ 'four',
17
+ 'five',
18
+ 'six',
19
+ 'seven',
20
+ 'eight',
21
+ 'nine',
22
+ 'ten',
23
+ 'eleven',
24
+ 'twelve',
25
+ 'thirteen',
26
+ 'fourteen',
27
+ 'fifteen',
28
+ 'sixteen',
29
+ 'seventeen',
30
+ 'eighteen',
31
+ 'nineteen',
32
+ 'twenty'
33
+ ].index(text.downcase)
34
+ end
35
+ end
data/ruby.watchr ADDED
@@ -0,0 +1,59 @@
1
+ # Run me with:
2
+ #
3
+ # $ watchr specs.watchr
4
+
5
+ # --------------------------------------------------
6
+ # Convenience Methods
7
+ # --------------------------------------------------
8
+ def all_test_files
9
+ Dir['spec/**/*_spec.rb']
10
+ end
11
+
12
+ def run_test_matching(thing_to_match)
13
+ matches = all_test_files.grep(/#{thing_to_match}/i)
14
+ if matches.empty?
15
+ puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
16
+ else
17
+ run matches.join(' ')
18
+ end
19
+ end
20
+
21
+ def run(files_to_run)
22
+ puts("Running: #{files_to_run}")
23
+ system("clear;spec -cfs #{files_to_run}")
24
+ no_int_for_you
25
+ end
26
+
27
+ def run_all_tests
28
+ run(all_test_files.join(' '))
29
+ end
30
+ #
31
+ # --------------------------------------------------
32
+ # Watchr Rules
33
+ # --------------------------------------------------
34
+ watch('^spec/(.*)_spec\.rb' ) { |m| run_test_matching(m[1]) }
35
+ watch('^lib/(.*)\.rb' ) { |m| run_test_matching(m[1]) }
36
+ watch('^spec/spec_helper\.rb') { run_all_tests }
37
+ # --------------------------------------------------
38
+ # Signal Handling
39
+ # --------------------------------------------------
40
+
41
+ def no_int_for_you
42
+ @sent_an_int = nil
43
+ end
44
+
45
+ Signal.trap 'INT' do
46
+ if @sent_an_int then
47
+ puts " A second INT? Ok, I get the message. Shutting down now."
48
+ exit
49
+ else
50
+ puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
51
+ @sent_an_int = true
52
+ Kernel.sleep 1.5
53
+ run_all_tests
54
+ end
55
+ end
56
+
57
+ run_all_tests
58
+
59
+ # vim:ft=ruby
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'rational'
3
+
4
+ describe Numeral do
5
+ describe "parses" do
6
+ include Numeral
7
+
8
+ it "digits" do
9
+ parse("1").should == 1
10
+ parse("12").should == 12
11
+ end
12
+
13
+ it "fractions" do
14
+ parse("2/3").should == Rational(2,3)
15
+ parse("3/4").should == 0.75
16
+ end
17
+
18
+ it "decimals" do
19
+ parse("0.5").should == 0.5
20
+ parse(".5").should == 0.5
21
+ end
22
+
23
+ it "natural words" do
24
+ parse("zero").should == 0
25
+ parse("One").should == 1
26
+ parse("TEN").should == 10
27
+ parse("thirteen").should == 13
28
+ parse("twenty").should == 20
29
+ end
30
+ end
31
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'numeral'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numeral
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Ben Rady
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-16 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Converts natural text like 'twelve', '1/3' and '15' to Rational ruby numbers
35
+ email: brady@drw.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/numeral.rb
51
+ - ruby.watchr
52
+ - spec/numeral_spec.rb
53
+ - spec/spec.opts
54
+ - spec/spec_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/benrady/numeral
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.6
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Converts natural text to numbers
85
+ test_files:
86
+ - spec/numeral_spec.rb
87
+ - spec/spec_helper.rb