slovom 0.1.2 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +18 -3
- data/lib/slovom.rb +123 -122
- data/lib/slovom/version.rb +1 -1
- metadata +3 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
# Slovom
|
1
|
+
# Slovom
|
2
2
|
|
3
3
|
A Ruby gem which converts decimal currency numbers into text in Bulgarian language. For use in financial applications, accounting documents, and all other instances requiring currency verbalization.
|
4
4
|
|
5
5
|
Handles the specifics of verbally presenting numbers and prices in Bulgarian, including grammatical irregularities, differences due to gender, singularity and plurality and the logic of using or omitting the "and" conjunction from the resulting string.
|
6
6
|
|
7
|
+
[![Build Status](https://secure.travis-ci.org/tarakanbg/slovom.png)](http://travis-ci.org/tarakanbg/slovom)
|
8
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/tarakanbg/slovom)
|
9
|
+
|
7
10
|
## Installation
|
8
11
|
|
9
12
|
Add this line to your application's Gemfile:
|
@@ -45,5 +48,17 @@ Note the `.slovom` method is attached to the `BigDecimal` class, which is also u
|
|
45
48
|
1. Fork it
|
46
49
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
50
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
48
|
-
4.
|
49
|
-
5.
|
51
|
+
4. Make sure all tests are passing!
|
52
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
6. Create new Pull Request
|
54
|
+
|
55
|
+
Copyright © 2012 [Svilen Vassilev](http://about.me/svilen)
|
56
|
+
|
57
|
+
*If you find my work useful or time-saving, you can endorse it or buy me a beer:*
|
58
|
+
|
59
|
+
[![endorse](http://api.coderwall.com/svilenv/endorse.png)](http://coderwall.com/svilenv)
|
60
|
+
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5FR7AQA4PLD8A)
|
61
|
+
|
62
|
+
Released under the [MIT LICENSE](https://github.com/tarakanbg/slovom/blob/master/LICENSE)
|
63
|
+
|
64
|
+
|
data/lib/slovom.rb
CHANGED
@@ -4,148 +4,149 @@ require 'bigdecimal'
|
|
4
4
|
|
5
5
|
class BigDecimal
|
6
6
|
def slovom
|
7
|
-
Slovom.
|
7
|
+
Slovom::Verbalizer.new(self).to_s
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
module Slovom
|
12
|
-
|
13
|
-
input = input.round(2)
|
14
|
-
levs = input.fix.to_i
|
15
|
-
stotinki = input.frac.to_s('F').gsub("0.", '')
|
16
|
-
stotinki = (stotinki+"0")[0..1].to_i
|
17
|
-
output = ""
|
18
|
-
output += levs_slovom(levs) + levs_title(levs) unless levs_slovom(levs).nil? || levs_slovom(levs) == "много"
|
19
|
-
output += " и " unless levs_slovom(levs).nil? || levs_slovom(levs) == "много" || levs_slovom(stotinki, feminine=true).nil? || levs_slovom(stotinki, feminine=true) == "много"
|
20
|
-
output += levs_slovom(stotinki, feminine=true) + stotinki_title(stotinki) unless levs_slovom(stotinki, feminine=true).nil? || levs_slovom(stotinki, feminine=true) == "много"
|
21
|
-
return output
|
22
|
-
end
|
12
|
+
class Verbalizer
|
23
13
|
|
24
|
-
|
25
|
-
|
26
|
-
"много"
|
27
|
-
end
|
14
|
+
attributes = %w{decimal levs stotinki output levs_title stotinki_title too_big}
|
15
|
+
attributes.each {|attribute| attr_accessor attribute.to_sym }
|
28
16
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
when 100..999 then hundreds(levs, feminine)
|
41
|
-
when 1000..999999 then thousands(levs, feminine)
|
42
|
-
when 1000000..999999999999999 then gazillions(levs, feminine)
|
43
|
-
else
|
44
|
-
too_big
|
17
|
+
def initialize(decimal)
|
18
|
+
@decimal = decimal.round(2)
|
19
|
+
@levs = @decimal.fix.to_i
|
20
|
+
@stotinki = ((@decimal.frac.to_s('F').gsub("0.", ''))+"0")[0..1].to_i
|
21
|
+
@levs == 1 ? @levs_title = " лев" : @levs_title = " лева"
|
22
|
+
@stotinki == 1 ? @stotinki_title = " стотинка" : @stotinki_title = " стотинки"
|
23
|
+
@too_big = "много"
|
24
|
+
@output = ""
|
25
|
+
@output += numbers_slovom(@levs) + @levs_title unless numbers_slovom(@levs).nil? || numbers_slovom(@levs) == "много"
|
26
|
+
@output += " и " unless numbers_slovom(@levs).nil? || numbers_slovom(@levs) == "много" || numbers_slovom(@stotinki, feminine=true).nil? || numbers_slovom(@stotinki, feminine=true) == "много"
|
27
|
+
@output += numbers_slovom(@stotinki, feminine=true) + @stotinki_title unless numbers_slovom(@stotinki, feminine=true).nil? || numbers_slovom(@stotinki, feminine=true) == "много"
|
45
28
|
end
|
46
|
-
end
|
47
29
|
|
48
|
-
|
49
|
-
|
50
|
-
when 1..19 then basic_number(digits, feminine)
|
51
|
-
when 20..99 then round_ten(digits, feminine)
|
30
|
+
def to_s
|
31
|
+
@output
|
52
32
|
end
|
53
|
-
end
|
54
33
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
when 8 then "осем"
|
67
|
-
when 9 then "девет"
|
68
|
-
when 10 then "десет"
|
69
|
-
when 11 then "единадесет"
|
70
|
-
when 12 then "дванадесет"
|
71
|
-
when 13..19 then basic_number(digits.to_s[1].to_i)+"надесет"
|
34
|
+
private
|
35
|
+
|
36
|
+
def numbers_slovom(ammount, feminine=nil)
|
37
|
+
case ammount
|
38
|
+
when 1..99 then below_hundred(ammount, feminine)
|
39
|
+
when 100..999 then hundreds(ammount, feminine)
|
40
|
+
when 1000..999999 then thousands(ammount, feminine)
|
41
|
+
when 1000000..999999999999999 then gazillions(ammount, feminine)
|
42
|
+
else
|
43
|
+
@too_big
|
44
|
+
end
|
72
45
|
end
|
73
|
-
end
|
74
46
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
47
|
+
def below_hundred(digits, feminine=nil)
|
48
|
+
case digits
|
49
|
+
when 1..19 then basic_number(digits, feminine)
|
50
|
+
when 20..99 then round_ten(digits, feminine)
|
51
|
+
end
|
52
|
+
end
|
81
53
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
54
|
+
def basic_number(digits, feminine=nil)
|
55
|
+
case digits
|
56
|
+
when 1 then
|
57
|
+
feminine == true ? "една" : "един"
|
58
|
+
when 2 then
|
59
|
+
feminine == true ? "две" : "два"
|
60
|
+
when 3 then "три"
|
61
|
+
when 4 then "четири"
|
62
|
+
when 5 then "пет"
|
63
|
+
when 6 then "шест"
|
64
|
+
when 7 then "седем"
|
65
|
+
when 8 then "осем"
|
66
|
+
when 9 then "девет"
|
67
|
+
when 10 then "десет"
|
68
|
+
when 11 then "единадесет"
|
69
|
+
when 12 then "дванадесет"
|
70
|
+
when 13..19 then basic_number(digits.to_s[1].to_i)+"надесет"
|
71
|
+
end
|
90
72
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
73
|
+
|
74
|
+
def round_ten(digits, feminine=nil)
|
75
|
+
second_digit = digits.to_s.reverse.chr.to_i
|
76
|
+
output = basic_number(digits.to_s[0].to_i)+"десет"
|
77
|
+
output += " и " + basic_number(second_digit, feminine) unless second_digit == 0
|
78
|
+
output
|
94
79
|
end
|
95
|
-
output += " " + levs_slovom(final_digits, feminine) unless final_digits == 0
|
96
|
-
return output.gsub(/\s+/, " ").strip
|
97
|
-
end
|
98
80
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
81
|
+
def hundreds(digits, feminine=nil)
|
82
|
+
final_digits = digits.to_s[1..3].to_i
|
83
|
+
first_digit = digits.to_s[0].to_i
|
84
|
+
case first_digit
|
85
|
+
when 1 then hh = "сто"
|
86
|
+
when 2 then hh = "двеста"
|
87
|
+
when 3 then hh = "триста"
|
88
|
+
when 4..9 then hh = basic_number(first_digit)+"стотин"
|
89
|
+
end
|
90
|
+
output = hh
|
91
|
+
case final_digits
|
92
|
+
when 1..20 then output += " и "
|
93
|
+
end
|
94
|
+
output += " " + numbers_slovom(final_digits, feminine) unless final_digits == 0
|
95
|
+
output.gsub(/\s+/, " ").strip
|
114
96
|
end
|
115
|
-
output = hh
|
116
|
-
output += " и " unless (levs_slovom(big).include?(" и ") && big.to_s.length == 3) or levs_slovom(big) == "много"
|
117
|
-
output += levs_slovom(big) unless big == 0
|
118
|
-
return output.gsub(/\s+/, " ").strip
|
119
|
-
end
|
120
97
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
98
|
+
def thousands(digits, feminine=nil)
|
99
|
+
count = digits.to_s.length
|
100
|
+
case count
|
101
|
+
when 4 then
|
102
|
+
small = digits.to_s[0].to_i
|
103
|
+
small == 1 ? hh = "хиляда " : hh = numbers_slovom(small, feminine = true) + " хиляди "
|
104
|
+
big = digits.to_s[1..3].to_i
|
105
|
+
when 5 then
|
106
|
+
small = digits.to_s[0..1].to_i
|
107
|
+
hh = numbers_slovom(small, feminine = true) + " хиляди "
|
108
|
+
big = digits.to_s[2..4].to_i
|
109
|
+
when 6 then
|
110
|
+
small = digits.to_s[0..2].to_i
|
111
|
+
hh = numbers_slovom(small, feminine = true) + " хиляди "
|
112
|
+
big = digits.to_s[3..5].to_i
|
113
|
+
end
|
114
|
+
output = hh
|
115
|
+
output += " и " unless (numbers_slovom(big).include?(" и ") && big.to_s.length == 3) or numbers_slovom(big) == "много"
|
116
|
+
output += numbers_slovom(big) unless big == 0
|
117
|
+
output.gsub(/\s+/, " ").strip
|
133
118
|
end
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
119
|
+
|
120
|
+
def gazillions(digits, feminine=nil)
|
121
|
+
count = digits.to_s.length
|
122
|
+
case count
|
123
|
+
when 7..9 then
|
124
|
+
base_count = 7
|
125
|
+
word_plural = " милиона "; word_singular = " милион "
|
126
|
+
when 10..12 then
|
127
|
+
base_count = 10
|
128
|
+
word_plural = " милиарда "; word_singular = " милиард "
|
129
|
+
when 13..15 then
|
130
|
+
base_count = 13
|
131
|
+
word_plural = " трилиона "; word_singular = " трилион "
|
132
|
+
end
|
133
|
+
if count == base_count
|
134
|
+
big_number = digits.to_s[0].to_i
|
135
|
+
small_number = digits.to_s[1..24].to_i
|
136
|
+
elsif count == base_count + 1
|
137
|
+
big_number = digits.to_s[0..1].to_i
|
138
|
+
small_number = digits.to_s[2..24].to_i
|
139
|
+
elsif count == base_count + 2
|
140
|
+
big_number = digits.to_s[0..2].to_i
|
141
|
+
small_number = digits.to_s[3..24].to_i
|
142
|
+
end
|
143
|
+
big_number == 1 ? word = word_singular : word = word_plural
|
144
|
+
|
145
|
+
output = numbers_slovom(big_number) + word
|
146
|
+
output += " и " unless numbers_slovom(small_number).include? " и " or numbers_slovom(small_number) == "много"
|
147
|
+
output += numbers_slovom(small_number) unless numbers_slovom(small_number) == "много"
|
148
|
+
output.gsub(/\s+/, " ").strip
|
143
149
|
end
|
144
|
-
big_number == 1 ? word = word_singular : word = word_plural
|
145
150
|
|
146
|
-
output = levs_slovom(big_number) + word
|
147
|
-
output += " и " unless levs_slovom(small_number).include? " и " or levs_slovom(small_number) == "много"
|
148
|
-
output += levs_slovom(small_number) unless levs_slovom(small_number) == "много"
|
149
|
-
return output.gsub(/\s+/, " ").strip
|
150
151
|
end
|
151
152
|
end
|
data/lib/slovom/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slovom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
|
-
rubygems_version: 1.8.
|
89
|
+
rubygems_version: 1.8.24
|
90
90
|
signing_key:
|
91
91
|
specification_version: 3
|
92
92
|
summary: Converts decimal currency numbers into text in Bulgarian language
|