polish-number 0.0.2 → 0.1.0

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.
Files changed (3) hide show
  1. data/README.rdoc +6 -2
  2. data/lib/polish_number.rb +49 -37
  3. metadata +3 -3
@@ -2,19 +2,23 @@
2
2
  Translates numbers to Polish words
3
3
 
4
4
  = Installation
5
+ Tested with Ruby 1.8.7 and 1.9.2.
6
+
5
7
  gem install polish-number
6
8
 
7
9
  = Usage
8
10
  require 'polish_number'
9
-
11
+
10
12
  PolishNumber.translate(1234) #=> tysiąc dwieście trzydzieści cztery
13
+ PolishNumber.translate(34, :currency => :PLN) #=> trzydzieści cztery złote
14
+ PolishNumber.translate(12, :currency => :PLN) #=> dwanaście złotych
11
15
 
12
16
  = Running specs
13
17
 
14
18
  rake test
15
19
 
16
20
  = TODO
17
- - currently works for number 0-1999. Make it work with millions, billions, etc
21
+ - currently works for numbers 0-999999. Make it work with millions, billions, etc
18
22
  - optional noun pluralization
19
23
 
20
24
  = GitHub
@@ -15,35 +15,43 @@ module PolishNumber
15
15
 
16
16
  ZERO = 'zero'
17
17
 
18
- THOUSANDS = ['', 'tysiąc ', 'tysiące ', 'tysięcy ']
18
+ THOUSANDS = {:one => 'tysiąc', :few => 'tysiące', :many => 'tysięcy'}
19
19
 
20
- AND = 'i '
21
- DECIMALS = ['', 'dziesiąta ', 'dziesiąte ', 'dziesiątych ']
20
+ CURRENCIES = {
21
+ :PLN => {:one => 'złoty', :few => 'złote', :many => 'złotych'}
22
+ }
22
23
 
23
- def self.translate(number)
24
- number = number.to_f
25
-
26
- unless (0..999999.99).include? number
27
- raise ArgumentError, 'number should be in 0..999999.99 range'
24
+ def self.translate(number, options={})
25
+ if options[:currency] && !CURRENCIES.has_key?(options[:currency])
26
+ raise ArgumentError, "unknown :currency option '#{options[:currency].inspect}'. Choose one from: #{CURRENCIES.inspect}"
28
27
  end
29
28
 
30
- return ZERO.dup if number == 0.0
29
+ number = number.to_i
31
30
 
32
- formatted_number = sprintf("%09.2f", number)
33
- digits = formatted_number.chars.map { |char| char.to_i }
31
+ unless (0..999999).include? number
32
+ raise ArgumentError, 'number should be in 0..999999 range'
33
+ end
34
34
 
35
- result = ''
36
- result << process_0_999(digits[0..2])
37
- result << pluralize(digits[1..2], THOUSANDS)
38
- result << process_0_999(digits[3..5])
39
- # skip the 6th digit - it was a decimal dot
40
- fraction = process_0_99(digits[7..8])
41
- unless fraction == ''
42
- result << AND
43
- result << fraction
44
- result << pluralize(digits[7..8], DECIMALS)
35
+ if number == 0
36
+ result = ZERO.dup
37
+ else
38
+ formatted_number = sprintf('%06.0f', number)
39
+ digits = formatted_number.chars.map { |char| char.to_i }
40
+
41
+ result = ''
42
+ result << process_0_999(digits[0..2])
43
+ result << thousands(number/1000, digits[0..2])
44
+ result << ' '
45
+ result << process_0_999(digits[3..5])
46
+ result.strip!
47
+ end
48
+
49
+ if options[:currency]
50
+ currency = CURRENCIES[options[:currency]]
51
+ result << ' '
52
+ result << currency[classify(number, digits)]
45
53
  end
46
- result.strip!
54
+
47
55
  result
48
56
  end
49
57
 
@@ -52,29 +60,33 @@ module PolishNumber
52
60
  def self.process_0_999(digits)
53
61
  result = ''
54
62
  result << HUNDREDS[digits[0]]
55
- result << process_0_99(digits[1..2])
63
+
64
+ if digits[1] == 1 && digits[2] != 0
65
+ result << TEENS[digits[2]]
66
+ else
67
+ result << TENS[digits[1]]
68
+ result << UNITIES[digits[2]]
69
+ end
70
+
56
71
  result
57
72
  end
58
73
 
59
- def self.process_0_99(digits)
60
- if digits[0] == 1 && digits[1] != 0
61
- TEENS[digits[1]]
74
+ def self.thousands(number, digits)
75
+ if number == 0
76
+ ''
62
77
  else
63
- TENS[digits[0]] + UNITIES[digits[1]]
78
+ THOUSANDS[classify(number, digits)]
64
79
  end
65
80
  end
66
81
 
67
- def self.pluralize(digits, words)
68
- if digits[0] == 0 && digits[1] == 0
69
- thousand_id = 0
70
- elsif digits[1] == 0 && digits[2] == 1
71
- thousand_id = 1
72
- elsif digits[1] != 1 && (2..4).include?(digits[2])
73
- thousand_id = 2
82
+ def self.classify(number, digits)
83
+ if number == 1
84
+ :one
85
+ # all numbers with 2, 3 or 4 at the end, but not teens
86
+ elsif digits && (2..4).include?(digits[-1]) && digits[-2] != 1
87
+ :few
74
88
  else
75
- thousand_id = 3
89
+ :many
76
90
  end
77
- words[thousand_id]
78
91
  end
79
92
  end
80
-
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Wojciech Piekutowski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-10 00:00:00 +02:00
18
+ date: 2010-10-04 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21