papla 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ### 0.0.2 (2011-01-09)
2
+
3
+ * display decimal part of floats as cents/100
4
+
5
+ ### 0.0.1 (2011-01-03)
6
+
7
+ * initial release
8
+ * support converting numbers from 0 up to 999 999 999 to Polish words
data/README.md CHANGED
@@ -1,19 +1,55 @@
1
- # Papla
1
+ # Papla [![Build Status](https://secure.travis-ci.org/exviva/papla.png)](http://travis-ci.org/exviva/papla)
2
2
 
3
3
  Papla is a Ruby gem that allows you to convert numbers into Polish words (e.g. `153` into `"sto pięćdziesiąt trzy"`), including the decimal part as cents and currency symbol. Its primary use case are invoices, where the total amount has to be displayed as words at the bottom line.
4
4
 
5
- ## Basic examples
5
+ ## Installation
6
+
7
+ To install Papla, run
8
+
9
+ gem install papla
10
+
11
+ or add
12
+
13
+ ```ruby
14
+ gem 'papla'
15
+ ```
16
+
17
+ to your `Gemfile`.
18
+
19
+ ## Usage
20
+
21
+ ### Basic examples
6
22
 
7
23
  ```ruby
8
- Papla[158] # => 'Sto pięćdziesiąt'
9
- Papla[1_234] # => 'Tysiąc dwieście trzydzieści cztery'
10
- Papla[987_654_321] # => 'Dziewięćset osiemdziesiąt siedem milionów sześćset pięćdziesiąt cztery tysiące trzysta dwadzieścia jeden'
24
+ Papla[158] # => "Sto pięćdziesiąt osiem"
25
+ Papla[1_234] # => "Tysiąc dwieście trzydzieści cztery"
26
+ Papla[987_654_321] # => "Dziewięćset osiemdziesiąt siedem milionów sześćset pięćdziesiąt cztery tysiące trzysta dwadzieścia jeden"
11
27
  ```
12
28
 
13
- ## Cents and currency
29
+ ### Cents and currency
30
+
31
+ #### Cents
32
+
33
+ When given a `Float`, Papla will assume that the decimal part represents cents.
34
+ It will then round the number using `Float#round` to two decimal places,
35
+ and append the number of cents divided by hundred to the resulting string.
36
+
37
+ Example:
38
+
39
+ ```ruby
40
+ Papla[1.0] # => "Jeden 00/100"
41
+ Papla[87.654321] # => "Osiemdziesiąt siedem 65/100"
42
+ Papla[2.999] # => "Trzy 00/100"
43
+ ```
44
+
45
+ #### Currency
14
46
 
15
47
  This feature is planned for future releases.
16
48
 
17
- ## Integration with Money, I18n and Rails
49
+ ### Integration with Money, I18n and Rails
18
50
 
19
51
  This feature is planned for future releases.
52
+
53
+ ## Documentation
54
+
55
+ Documentation is available at [RubyDoc.info](http://rubydoc.info/github/exviva/papla/master/frames).
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.ruby_opts = '-w'
6
+ end
7
+
8
+ task default: :spec
data/lib/papla.rb CHANGED
@@ -2,15 +2,50 @@
2
2
  require "papla/version"
3
3
 
4
4
  module Papla
5
+ # Converts a number to Polish words, capitalizing
6
+ # the first letter of the whole phrase.
7
+ #
8
+ # Currently numbers from 0 up to 999 999 999
9
+ # are supported. If you pass a bigger number,
10
+ # an <tt>ArgumentError</tt> is raised.
11
+ #
12
+ # To convert a number, simply call:
13
+ #
14
+ # Papla[your_number]
15
+ #
16
+ # Examples:
17
+ #
18
+ # Papla[0] # => "Zero"
19
+ # Papla[22] # => "Dwadzieścia dwa"
20
+ # Papla[150] # => "Sto pięćdziesiąt"
21
+ # Papla[999] # => "Dziewięćset dziewięćdziesiąt dziewięć"
22
+ # Papla[12345] # => "Dwanaście tysięcy trzysta czterdzieści pięć"
23
+ # Papla[1_000_001] # => "Jeden milion jeden"
24
+ #
25
+ # When given a <tt>Float</tt>, Papla will assume that
26
+ # the decimal part represents cents. It will then
27
+ # round the number using <tt>Float#round</tt> to
28
+ # two decimal places, and append the number
29
+ # of cents divided by hundred to the resulting string.
30
+ #
31
+ # Example:
32
+ #
33
+ # Papla[1.0] # => "Jeden 00/100"
34
+ # Papla[87.654321] # => "Osiemdziesiąt siedem 65/100"
35
+ # Papla[2.999] # => "Trzy 00/100"
36
+ #
37
+ # @param [Fixnum] number the number to convert
38
+ # @return [String] the phrase in Polish
5
39
  def self.[](number)
6
40
  validate!(number)
7
- if number.zero?
8
- ZERO
9
- else
10
- groups = group(number)
11
- groups_as_words = convert_groups(groups)
12
- groups_as_words.flatten.join(' ')
13
- end.capitalize
41
+ number = prepare(number)
42
+ basic_number = number.to_i
43
+ basic_phrase = build_basic_phrase(basic_number)
44
+
45
+ case number
46
+ when Float; append_cents(basic_phrase, number)
47
+ else basic_phrase
48
+ end
14
49
  end
15
50
 
16
51
  private
@@ -40,6 +75,23 @@ module Papla
40
75
  ['miliardów', 'miliard', 'miliardy'].freeze
41
76
  ].freeze
42
77
 
78
+ def self.prepare(number)
79
+ case number
80
+ when Float; number.round(2)
81
+ else number
82
+ end
83
+ end
84
+
85
+ def self.build_basic_phrase(basic_number)
86
+ if basic_number.zero?
87
+ ZERO
88
+ else
89
+ groups = group(basic_number)
90
+ groups_as_words = convert_groups(groups)
91
+ groups_as_words.flatten.join(' ')
92
+ end.capitalize
93
+ end
94
+
43
95
  def self.group(number)
44
96
  groups = []
45
97
 
@@ -104,4 +156,9 @@ module Papla
104
156
  max = 999_999_999
105
157
  raise ArgumentError, "#{number} is too big, only numbers up to #{max} are supported" if number > max
106
158
  end
159
+
160
+ def self.append_cents(basic_phrase, number)
161
+ cents = 100 * (number - number.to_i)
162
+ "%s %02d/100" % [basic_phrase, cents]
163
+ end
107
164
  end
data/lib/papla/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Papla
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/papla.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- # -*- encoding: utf-8 -*-
1
+ # encoding: utf-8
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
  require "papla/version"
4
4
 
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- s.add_development_dependency 'rspec', '>= 2.8.0.rc2'
23
- # s.add_runtime_dependency "rest-client"
21
+ s.add_development_dependency 'rspec', '>= 2.8.0'
22
+ s.add_development_dependency 'rake'
24
23
  end
data/spec/papla_spec.rb CHANGED
@@ -211,6 +211,20 @@ describe Papla do
211
211
  end
212
212
 
213
213
  it 'does not support billions' do
214
- proc { subject[1_000_000_000] }.should raise_error(ArgumentError)
214
+ expect { subject[1_000_000_000] }.to raise_error(ArgumentError)
215
+ end
216
+
217
+ describe 'floats' do
218
+ it 'appends the decimal part as cents' do
219
+ subject[1.23].should eq('Jeden 23/100')
220
+ end
221
+
222
+ it 'displays two decimal digits' do
223
+ subject[2.0].should eq('Dwa 00/100')
224
+ end
225
+
226
+ it 'rounds to two decimal places' do
227
+ subject[3.456].should eq('Trzy 46/100')
228
+ end
215
229
  end
216
230
  end
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-03 00:00:00.000000000 Z
12
+ date: 2012-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &19504300 !ruby/object:Gem::Requirement
16
+ requirement: &9069800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 2.8.0.rc2
21
+ version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *19504300
24
+ version_requirements: *9069800
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &9069220 !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: *9069220
25
36
  description: Papla is a Ruby gem that allows you to convert numbers into Polish words
26
37
  (e.g. 153 into "sto pięćdziesiąt trzy"), including the decimal part as cents and
27
38
  currency symbol. Its primary use case are invoices, where the total amount has to
@@ -35,6 +46,8 @@ files:
35
46
  - .gitignore
36
47
  - .rspec
37
48
  - .rvmrc
49
+ - .travis.yml
50
+ - CHANGELOG.md
38
51
  - Gemfile
39
52
  - README.md
40
53
  - Rakefile