papla 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,24 @@
1
+ ### 0.1.0 (2012-04-20)
2
+
3
+ * Support Money
4
+
1
5
  ### 0.0.5 (2012-01-21)
2
6
 
3
7
  * Make converting decimal points more robust
4
8
 
5
9
  ### 0.0.4 (2012-01-21)
6
10
 
7
- * support English locale
11
+ * Support English locale
8
12
 
9
13
  ### 0.0.3 (2012-01-16)
10
14
 
11
- * provide localization using I18n
15
+ * Provide localization using I18n
12
16
 
13
17
  ### 0.0.2 (2011-01-09)
14
18
 
15
- * display decimal part of floats as cents/100
19
+ * Display decimal part of floats as cents/100
16
20
 
17
21
  ### 0.0.1 (2011-01-03)
18
22
 
19
- * initial release
20
- * support converting numbers from 0 up to 999 999 999 to Polish words
23
+ * Initial release
24
+ * Support converting numbers from 0 up to 999 999 999 to Polish words
data/README.md CHANGED
@@ -54,7 +54,7 @@ Papla[2.999] # => "Trzy 00/100"
54
54
 
55
55
  This feature is planned for future releases.
56
56
 
57
- ## Integration with Money, I18n and Rails
57
+ ## Integration with I18n and Money
58
58
 
59
59
  ### I18n
60
60
 
@@ -64,9 +64,22 @@ for details of keys used. Currently `:pl` and `:en` locales are supported.
64
64
  Note: English support is not fully correct, e.g. `123` becomes
65
65
  `"One hundred twenty three"` and not `"One hundred and twenty three"`.
66
66
 
67
- ### Money, Rails
67
+ ### Money
68
68
 
69
- This feature is planned for future releases.
69
+ If you're using the `money` gem, you can pass in an instance of `Money` to Papla.
70
+ The returned string will contain the dollars part as words,
71
+ the cents part as 'xx/100' and the currency string (e.g. `EUR`).
72
+
73
+ Example:
74
+
75
+ ```ruby
76
+ eleven_and_a_half_pounds = Money.new(1150, 'GBP')
77
+ Papla[eleven_and_a_half_pounds] # => "Jedenaście 50/100 GBP"
78
+
79
+ I18n.locale = :en
80
+ discounted_price = Money.new(9999, 'PLN')
81
+ Papla[discounted_price] # => "Ninety nine 99/100 PLN"
82
+ ```
70
83
 
71
84
  ## Documentation
72
85
 
@@ -1,9 +1,9 @@
1
- require 'bigdecimal'
2
- require 'bigdecimal/util'
3
- require 'papla/backend'
4
- require 'papla/version'
5
-
6
1
  module Papla
2
+ autoload :Backend, 'papla/backend'
3
+ autoload :FixnumConverter, 'papla/fixnum_converter'
4
+ autoload :FloatConverter, 'papla/float_converter'
5
+ autoload :MoneyConverter, 'papla/money_converter'
6
+
7
7
  # Converts a number to Polish or English words,
8
8
  # capitalizing the first letter of the whole phrase.
9
9
  #
@@ -40,96 +40,35 @@ module Papla
40
40
  # Papla[87.654321] # => "Osiemdziesiąt siedem 65/100"
41
41
  # Papla[2.999] # => "Trzy 00/100"
42
42
  #
43
+ # If you're using the `money` gem, you can pass in an instance
44
+ # of `Money` to Papla. The returned string will contain the dollars
45
+ # part as words, the cents part as 'xx/100' and the currency
46
+ # string (e.g. `EUR`).
47
+ #
48
+ # Example:
49
+ #
50
+ # eleven_and_a_half_pounds = Money.new(1150, 'GBP')
51
+ # Papla[eleven_and_a_half_pounds] # => "Jedenaście 50/100 GBP"
52
+ #
53
+ # I18n.locale = :en
54
+ # discounted_price = Money.new(9999, 'PLN')
55
+ # Papla[discounted_price] # => "Ninety nine 99/100 PLN"
56
+ #
43
57
  # @param [Fixnum] number the number to convert
44
58
  # @return [String] the phrase in Polish or English
45
59
  def self.[](number)
46
- validate!(number)
47
- number = prepare(number)
48
- basic_number = number.to_i
49
- basic_phrase = build_basic_phrase(basic_number)
50
-
51
- case number
52
- when Float; append_cents(basic_phrase, number)
53
- else basic_phrase
54
- end
60
+ converter = converter_for(number)
61
+ converter.new.convert(number)
55
62
  end
56
63
 
57
64
  private
58
65
 
59
- def self.prepare(number)
66
+ def self.converter_for(number)
60
67
  case number
61
- when Float; number.round(2)
62
- else number
63
- end
64
- end
65
-
66
- def self.build_basic_phrase(basic_number)
67
- if basic_number.zero?
68
- spell_zero
69
- else
70
- groups = group(basic_number)
71
- groups_as_words = convert_groups(groups)
72
- groups_as_words.flatten.join(' ')
73
- end.capitalize
74
- end
75
-
76
- def self.group(number)
77
- groups = []
78
-
79
- while number > 0
80
- number, group = number.divmod(1000)
81
- groups.unshift(group)
68
+ when defined?(Money) && Money then MoneyConverter
69
+ when Float then FloatConverter
70
+ when Fixnum then FixnumConverter
71
+ else raise ArgumentError, "Unsupported type: #{klass}"
82
72
  end
83
-
84
- groups
85
- end
86
-
87
- def self.convert_groups(groups)
88
- bound = groups.count - 1
89
- result = []
90
-
91
- groups.each_with_index do |group, i|
92
- if group > 0
93
- result << convert_small_number(group)
94
- result << spell_rank(bound - i, group) if i < bound
95
- end
96
- end
97
-
98
- result
99
- end
100
-
101
- def self.convert_small_number(number)
102
- if number.zero?
103
- []
104
- elsif number < 20
105
- [spell_ones(number)]
106
- elsif number < 100
107
- tens, remainder = number.divmod(10)
108
- [spell_tens(tens), convert_small_number(remainder)]
109
- else
110
- hundreds, remainder = number.divmod(100)
111
- [spell_hundreds(hundreds), convert_small_number(remainder)]
112
- end
113
- end
114
-
115
- def self.validate!(number)
116
- max = 999_999_999
117
- raise ArgumentError, "#{number} is too big, only numbers up to #{max} are supported" if number > max
118
- end
119
-
120
- def self.append_cents(basic_phrase, number)
121
- cents = 100 * (number.to_d - number.to_i)
122
- spell_cents(basic_phrase, cents)
123
- end
124
-
125
- def self.backend
126
- @backend ||= Backend.new
127
73
  end
128
-
129
- def self.spell_zero; backend.zero; end
130
- def self.spell_ones(index); backend.ones(index); end
131
- def self.spell_tens(index); backend.tens(index); end
132
- def self.spell_hundreds(index); backend.hundreds(index); end
133
- def self.spell_rank(index, number); backend.rank(index, number); end
134
- def self.spell_cents(basic_phrase, cents); backend.cents(basic_phrase, cents); end
135
74
  end
@@ -0,0 +1,71 @@
1
+ module Papla
2
+ class FixnumConverter
3
+ def convert(number)
4
+ validate!(number)
5
+
6
+ if number.zero?
7
+ spell_zero
8
+ else
9
+ groups = group(number)
10
+ groups_as_words = convert_groups(groups)
11
+ groups_as_words.flatten.join(' ')
12
+ end.capitalize
13
+ end
14
+
15
+ private
16
+
17
+ def validate!(number)
18
+ max = 999_999_999
19
+ raise ArgumentError, "#{number} is too big, only numbers up to #{max} are supported" if number > max
20
+ end
21
+
22
+ def group(number)
23
+ groups = []
24
+
25
+ while number > 0
26
+ number, group = number.divmod(1000)
27
+ groups.unshift(group)
28
+ end
29
+
30
+ groups
31
+ end
32
+
33
+ def convert_groups(groups)
34
+ bound = groups.count - 1
35
+ result = []
36
+
37
+ groups.each_with_index do |group, i|
38
+ if group > 0
39
+ result << convert_small_number(group)
40
+ result << spell_rank(bound - i, group) if i < bound
41
+ end
42
+ end
43
+
44
+ result
45
+ end
46
+
47
+ def convert_small_number(number)
48
+ if number.zero?
49
+ []
50
+ elsif number < 20
51
+ [spell_ones(number)]
52
+ elsif number < 100
53
+ tens, remainder = number.divmod(10)
54
+ [spell_tens(tens), convert_small_number(remainder)]
55
+ else
56
+ hundreds, remainder = number.divmod(100)
57
+ [spell_hundreds(hundreds), convert_small_number(remainder)]
58
+ end
59
+ end
60
+
61
+ def backend
62
+ @backend ||= Backend.new
63
+ end
64
+
65
+ def spell_zero; backend.zero; end
66
+ def spell_ones(index); backend.ones(index); end
67
+ def spell_tens(index); backend.tens(index); end
68
+ def spell_hundreds(index); backend.hundreds(index); end
69
+ def spell_rank(index, number); backend.rank(index, number); end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ require 'bigdecimal'
2
+ require 'bigdecimal/util'
3
+
4
+ module Papla
5
+ class FloatConverter < FixnumConverter
6
+ def convert(number)
7
+ number = number.round(2)
8
+ append_cents(super(number.to_i), number)
9
+ end
10
+
11
+ private
12
+
13
+ def append_cents(basic_phrase, number)
14
+ cents = 100 * (number.to_d - number.to_i)
15
+ cents = cents.round(2)
16
+ spell_cents(basic_phrase, cents)
17
+ end
18
+
19
+ def spell_cents(basic_phrase, cents); backend.cents(basic_phrase, cents); end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module Papla
2
+ class MoneyConverter < FloatConverter
3
+ def convert(number)
4
+ append_currency(super(number.to_f.round(2)), number)
5
+ end
6
+
7
+ private
8
+
9
+ def append_currency(phrase, money)
10
+ '%s %s' % [phrase, money.currency_as_string]
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Papla
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency 'i18n'
22
22
 
23
+ s.add_development_dependency 'money'
23
24
  s.add_development_dependency 'rspec', '>= 2.8.0'
24
25
  s.add_development_dependency 'rake'
25
26
  end
@@ -236,4 +236,11 @@ describe Papla, 'en' do
236
236
  subject[111_111.2].should eq('One hundred eleven thousand one hundred eleven 20/100')
237
237
  end
238
238
  end
239
+
240
+ describe 'money' do
241
+ it 'concatenates dollars, cents and currency' do
242
+ discounted_price = Money.new(9999, 'PLN')
243
+ Papla[discounted_price].should eq('Ninety nine 99/100 PLN')
244
+ end
245
+ end
239
246
  end
@@ -237,4 +237,11 @@ describe Papla, 'pl' do
237
237
  subject[111_111.2].should eq('Sto jedenaście tysięcy sto jedenaście 20/100')
238
238
  end
239
239
  end
240
+
241
+ describe 'money' do
242
+ it 'concatenates dollars, cents and currency' do
243
+ eleven_and_a_half_pounds = Money.new(1150, 'GBP')
244
+ Papla[eleven_and_a_half_pounds].should eq('Jedenaście 50/100 GBP')
245
+ end
246
+ end
240
247
  end
@@ -1 +1,2 @@
1
+ require 'money'
1
2
  require 'papla'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-28 00:00:00.000000000 Z
12
+ date: 2012-04-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &9548560 !ruby/object:Gem::Requirement
16
+ requirement: &17749220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *9548560
24
+ version_requirements: *17749220
25
+ - !ruby/object:Gem::Dependency
26
+ name: money
27
+ requirement: &17748560 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *17748560
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &9547300 !ruby/object:Gem::Requirement
38
+ requirement: &17763960 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 2.8.0
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *9547300
46
+ version_requirements: *17763960
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rake
38
- requirement: &9546380 !ruby/object:Gem::Requirement
49
+ requirement: &17763380 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *9546380
57
+ version_requirements: *17763380
47
58
  description: Papla is a Ruby gem that allows you to convert numbers into Polish and
48
59
  English words.
49
60
  email:
@@ -62,10 +73,13 @@ files:
62
73
  - Rakefile
63
74
  - lib/papla.rb
64
75
  - lib/papla/backend.rb
76
+ - lib/papla/fixnum_converter.rb
77
+ - lib/papla/float_converter.rb
65
78
  - lib/papla/locale/en.rb
66
79
  - lib/papla/locale/en.yml
67
80
  - lib/papla/locale/pl.rb
68
81
  - lib/papla/locale/pl.yml
82
+ - lib/papla/money_converter.rb
69
83
  - lib/papla/version.rb
70
84
  - papla.gemspec
71
85
  - spec/en_spec.rb