english_number 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +74 -0
- data/english_number.gemspec +3 -3
- data/lib/digits/three_digits.rb +44 -0
- data/lib/digits/two_digits.rb +5 -5
- data/lib/english_number.rb +4 -1
- data/lib/modules/compute.rb +2 -6
- data/lib/modules/languages/english_numbers.yml +2 -1
- data/lib/modules/translate.rb +27 -5
- data/spec/integrations/english_number_int_spec.rb +120 -38
- data/spec/unit_tests/compute_spec.rb +3 -7
- data/spec/unit_tests/english_number_unit_spec.rb +0 -4
- data/spec/unit_tests/three_digits_spec.rb +61 -0
- data/spec/unit_tests/translate_spec.rb +31 -0
- data/spec/unit_tests/two_digits_spec.rb +61 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad1e9753922557166b6fd050738d7dec6a4d0b70
|
4
|
+
data.tar.gz: fa8ce30e3cc5af982efa6d566dac4f6b360a3b4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96b2a71799d08d7702f97d7b1d31cb997f96a5a72a033d810928752f209a7c27dc811ac2458fff1f075697e3eeda2f8cd4ff6fe10185f58ea8ed17b6fb753d23
|
7
|
+
data.tar.gz: f7c05cf0299f4abf37a01e14d77cbaa37906d903e74f66071c7fbe244c950fef85be8fbb017d191517a7d7604e707c05373900cb39218c68b173bdc40b0f2c88
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
## 0.0.3 (May 15, 2016)
|
2
|
+
|
3
|
+
* Added feature - gem works for three-digits long numbers (limit still set on 2 digits though, it can be changed in module ::Translate)
|
4
|
+
* Added specs to cover all classes' and modules' public methods
|
5
|
+
* Refactored whole code
|
6
|
+
|
7
|
+
## 0.0.2 (May 13, 2016)
|
8
|
+
|
9
|
+
* Created working gem, limited to two-digit long numbers
|
10
|
+
* Added specs and simplecov (100% coverage)
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
== english_number
|
2
|
+
|
3
|
+
<!--TODO:-->
|
4
|
+
<!--{<img src="https://badge.fury.io/rb/numbers_and_words.svg?branch=master" alt="Gem Version" />}[http://badge.fury.io/rb/numbers_and_words]-->
|
5
|
+
<!--{<img src="https://gemnasium.com/kslazarev/numbers_and_words.svg?branch=master" alt="Dependency Status" />}[https://gemnasium.com/kslazarev/numbers_and_words]-->
|
6
|
+
<!--{<img src="https://codeclimate.com/github/kslazarev/numbers_and_words.svg?branch=master" />}[https://codeclimate.com/github/kslazarev/numbers_and_words]-->
|
7
|
+
<!--{<img src="https://secure.travis-ci.org/kslazarev/numbers_and_words.svg?branch=master" />}[http://travis-ci.org/kslazarev/numbers_and_words]-->
|
8
|
+
<!--{<img src="https://coveralls.io/repos/kslazarev/numbers_and_words/badge.svg?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/kslazarev/numbers_and_words]-->
|
9
|
+
|
10
|
+
Convert numbers to words in English. Limit of digits handled can be set. For now works for up to 3 digits. Additionally EnglishNumber objects can be added, subtracted, multiplied and divided.
|
11
|
+
|
12
|
+
|
13
|
+
== Supported Languages
|
14
|
+
|
15
|
+
* English [en]
|
16
|
+
|
17
|
+
|
18
|
+
== Examples
|
19
|
+
|
20
|
+
EnglishNumber.new(5).in_english
|
21
|
+
=> "five"
|
22
|
+
|
23
|
+
EnglishNumber.new(65).in_english
|
24
|
+
=> "sixty-five"
|
25
|
+
|
26
|
+
EnglishNumber.new(100).in_english
|
27
|
+
=> nil
|
28
|
+
|
29
|
+
EnglishNumber.new(-100).in_english
|
30
|
+
=> nil
|
31
|
+
|
32
|
+
EnglishNumber.new(78.4444).in_english
|
33
|
+
=> "seventy-eight"
|
34
|
+
|
35
|
+
EnglishNumber.new(-80).in_english
|
36
|
+
=> "minus-eighty"
|
37
|
+
|
38
|
+
(EnglishNumber.new(80) + EnglishNumber.new(13)).in_english
|
39
|
+
=> "ninety-three"
|
40
|
+
|
41
|
+
(EnglishNumber.new(74) - EnglishNumber.new(35)).in_english
|
42
|
+
=> "thirty-nine"
|
43
|
+
|
44
|
+
(EnglishNumber.new(-80) * EnglishNumber.new(0.5)).in_english
|
45
|
+
=> "forty"
|
46
|
+
|
47
|
+
(EnglishNumber.new(50) / EnglishNumber.new(-2)).in_english
|
48
|
+
=> "minus-twenty-five"
|
49
|
+
|
50
|
+
|
51
|
+
== Requirements
|
52
|
+
|
53
|
+
* 2.3.0 <= Ruby
|
54
|
+
|
55
|
+
|
56
|
+
== Installation
|
57
|
+
|
58
|
+
gem install english_number
|
59
|
+
|
60
|
+
== License
|
61
|
+
|
62
|
+
MIT License
|
63
|
+
|
64
|
+
== Changes
|
65
|
+
|
66
|
+
See CHANGELOG.md for last changes.
|
67
|
+
|
68
|
+
Fork the project. Make your feature addition or bug fix with tests.
|
69
|
+
|
70
|
+
Send a pull request.
|
71
|
+
|
72
|
+
== Contacts
|
73
|
+
|
74
|
+
* Piotr Brych (mailto:pbrych@gmail.com)
|
data/english_number.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'english_number'
|
3
|
-
s.version = '0.0.
|
4
|
-
s.date = '2016-05-
|
3
|
+
s.version = '0.0.3'
|
4
|
+
s.date = '2016-05-15'
|
5
5
|
s.summary = "Converting English numbers to strings"
|
6
6
|
s.description = "Example: EnglishNumber.new(63).in_english # => 'sixty-three'.\n
|
7
7
|
EnglishNumber objects can be added, subtracted, multiplied, divided.\n
|
8
|
-
For now gem is deliberately limited to numbers maximum
|
8
|
+
For now gem is deliberately limited to numbers maximum three digits long.\n"
|
9
9
|
|
10
10
|
s.authors = ["Piotr Brych"]
|
11
11
|
s.email = 'pbrych@gmail.com'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'modules/compute'
|
2
|
+
require 'modules/translate'
|
3
|
+
|
4
|
+
class ThreeDigits
|
5
|
+
include Compute
|
6
|
+
include Translate
|
7
|
+
|
8
|
+
def self.run(num)
|
9
|
+
new(num).in_words
|
10
|
+
end
|
11
|
+
|
12
|
+
def in_words
|
13
|
+
num_mod(100).zero? ? simple_hundreds : complex_hundreds
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :num
|
19
|
+
|
20
|
+
def initialize(num)
|
21
|
+
@num = num
|
22
|
+
end
|
23
|
+
|
24
|
+
def first_digit
|
25
|
+
single_digits(in_array[0].to_i)
|
26
|
+
end
|
27
|
+
|
28
|
+
def rest_digits
|
29
|
+
in_array[1..-1].join.to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
def rest
|
33
|
+
rest_digits < 10 ? single_digits(rest_digits) : TwoDigits.run(rest_digits)
|
34
|
+
end
|
35
|
+
|
36
|
+
def simple_hundreds
|
37
|
+
[first_digit, prefix(power_of_ten)].join('-')
|
38
|
+
end
|
39
|
+
|
40
|
+
def complex_hundreds
|
41
|
+
[first_digit, prefix(power_of_ten), rest].join('-')
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/digits/two_digits.rb
CHANGED
@@ -22,23 +22,23 @@ class TwoDigits
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def less_than_20?
|
25
|
-
|
25
|
+
num < 20
|
26
26
|
end
|
27
27
|
|
28
28
|
def less_than_20
|
29
|
-
numbers_in_words['tenss'][num_mod]
|
29
|
+
numbers_in_words['tenss'][num_mod(10)]
|
30
30
|
end
|
31
31
|
|
32
32
|
def more_than_19
|
33
|
-
num_mod.zero? ? simple_tens : complex_tens
|
33
|
+
num_mod(10).zero? ? simple_tens : complex_tens
|
34
34
|
end
|
35
35
|
|
36
36
|
def simple_tens
|
37
|
-
numbers_in_words['tens'][
|
37
|
+
numbers_in_words['tens'][num/10]
|
38
38
|
end
|
39
39
|
|
40
40
|
def complex_tens
|
41
|
-
simple_tens
|
41
|
+
[simple_tens, single_digits(num_mod(10))].join('-')
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|
data/lib/english_number.rb
CHANGED
@@ -2,6 +2,7 @@ require 'yaml'
|
|
2
2
|
require 'active_support/inflector'
|
3
3
|
|
4
4
|
require 'digits/two_digits'
|
5
|
+
require 'digits/three_digits'
|
5
6
|
require 'modules/compute'
|
6
7
|
require 'modules/translate'
|
7
8
|
|
@@ -12,10 +13,12 @@ class EnglishNumber
|
|
12
13
|
|
13
14
|
def initialize(num)
|
14
15
|
raise ArgumentError unless num.is_a?(Numeric)
|
15
|
-
@num = num
|
16
|
+
@num = num
|
16
17
|
end
|
17
18
|
|
18
19
|
# To get translation use #in_english method from ::Translate
|
20
|
+
# Limit of digits can be set on constant DIGITS_LIMIT on ::Translate
|
21
|
+
# For now gem works for up to three-digit numbers
|
19
22
|
|
20
23
|
end
|
21
24
|
|
data/lib/modules/compute.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
ones: [zero, one, two, three, four, five, six, seven, eight, nine]
|
2
2
|
tenss: [ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen]
|
3
|
-
tens: [zero, ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety]
|
3
|
+
tens: [zero, ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety]
|
4
|
+
powers: [one, ten, hundred, thousand, ten-thousand, hundred-thousand, million, ten-million, hundred-million, billion]
|
data/lib/modules/translate.rb
CHANGED
@@ -1,28 +1,50 @@
|
|
1
1
|
module Translate
|
2
|
+
# Set the limit of translated number of digits
|
3
|
+
DIGITS_LIMIT = 2
|
2
4
|
|
3
5
|
def in_english
|
4
|
-
|
5
|
-
return nil if digits_count > 2
|
6
|
+
return nil if over_limit?
|
6
7
|
|
7
8
|
num < 0 ? "minus-#{in_words}" : in_words
|
8
9
|
end
|
9
10
|
|
10
11
|
private
|
11
12
|
|
13
|
+
def over_limit?
|
14
|
+
digits_count > DIGITS_LIMIT
|
15
|
+
end
|
16
|
+
|
12
17
|
def numbers_in_words
|
13
18
|
return YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'languages/english_numbers.yml'))
|
14
19
|
end
|
15
20
|
|
16
21
|
def in_words
|
17
|
-
return single_digits(
|
18
|
-
"#{single_digits(digits_count).capitalize}Digits".constantize.send(:run,
|
22
|
+
return single_digits(formatted_num) unless digits_count > 1
|
23
|
+
"#{single_digits(digits_count).capitalize}Digits".constantize.send(:run, formatted_num)
|
24
|
+
end
|
25
|
+
|
26
|
+
def in_array
|
27
|
+
formatted_num.to_s.split(//)
|
19
28
|
end
|
20
29
|
|
21
30
|
def digits_count
|
22
|
-
|
31
|
+
in_array.count
|
32
|
+
end
|
33
|
+
|
34
|
+
def power_of_ten
|
35
|
+
digits_count - 1
|
23
36
|
end
|
24
37
|
|
25
38
|
def single_digits(number)
|
26
39
|
numbers_in_words['ones'][number]
|
27
40
|
end
|
41
|
+
|
42
|
+
def prefix(number)
|
43
|
+
numbers_in_words['powers'][number]
|
44
|
+
end
|
45
|
+
|
46
|
+
def formatted_num
|
47
|
+
num.abs.to_i
|
48
|
+
end
|
49
|
+
|
28
50
|
end
|
@@ -2,72 +2,154 @@ RSpec.describe EnglishNumber do
|
|
2
2
|
subject(:eng_num) { described_class }
|
3
3
|
|
4
4
|
# Integration tests
|
5
|
-
it 'includes
|
6
|
-
expect(eng_num.included_modules).to include(
|
5
|
+
it 'includes Translate module' do
|
6
|
+
expect(eng_num.included_modules).to include(Translate)
|
7
7
|
end
|
8
8
|
|
9
9
|
describe '::Translate module' do
|
10
|
+
|
11
|
+
context 'when single digit' do
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
it 'translates one digit numbers to English words' do
|
14
|
+
expect(eng_num.new(6).in_english).to eq('six')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'translates negative one digit numbers' do
|
18
|
+
expect(eng_num.new(-7).in_english).to eq('minus-seven')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'translates float numbers to English words rounded down' do
|
22
|
+
expect(eng_num.new(5.5).in_english).to eq('five')
|
23
|
+
end
|
24
|
+
|
13
25
|
end
|
14
26
|
|
15
|
-
|
16
|
-
expect(eng_num.new(11).in_english).to eq('eleven')
|
17
|
-
end
|
27
|
+
context 'when two digits', if: Translate::DIGITS_LIMIT > 1 do
|
18
28
|
|
19
|
-
|
20
|
-
|
21
|
-
|
29
|
+
it 'translates two digit numbers < 20 to English words' do
|
30
|
+
expect(eng_num.new(11).in_english).to eq('eleven')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'translates two digit numbers >= 20 to English words' do
|
34
|
+
expect(eng_num.new(22).in_english).to eq('twenty-two')
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'translates negative two digit numbers < 20' do
|
38
|
+
expect(eng_num.new(-15).in_english).to eq('minus-fifteen')
|
39
|
+
end
|
22
40
|
|
23
|
-
|
24
|
-
|
41
|
+
it 'translates negative two digit numbers >= 20' do
|
42
|
+
expect(eng_num.new(-43).in_english).to eq('minus-forty-three')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'translates float numbers to English words rounded down' do
|
46
|
+
expect(eng_num.new(24.5).in_english).to eq('twenty-four')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'translates multiplications of 10 properly' do
|
50
|
+
expect(eng_num.new(30).in_english).to eq('thirty')
|
51
|
+
end
|
52
|
+
|
25
53
|
end
|
26
54
|
|
27
|
-
|
28
|
-
expect(eng_num.new(99).in_english).to eq('ninety-nine')
|
29
|
-
expect(eng_num.new(100).in_english).to eq(nil)
|
30
|
-
end
|
55
|
+
context 'when three digits', if: Translate::DIGITS_LIMIT > 2 do
|
31
56
|
|
32
|
-
|
33
|
-
|
34
|
-
|
57
|
+
it 'translates three digit numbers to English words' do
|
58
|
+
expect(eng_num.new(765).in_english).to eq('seven-hundred-sixty-five')
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'translates negative three digit numbers' do
|
62
|
+
expect(eng_num.new(-145).in_english).to eq('minus-one-hundred-forty-five')
|
63
|
+
end
|
35
64
|
|
36
|
-
|
37
|
-
|
38
|
-
|
65
|
+
it 'translates multiplications of 100 properly' do
|
66
|
+
expect(eng_num.new(400).in_english).to eq('four-hundred')
|
67
|
+
end
|
39
68
|
|
40
|
-
it 'translates negative two digit numbers >= 20' do
|
41
|
-
expect(eng_num.new(-43).in_english).to eq('minus-forty-three')
|
42
69
|
end
|
43
70
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
71
|
+
context 'when over set limit of digits' do
|
72
|
+
let(:limit) { 10**Translate::DIGITS_LIMIT }
|
73
|
+
let(:num) { rand(limit..10*limit) }
|
74
|
+
|
75
|
+
it 'doesn\'t translate positive numbers with more digits than limit' do
|
76
|
+
expect(eng_num.new(num).in_english).to eq(nil)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'doesn\'t translate negative numbers with more digits than limit' do
|
80
|
+
expect(eng_num.new(0-num).in_english).to eq(nil)
|
81
|
+
end
|
82
|
+
end
|
48
83
|
|
49
84
|
end
|
50
85
|
|
51
|
-
it 'includes
|
52
|
-
expect(eng_num.included_modules).to include(
|
86
|
+
it 'includes Compute module' do
|
87
|
+
expect(eng_num.included_modules).to include(Compute)
|
53
88
|
end
|
54
89
|
|
55
90
|
describe '::Compute module' do
|
56
91
|
|
57
|
-
|
58
|
-
|
92
|
+
context 'adds numbers and returns new object' do
|
93
|
+
|
94
|
+
it 'single digits' do
|
95
|
+
expect((eng_num.new(2) + eng_num.new(6)).num).to eq(8)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'two digits', if: Translate::DIGITS_LIMIT > 1 do
|
99
|
+
expect((eng_num.new(12) + eng_num.new(86)).num).to eq(98)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'three digits', if: Translate::DIGITS_LIMIT > 2 do
|
103
|
+
expect((eng_num.new(112) + eng_num.new(245)).num).to eq(357)
|
104
|
+
end
|
59
105
|
end
|
60
106
|
|
61
|
-
|
62
|
-
|
107
|
+
context 'subtracts numbers and returns new object' do
|
108
|
+
|
109
|
+
it 'single digits' do
|
110
|
+
expect((eng_num.new(7) - eng_num.new(3)).num).to eq(4)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'two digits' do
|
114
|
+
expect((eng_num.new(10) - eng_num.new(17)).num).to eq(-7)
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'three digits' do
|
118
|
+
expect((eng_num.new(765) - eng_num.new(265)).num).to eq(500)
|
119
|
+
end
|
120
|
+
|
63
121
|
end
|
64
122
|
|
65
|
-
|
66
|
-
|
123
|
+
context 'multiplies numbers and returns new object' do
|
124
|
+
|
125
|
+
it 'single digits' do
|
126
|
+
expect((eng_num.new(2) * eng_num.new(3)).num).to eq(6)
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'two digits' do
|
130
|
+
expect((eng_num.new(11) * eng_num.new(45)).num).to eq(495)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'three digits' do
|
134
|
+
expect((eng_num.new(765) - eng_num.new(265)).num).to eq(500)
|
135
|
+
end
|
136
|
+
|
67
137
|
end
|
68
138
|
|
69
|
-
|
70
|
-
|
139
|
+
|
140
|
+
context 'divides numbers and returns new object' do
|
141
|
+
|
142
|
+
it 'single digits' do
|
143
|
+
expect((eng_num.new(8) / eng_num.new(2)).num).to eq(4)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'two digits' do
|
147
|
+
expect((eng_num.new(66) / eng_num.new(3)).num).to eq(22)
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'three digits' do
|
151
|
+
expect((eng_num.new(754) / eng_num.new(4)).num).to eq(188)
|
152
|
+
end
|
71
153
|
end
|
72
154
|
|
73
155
|
end
|
@@ -60,13 +60,9 @@ RSpec.describe 'Compute module' do
|
|
60
60
|
|
61
61
|
context 'having any object with num attribute' do
|
62
62
|
let(:object) { dummy_class.new(-68) }
|
63
|
-
|
64
|
-
it '#
|
65
|
-
expect(object.
|
66
|
-
end
|
67
|
-
|
68
|
-
it '#num_mod returns modulo from dividing num attribute absolute by 10' do
|
69
|
-
expect(object.num_mod).to eq(8)
|
63
|
+
|
64
|
+
it '#num_mod returns modulo from dividing num attribute absolute by given number' do
|
65
|
+
expect(object.num_mod(10)).to eq(8)
|
70
66
|
end
|
71
67
|
|
72
68
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
RSpec.describe ThreeDigits do
|
2
|
+
subject(:three_dig) { described_class }
|
3
|
+
|
4
|
+
context 'modules' do
|
5
|
+
|
6
|
+
it 'includes Compute module' do
|
7
|
+
expect(three_dig.included_modules).to include(Compute)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'includes Translate module' do
|
11
|
+
expect(three_dig.included_modules).to include(Translate)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
context '#run' do
|
17
|
+
let(:num) { rand }
|
18
|
+
let(:inst) { instance_double('ThreeDigits') }
|
19
|
+
|
20
|
+
it 'responds to class method #run(num)' do
|
21
|
+
expect(three_dig).to respond_to(:run).with(1).argument
|
22
|
+
three_dig.run(num)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'when #run used it it invokes #in_words on new instance' do
|
26
|
+
allow(three_dig).to receive(:new).with(num) { inst }
|
27
|
+
expect(inst).to receive(:in_words)
|
28
|
+
three_dig.run(num)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context '#in_words', if: Translate::DIGITS_LIMIT > 2 do
|
34
|
+
|
35
|
+
context 'works for three-digit numbers:' do
|
36
|
+
it '100' do
|
37
|
+
expect(three_dig.new(100).in_words).to eq('one-hundred')
|
38
|
+
end
|
39
|
+
it '121' do
|
40
|
+
expect(three_dig.new(121).in_words).to eq('one-hundred-twenty-one')
|
41
|
+
end
|
42
|
+
it '237' do
|
43
|
+
expect(three_dig.new(237).in_words).to eq('two-hundred-thirty-seven')
|
44
|
+
end
|
45
|
+
it '300' do
|
46
|
+
expect(three_dig.new(300).in_words).to eq('three-hundred')
|
47
|
+
end
|
48
|
+
it '600' do
|
49
|
+
expect(three_dig.new(600).in_words).to eq('six-hundred')
|
50
|
+
end
|
51
|
+
it '873' do
|
52
|
+
expect(three_dig.new(873).in_words).to eq('eight-hundred-seventy-three')
|
53
|
+
end
|
54
|
+
it '999' do
|
55
|
+
expect(three_dig.new(999).in_words).to eq('nine-hundred-ninety-nine')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
RSpec.describe 'Translate module' do
|
2
|
+
let(:dummy_class) { Class.new {
|
3
|
+
include Translate
|
4
|
+
attr_reader :num
|
5
|
+
def initialize(num)
|
6
|
+
@num = num
|
7
|
+
end } }
|
8
|
+
|
9
|
+
|
10
|
+
context '#in_english, having any object with num attribute' do
|
11
|
+
let(:limit_number) { 10**Translate::DIGITS_LIMIT }
|
12
|
+
let(:within_limit_num) { rand(limit_number) }
|
13
|
+
let(:positive_num_object) { dummy_class.new(within_limit_num) }
|
14
|
+
let(:negative_num_object) { dummy_class.new(0-within_limit_num) }
|
15
|
+
let(:overlimit_num) { rand(limit_number..10*limit_number) }
|
16
|
+
let(:bad_object) { dummy_class.new(overlimit_num) }
|
17
|
+
|
18
|
+
it 'returns nil when digits count of num is over limit set in over_limit? method' do
|
19
|
+
expect(bad_object.in_english).to eq(nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should add "minus-" when the num is negative' do
|
23
|
+
expect(negative_num_object.in_english).to match(/^minus-/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'shouldn\'t add "minus-" when the num is positive' do
|
27
|
+
expect(positive_num_object.in_english).not_to match(/^minus-/)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
RSpec.describe TwoDigits do
|
2
|
+
subject(:two_dig) { described_class }
|
3
|
+
|
4
|
+
context 'modules' do
|
5
|
+
|
6
|
+
it 'includes Compute module' do
|
7
|
+
expect(two_dig.included_modules).to include(Compute)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'includes Translate module' do
|
11
|
+
expect(two_dig.included_modules).to include(Translate)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
context '#run' do
|
17
|
+
let(:num) { rand }
|
18
|
+
let(:inst) { instance_double('TwoDigits') }
|
19
|
+
|
20
|
+
it 'responds to class method #run(num)' do
|
21
|
+
expect(two_dig).to respond_to(:run).with(1).argument
|
22
|
+
two_dig.run(num)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'when #run used it it invokes #in_words on new instance' do
|
26
|
+
allow(two_dig).to receive(:new).with(num) { inst }
|
27
|
+
expect(inst).to receive(:in_words)
|
28
|
+
two_dig.run(num)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
context '#in_words', if: Translate::DIGITS_LIMIT > 1 do
|
34
|
+
|
35
|
+
context 'works for two-digit numbers smaller than 20:' do
|
36
|
+
it '10' do
|
37
|
+
expect(two_dig.new(10).in_words).to eq('ten')
|
38
|
+
end
|
39
|
+
it '16' do
|
40
|
+
expect(two_dig.new(16).in_words).to eq('sixteen')
|
41
|
+
end
|
42
|
+
it '19' do
|
43
|
+
expect(two_dig.new(19).in_words).to eq('nineteen')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'works for two-digit numbers greater than 19' do
|
48
|
+
it '25' do
|
49
|
+
expect(two_dig.new(25).in_words).to eq('twenty-five')
|
50
|
+
end
|
51
|
+
it '50' do
|
52
|
+
expect(two_dig.new(50).in_words).to eq('fifty')
|
53
|
+
end
|
54
|
+
it '99' do
|
55
|
+
expect(two_dig.new(99).in_words).to eq('ninety-nine')
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: english_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Brych
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 0.11.2
|
55
55
|
description: "Example: EnglishNumber.new(63).in_english # => 'sixty-three'.\n \n EnglishNumber
|
56
56
|
objects can be added, subtracted, multiplied, divided.\n\n For
|
57
|
-
now gem is deliberately limited to numbers maximum
|
57
|
+
now gem is deliberately limited to numbers maximum three digits long.\n"
|
58
58
|
email: pbrych@gmail.com
|
59
59
|
executables: []
|
60
60
|
extensions: []
|
@@ -62,8 +62,11 @@ extra_rdoc_files: []
|
|
62
62
|
files:
|
63
63
|
- ".gitignore"
|
64
64
|
- ".rspec"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- Gemfile
|
67
|
+
- README.md
|
66
68
|
- english_number.gemspec
|
69
|
+
- lib/digits/three_digits.rb
|
67
70
|
- lib/digits/two_digits.rb
|
68
71
|
- lib/english_number.rb
|
69
72
|
- lib/modules/compute.rb
|
@@ -73,6 +76,9 @@ files:
|
|
73
76
|
- spec/spec_helper.rb
|
74
77
|
- spec/unit_tests/compute_spec.rb
|
75
78
|
- spec/unit_tests/english_number_unit_spec.rb
|
79
|
+
- spec/unit_tests/three_digits_spec.rb
|
80
|
+
- spec/unit_tests/translate_spec.rb
|
81
|
+
- spec/unit_tests/two_digits_spec.rb
|
76
82
|
homepage: http://rubygems.org/gems/english-number.rb
|
77
83
|
licenses:
|
78
84
|
- MIT
|