slovom 0.1.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/README.md +18 -3
  3. data/lib/slovom.rb +123 -122
  4. data/lib/slovom/version.rb +1 -1
  5. metadata +3 -3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v. 0.2
4
+
5
+ * OO refactoring, no user facing changes
6
+
3
7
  ## v. 0.1
4
8
 
5
9
  * Initial release
data/README.md CHANGED
@@ -1,9 +1,12 @@
1
- # Slovom [![Build Status](https://secure.travis-ci.org/tarakanbg/slovom.png)](http://travis-ci.org/tarakanbg/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. Push to the branch (`git push origin my-new-feature`)
49
- 5. Create new Pull Request
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.slovom(self)
7
+ Slovom::Verbalizer.new(self).to_s
8
8
  end
9
9
  end
10
10
 
11
11
  module Slovom
12
- def self.slovom(input)
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
- private
25
- def self.too_big
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
- def self.levs_title(levs)
30
- levs == 1 ? " лев" : " лева"
31
- end
32
-
33
- def self.stotinki_title(stotinki)
34
- stotinki == 1 ? " стотинка" : " стотинки"
35
- end
36
-
37
- def self.levs_slovom(levs, feminine=nil)
38
- case levs
39
- when 1..99 then below_hundred(levs, feminine)
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
- def self.below_hundred(digits, feminine=nil)
49
- case digits
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
- def self.basic_number(digits, feminine=nil)
56
- case digits
57
- when 1 then
58
- feminine == true ? "една" : "един"
59
- when 2 then
60
- feminine == true ? "две" : "два"
61
- when 3 then "три"
62
- when 4 then "четири"
63
- when 5 then "пет"
64
- when 6 then "шест"
65
- when 7 then "седем"
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
- def self.round_ten(digits, feminine=nil)
76
- second_digit = digits.to_s.reverse.chr.to_i
77
- output = basic_number(digits.to_s[0].to_i)+"десет"
78
- output += " и " + basic_number(second_digit, feminine) unless second_digit == 0
79
- return output
80
- end
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
- def self.hundreds(digits, feminine=nil)
83
- final_digits = digits.to_s[1..3].to_i
84
- first_digit = digits.to_s[0].to_i
85
- case first_digit
86
- when 1 then hh = "сто"
87
- when 2 then hh = "двеста"
88
- when 3 then hh = "триста"
89
- when 4..9 then hh = basic_number(first_digit)+"стотин"
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
- output = hh
92
- case final_digits
93
- when 1..20 then output += " и "
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
- def self.thousands(digits, feminine=nil)
100
- count = digits.to_s.length
101
- case count
102
- when 4 then
103
- small = digits.to_s[0].to_i
104
- small == 1 ? hh = "хиляда " : hh = levs_slovom(small, feminine = true) + " хиляди "
105
- big = digits.to_s[1..3].to_i
106
- when 5 then
107
- small = digits.to_s[0..1].to_i
108
- hh = levs_slovom(small, feminine = true) + " хиляди "
109
- big = digits.to_s[2..4].to_i
110
- when 6 then
111
- small = digits.to_s[0..2].to_i
112
- hh = levs_slovom(small, feminine = true) + " хиляди "
113
- big = digits.to_s[3..5].to_i
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
- def self.gazillions(digits, feminine=nil)
122
- count = digits.to_s.length
123
- case count
124
- when 7..9 then
125
- base_count = 7
126
- word_plural = " милиона "; word_singular = " милион "
127
- when 10..12 then
128
- base_count = 10
129
- word_plural = " милиарда "; word_singular = " милиард "
130
- when 13..15 then
131
- base_count = 13
132
- word_plural = " трилиона "; word_singular = " трилион "
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
- if count == base_count
135
- big_number = digits.to_s[0].to_i
136
- small_number = digits.to_s[1..24].to_i
137
- elsif count == base_count + 1
138
- big_number = digits.to_s[0..1].to_i
139
- small_number = digits.to_s[2..24].to_i
140
- elsif count == base_count + 2
141
- big_number = digits.to_s[0..2].to_i
142
- small_number = digits.to_s[3..24].to_i
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
@@ -1,3 +1,3 @@
1
1
  module Slovom
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2"
3
3
  end
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.1.2
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-04-22 00:00:00.000000000 Z
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.21
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