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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b54dc91c4b10e129d562e5c1c298655a7472d0e9
4
- data.tar.gz: b223d38f34c12fd4dd854f87f5b2395088cc8678
3
+ metadata.gz: c101bb50f76d3c3bfff154c5e420b69a2558e81a
4
+ data.tar.gz: 636f8f442b6b45c8f22ab111c465db1b1c1c760d
5
5
  SHA512:
6
- metadata.gz: 1355837b14a476cb0ed7dc81c1cbdbe9618be241893e2a2c2f12d325492141b2ead43c8e1d2087adbc608bcc3df3f47054395d097672ad3cc0ec815faa45823a
7
- data.tar.gz: 5f08687ce36d79c16217ea87632059e0468ce660695fb9da92d3b66a05f7d200733445c2ca382f32f0020165538f025ca7b083570d982018ac8bc6db03d78e47
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
@@ -4,7 +4,7 @@ require 'rake/testtask'
4
4
 
5
5
  Rake::TestTask.new do |t|
6
6
  t.libs << "test"
7
- t.test_files = FileList['test/lib/*_test.rb']
7
+ t.test_files = FileList['test/lib/*_test.rb', "test/lib/**/*_test.rb"]
8
8
  t.verbose = true
9
9
  end
10
10
 
@@ -35,7 +35,7 @@ module Kabal
35
35
  end
36
36
 
37
37
  def current_language
38
- @language
38
+ @language ||= "Russian"
39
39
  end
40
40
 
41
41
  def supported_languages
@@ -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
@@ -2,5 +2,6 @@ module Kabal
2
2
  module SupportedLanguages
3
3
  #FIXME add autoload
4
4
  require 'kabal/languages/russian'
5
+ require 'kabal/languages/english'
5
6
  end
6
7
  end
@@ -1,3 +1,3 @@
1
1
  module Kabal
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
@@ -1 +1,2 @@
1
1
  Russian: "ru"
2
+ English: "en"
@@ -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.1.7
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