kabal 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -4
- data/Rakefile +1 -1
- data/lib/kabal.rb +1 -1
- data/lib/kabal/languages/english.rb +66 -0
- data/lib/kabal/supported_languages.rb +1 -0
- data/lib/kabal/version.rb +1 -1
- data/test/lib/kabal/languages/english_test.rb +39 -0
- data/yaml/languages.yml +1 -0
- data/yaml/languages/en.yml +37 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c101bb50f76d3c3bfff154c5e420b69a2558e81a
|
4
|
+
data.tar.gz: 636f8f442b6b45c8f22ab111c465db1b1c1c760d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4a8e658b4c4f17f7c53348ef7510ef92297827976c341ab599733b1753560032b05fde56179fe0414088f3bd2c3f6275c6b5f16b88a228eff0682cc884849a3
|
7
|
+
data.tar.gz: 21377db86be3a7c956af37b0bd79163d206ca5a9011c732d2c3e7fc5fb72eeb22a15a0c9f97c8ad46f5855f6487ef5b91181122bcfb50322579bd600139c10c1
|
data/README.md
CHANGED
@@ -4,6 +4,9 @@
|
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/kalashnikovisme/kabal/badge.png?branch=master)](https://coveralls.io/r/kalashnikovisme/kabal?branch=master)
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/kabal.svg)](http://badge.fury.io/rb/kabal)
|
6
6
|
[![endorse](https://api.coderwall.com/kalashnikovisme/endorsecount.png)](https://coderwall.com/kalashnikovisme)
|
7
|
+
[![Dependency Status](https://gemnasium.com/kalashnikovisme/kabal.svg)](https://gemnasium.com/kalashnikovisme/kabal)
|
8
|
+
[![Still MainTained](http://stillmaintained.com/kalashnikovisme/kabal.png)](http://stillmaintained.com/kalashnikovisme/kabal)
|
9
|
+
|
7
10
|
|
8
11
|
Kabal allows you to translate number into text.
|
9
12
|
|
@@ -25,13 +28,15 @@ Or install it yourself as:
|
|
25
28
|
|
26
29
|
Kabal has one main feature - number to text "converting".
|
27
30
|
|
28
|
-
require 'kabal'
|
29
|
-
include Kabal
|
30
|
-
Kabal.to_text 1 #=> "один"
|
31
|
+
$> require 'kabal'
|
32
|
+
$> include Kabal
|
33
|
+
$> Kabal.to_text 1 #=> "один"
|
34
|
+
$> Kabal.to_text -1000000 #=> "минус один миллион"
|
31
35
|
|
32
36
|
You can choose language to "converting".
|
33
37
|
|
34
|
-
Kabal.language = "Russian"
|
38
|
+
$> Kabal.language = "Russian"
|
39
|
+
$> Kabal.current_language #=> "Russian"
|
35
40
|
|
36
41
|
Supported Languages:
|
37
42
|
|
@@ -39,6 +44,10 @@ Supported Languages:
|
|
39
44
|
|
40
45
|
*More languages will be added later.*
|
41
46
|
|
47
|
+
You can see actual list of supported languages:
|
48
|
+
|
49
|
+
$> Kabal.supported_languages #=> ["Russian"]
|
50
|
+
|
42
51
|
## Contributing
|
43
52
|
|
44
53
|
1. Fork it ( https://github.com/kalashnikovisme/kabal/fork )
|
@@ -52,8 +61,23 @@ Supported Languages:
|
|
52
61
|
To add Elfin language you should:
|
53
62
|
|
54
63
|
1. Create `lib/kabal/languages/elfin.rb`. You'll describe rules of this language to drawing numbers there.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require 'kabal/languages/language'
|
67
|
+
|
68
|
+
class Kabal::Elfin < Kabal::Language
|
69
|
+
def convert(number)
|
70
|
+
#anything you want
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
55
74
|
2. Create `test/lib/kabal/languages/elfin_test.rb`. You'll cover your code by tests there.
|
56
75
|
3. Create `yaml/languages/el.yml`. You'll add numbers and words, which they correspond, there.
|
76
|
+
|
77
|
+
```yaml
|
78
|
+
el:
|
79
|
+
minus: #Minus on Elfin Language#
|
80
|
+
```
|
57
81
|
4. Add `Elfin: "el"` to `yaml/languages.yml`.
|
58
82
|
5. Add `require "kabal/languages/elfin"` to `lib/kabal/supported_languages.rb`.
|
59
83
|
|
data/Rakefile
CHANGED
data/lib/kabal.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'kabal/languages/language'
|
2
|
+
|
3
|
+
class Kabal::English < Kabal::Language
|
4
|
+
def convert(number)
|
5
|
+
raise NumberOutRangeError.message if number < 0 or number >= 10 ** 12
|
6
|
+
return single number if number >= 0 and number <= 19
|
7
|
+
return two_words number if number >= 0 and number <= 99
|
8
|
+
return three_words number if number >= 100 and number <= 999
|
9
|
+
ten_powers number if number >= 100
|
10
|
+
end
|
11
|
+
|
12
|
+
def single(number)
|
13
|
+
names[lang]["single"][number]
|
14
|
+
end
|
15
|
+
|
16
|
+
def two_words(number)
|
17
|
+
return single number if number <= 19
|
18
|
+
number_name = names[lang]["two_words"][number / 10]
|
19
|
+
number_name += " " + single(number % 10) if (number % 10 != 0)
|
20
|
+
number_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def three_words(number)
|
24
|
+
return two_words number if number <= 99
|
25
|
+
number_name = single(number / 100) + " " + names[lang]["ten_powers"][2]
|
26
|
+
number_name += " " + two_words(number % 100) if (number % 100 >= 10)
|
27
|
+
number_name += " and " + single(number % 100) if (number % 100 < 10)
|
28
|
+
number_name
|
29
|
+
end
|
30
|
+
|
31
|
+
def ten_powers(number)
|
32
|
+
less_thousands number if number_is_thousands? number
|
33
|
+
return @number_name if number_is_thousands? number
|
34
|
+
create_number_name number
|
35
|
+
ten_powers number % (10 ** number_order(number))
|
36
|
+
end
|
37
|
+
|
38
|
+
def number_order(number)
|
39
|
+
((number.to_s.length - 1) / 3) * 3
|
40
|
+
end
|
41
|
+
|
42
|
+
def count(number)
|
43
|
+
number / (10 ** number_order(number))
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_number_name(number)
|
47
|
+
if @number_name.nil?
|
48
|
+
@number_name = count_name(number) + " " + names[lang]["ten_powers"][number_order(number)]
|
49
|
+
elsif count(number) != 0
|
50
|
+
@number_name += " " + count_name(number) + " " + names[lang]["ten_powers"][number_order(number)]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def count_name(number)
|
55
|
+
three_words count(number)
|
56
|
+
end
|
57
|
+
|
58
|
+
def less_thousands(number)
|
59
|
+
@number_name += " and" if number < 100
|
60
|
+
@number_name += " " + three_words(number % 1000) unless number == 0
|
61
|
+
end
|
62
|
+
|
63
|
+
def number_is_thousands?(number)
|
64
|
+
number_order(number) < 3
|
65
|
+
end
|
66
|
+
end
|
data/lib/kabal/version.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EnglishTest < TestCase
|
4
|
+
def setup
|
5
|
+
@eng = Kabal::English.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_single_number
|
9
|
+
assert_equal @eng.convert(1), "one"
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_two_words
|
13
|
+
assert_equal @eng.convert(21), "twenty one"
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_three_words
|
17
|
+
assert_equal @eng.convert(121), "one hundred twenty one"
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_three_words_with_and
|
21
|
+
assert_equal @eng.convert(102), "one hundred and two"
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_thousands
|
25
|
+
assert_equal @eng.convert(11121), "eleven thousand one hundred twenty one"
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_thousands_with_and
|
29
|
+
assert_equal @eng.convert(11021), "eleven thousand and twenty one"
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_hundred_thousands_with_and
|
33
|
+
assert_equal @eng.convert(111021), "one hundred eleven thousand and twenty one"
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_hundred_millions_with_and
|
37
|
+
assert_equal @eng.convert(111150021), "one hundred eleven million one hundred fifty thousand and twenty one"
|
38
|
+
end
|
39
|
+
end
|
data/yaml/languages.yml
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
en:
|
2
|
+
minus: minus
|
3
|
+
single:
|
4
|
+
0: zero
|
5
|
+
1: one
|
6
|
+
2: two
|
7
|
+
3: three
|
8
|
+
4: four
|
9
|
+
5: five
|
10
|
+
6: six
|
11
|
+
7: seven
|
12
|
+
8: eight
|
13
|
+
9: nine
|
14
|
+
10: ten
|
15
|
+
11: eleven
|
16
|
+
12: twelve
|
17
|
+
13: thirteen
|
18
|
+
14: fourteen
|
19
|
+
15: fifteen
|
20
|
+
16: sixteen
|
21
|
+
17: seventeen
|
22
|
+
18: eighteen
|
23
|
+
19: nineteen
|
24
|
+
two_words:
|
25
|
+
2: twenty
|
26
|
+
3: thirty
|
27
|
+
4: forty
|
28
|
+
5: fifty
|
29
|
+
6: sixty
|
30
|
+
7: seventy
|
31
|
+
8: eighty
|
32
|
+
9: ninety
|
33
|
+
ten_powers:
|
34
|
+
2: hundred
|
35
|
+
3: thousand
|
36
|
+
6: million
|
37
|
+
9: billion
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kabal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
@@ -60,16 +60,19 @@ files:
|
|
60
60
|
- lib/kabal/errors.rb
|
61
61
|
- lib/kabal/errors/no_language_support_error.rb
|
62
62
|
- lib/kabal/errors/number_out_range_error.rb
|
63
|
+
- lib/kabal/languages/english.rb
|
63
64
|
- lib/kabal/languages/language.rb
|
64
65
|
- lib/kabal/languages/russian.rb
|
65
66
|
- lib/kabal/languages/russian/declinations.rb
|
66
67
|
- lib/kabal/supported_languages.rb
|
67
68
|
- lib/kabal/version.rb
|
68
69
|
- test/lib/bigtest.rb
|
70
|
+
- test/lib/kabal/languages/english_test.rb
|
69
71
|
- test/lib/kabal/languages/russian_test.rb
|
70
72
|
- test/lib/kabal_test.rb
|
71
73
|
- test/test_helper.rb
|
72
74
|
- yaml/languages.yml
|
75
|
+
- yaml/languages/en.yml
|
73
76
|
- yaml/languages/ru.yml
|
74
77
|
homepage: ''
|
75
78
|
licenses:
|
@@ -97,6 +100,7 @@ specification_version: 4
|
|
97
100
|
summary: Number To Text Helper
|
98
101
|
test_files:
|
99
102
|
- test/lib/bigtest.rb
|
103
|
+
- test/lib/kabal/languages/english_test.rb
|
100
104
|
- test/lib/kabal/languages/russian_test.rb
|
101
105
|
- test/lib/kabal_test.rb
|
102
106
|
- test/test_helper.rb
|