amount_inflector 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode # JRuby in 1.9 mode
5
+ - rbx-19mode
data/Gemfile CHANGED
@@ -8,8 +8,7 @@ source "http://rubygems.org"
8
8
  #
9
9
 
10
10
  group :development do
11
- gem "rspec-rails", "~> 2.3.0"
12
- gem "bundler", "~> 1.0.0"
11
+ gem "rspec-rails", "~> 2.4.0"
12
+ gem "bundler", "~> 1.2.1"
13
13
  gem "jeweler", "~> 1.6.4"
14
- gem "rcov", ">= 0"
15
14
  end
data/README.md CHANGED
@@ -1,26 +1,16 @@
1
1
  Amount Inflector
2
2
  ================
3
- Small gem for pluralizing year/month/week/day words in Croatian.
3
+ [![Build Status](https://travis-ci.org/drKreso/amount_inflector.png)](https://travis-ci.org/drKreso/amount_inflector)
4
4
 
5
- In English it is straightforward:
5
+ This gem can be used for:
6
6
 
7
- ```
8
- 1 week
9
- 2 weeks...
10
- 2002020 weeks
11
- ```
12
-
13
- In some other language it is not as simple, for example "week" in Croatian:
7
+ 1. converting number amounts to words in HRK currency
8
+ 2. pluralizing year/month/week/day and other words in Croatian that depend on :one, :few, :many rule
14
9
 
10
+ ```ruby
11
+ NumberToKune.convert(97_543.21) #=> "devedesetsedamtisućapetstočetrdesettri kune i dvadesetjedna lipa"
12
+ AmountInflector.inflect(77, :godina).to_s #=> 77 godina
15
13
  ```
16
- 1 tjedan
17
- 2 tjedna
18
- 3 tjedna
19
- 4 tjedna
20
- 5 tjedana....
21
- ```
22
-
23
- It turns out in Croatian it is dependable on trailing digits, and rather simple (3 lines rule).
24
14
 
25
15
  Usage
26
16
  ----------
@@ -37,33 +27,6 @@ And then:
37
27
  bundle
38
28
  ```
39
29
 
40
- In your code
41
-
42
- ```ruby
43
- AmountInflector.inflect(77, :godina).to_s #=> 77 godina
44
- ```
45
-
46
- Is It For Me?
47
- --------------
48
- This is basically reimplementation of stuff I18n provides with :one, :few and :many
49
-
50
- [Example for Polish](http://dev.netizer.pl/rails-i18n-and-zero-one-two-few-many-other.html)
51
-
52
- ```ruby
53
- # config/locales/pluralization.rb
54
- I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
55
- {
56
- :pl => {:'i18n.plural.rule' => lambda { |n| n == 1 ? :one : (2..4).include?(n % 10) && !(12..14).include?(n % 100) ? :few : :other }},
57
- }
58
- ```
59
- <blockquote>
60
- Only usage would be if you prefer not to use the whole I18n machinery for this simple task.
61
- </blockquote>
62
-
63
- Tweaking
64
- ----------
65
- Everything is in a sigle file (pluralization rules and translations) in lib/amount_inflector/amount_inflector.rb. That is the whole point, one small file for that.
66
-
67
30
  Contributing to amount_inflector
68
31
  ---------------------------------
69
32
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 3.0.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "amount_inflector"
8
- s.version = "2.0.1"
8
+ s.version = "3.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kresimir Bojcic"]
12
- s.date = "2012-06-09"
12
+ s.date = "2012-11-06"
13
13
  s.description = "Amount Inflector for year/month/week/day (Croatian)"
14
14
  s.email = "kresimir.bojcic@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".rspec",
22
+ ".travis.yml",
22
23
  "Gemfile",
23
24
  "LICENSE.txt",
24
25
  "README.md",
@@ -27,33 +28,32 @@ Gem::Specification.new do |s|
27
28
  "amount_inflector.gemspec",
28
29
  "lib/amount_inflector.rb",
29
30
  "lib/amount_inflector/amount_inflector.rb",
30
- "spec/amount_inflector/amount_inflector_spec.rb"
31
+ "lib/amount_inflector/number_to_kune.rb",
32
+ "spec/amount_inflector/amount_inflector_spec.rb",
33
+ "spec/amount_inflector/number_to_kune_spec.rb"
31
34
  ]
32
35
  s.homepage = "http://github.com/drKreso/amount_inflector"
33
36
  s.licenses = ["MIT"]
34
37
  s.require_paths = ["lib"]
35
- s.rubygems_version = "1.8.10"
38
+ s.rubygems_version = "1.8.24"
36
39
  s.summary = "Amount Inflector"
37
40
 
38
41
  if s.respond_to? :specification_version then
39
42
  s.specification_version = 3
40
43
 
41
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
- s.add_development_dependency(%q<rspec-rails>, ["~> 2.3.0"])
43
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<rspec-rails>, ["~> 2.4.0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
44
47
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
45
- s.add_development_dependency(%q<rcov>, [">= 0"])
46
48
  else
47
- s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
48
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_dependency(%q<rspec-rails>, ["~> 2.4.0"])
50
+ s.add_dependency(%q<bundler>, ["~> 1.2.1"])
49
51
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
50
- s.add_dependency(%q<rcov>, [">= 0"])
51
52
  end
52
53
  else
53
- s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
54
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<rspec-rails>, ["~> 2.4.0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.2.1"])
55
56
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
56
- s.add_dependency(%q<rcov>, [">= 0"])
57
57
  end
58
58
  end
59
59
 
@@ -1,2 +1,3 @@
1
1
  require 'amount_inflector/amount_inflector.rb'
2
+ require 'amount_inflector/number_to_kune.rb'
2
3
 
@@ -1,22 +1,34 @@
1
- class AmountInflector
1
+ #encoding:utf-8
2
2
 
3
+ class AmountInflector
3
4
  CONFIG = {
4
5
  :godina => { one:"godina", few:"godine", many:"godina" },
5
6
  :mjesec => { one:"mjesec", few:"mjeseca", many:"mjeseci" },
6
7
  :tjedan => { one:"tjedan", few:"tjedna", many:"tjedana" },
7
- :dan => { one:"dan", few:"dana", many:"dana"}
8
+ :dan => { one:"dan", few:"dana", many:"dana" },
9
+ :kuna => { one:"kuna", few:"kune", many:"kuna" },
10
+ :lipa => { one:"lipa", few:"lipe", many:"lipa" },
11
+ :tisuca => { one:"tisuću", few:"tisuće", many:"tisuća" },
12
+ :milijun => { one:"milijun", few:"milijuna", many:"milijuna" },
13
+ :milijarda => { one:"milijarda", few:"milijarde", many:"milijardi" }
8
14
  }
9
15
 
10
16
  def self.inflect(amount, unit)
11
- unit = unit.to_s.to_sym
17
+ unit = unit.to_s.to_sym
12
18
  raise "Inflection :#{unit} is unsupported" if CONFIG[unit].nil?
13
19
  "#{amount} #{CONFIG[unit][pluralize_form(amount)]}"
14
20
  end
15
21
 
22
+ def self.inflect_unit(amount, unit)
23
+ unit = unit.to_s.to_sym
24
+ raise "Inflection :#{unit} is unsupported" if CONFIG[unit].nil?
25
+ "#{CONFIG[unit][pluralize_form(amount)]}"
26
+ end
27
+
28
+
16
29
  def self.pluralize_form(n)
17
30
  return :many if (11..14).include?(n % 100)
18
31
  return :few if (2..4).include?(n % 10)
19
32
  (n % 10 == 1) ? :one : :many
20
33
  end
21
-
22
34
  end
@@ -0,0 +1,116 @@
1
+ #encoding:utf-8
2
+ require 'bigdecimal'
3
+
4
+ class NumberToKune
5
+ def self.convert(amount)
6
+ NumberToKune.new.convert(amount)
7
+ end
8
+
9
+ #needs to 'rhime' on "kuna"
10
+ WORDS = {
11
+ "0" => "nula",
12
+ "1" => "jedna",
13
+ "1_tisuca" => "jednu",
14
+ "1_milijun" => "jedan",
15
+ "2" => "dvije",
16
+ "2_milijun" => "dva",
17
+ "3" => "tri",
18
+ "4" => "četiri",
19
+ "5" => "pet",
20
+ "6" => "šest",
21
+ "7" => "sedam",
22
+ "8" => "osam",
23
+ "9" => "devet",
24
+ "10" => "deset",
25
+ "11" => "jedanaest",
26
+ "12" => "dvanaest",
27
+ "13" => "trinaest",
28
+ "14" => "četrnaest",
29
+ "15" => "petnaest",
30
+ "16" => "šesnaest",
31
+ "17" => "sedamnaest",
32
+ "18" => "osamnaest",
33
+ "19" => "devetnaest",
34
+ "20" => "dvadeset",
35
+ "21" => "dvadesetjedna",
36
+ "21_milijun" => "dvadesetjedan",
37
+ "22" => "dvadesetdvije",
38
+ "22_milijun" => "dvadesetdva",
39
+ "30" => "trideset",
40
+ "40" => "četrdeset",
41
+ "50" => "pedeset",
42
+ "60" => "šezdeset",
43
+ "70" => "sedamdeset",
44
+ "80" => "osamdeset",
45
+ "90" => "devedeset" ,
46
+ "100" => "sto",
47
+ "200" => "dvjesto",
48
+ "300" => "tristo",
49
+ "400" => "četiristo",
50
+ "500" => "petsto",
51
+ "600" => "šesto",
52
+ "700" => "sedamsto",
53
+ "800" => "osamsto",
54
+ "900" => "devetsto"
55
+ }
56
+
57
+ KOEFS =
58
+ {
59
+ :tisuca => 1_000,
60
+ :milijun => 1_000_000,
61
+ :milijarda => 1_000_000_000
62
+ }
63
+
64
+ def convert(amount)
65
+ return "nula kuna i nula lipa" if BigDecimal.new(amount.to_s) == BigDecimal.new("0.0")
66
+
67
+ after_decimal = (amount - amount.to_i).round(2) * 100
68
+ result = "#{translate_to_words(amount.to_i.to_s, '')} #{AmountInflector.inflect_unit(amount.to_i, :kuna)}"
69
+ result += " i #{translate_to_words(after_decimal.to_s, '')} #{AmountInflector.inflect_unit(after_decimal.to_i, :lipa)}"
70
+ end
71
+
72
+ private
73
+
74
+ def translate_to_words(amount, in_words, unit = nil)
75
+ return in_words if amount.nil? || amount.size == 0 || amount.gsub("0","").size == 0
76
+
77
+ amount = amount.to_i.to_s
78
+ if amount.to_i < 1000
79
+ if amount.to_i < 1_000
80
+ if as_word(amount, unit) != nil
81
+ in_words += as_word(amount, unit)
82
+ decoded = amount.size
83
+ else
84
+ in_words += as_word(amount[0] + "0" * (amount.size - 1), unit)
85
+ decoded = 1
86
+ end
87
+ translate_to_words(take_off(amount, decoded), in_words, unit)
88
+ end
89
+ elsif amount.to_i < 1_000_000
90
+ decompose(amount, :tisuca, in_words)
91
+ elsif amount.to_i < 1_000_000_000
92
+ decompose(amount, :milijun, in_words)
93
+ elsif amount.to_i < 1_000_000_000_000
94
+ decompose(amount, :milijarda, in_words)
95
+ else
96
+ raise "Nisu podrzani iznosi preko bilijun, a poslan je iznos #{amount}"
97
+ end
98
+ end
99
+
100
+ def as_word(amount, unit = nil)
101
+ (!unit.nil? && !WORDS["#{amount}_#{unit}"].nil?) ? WORDS["#{amount}_#{unit}"] : WORDS[amount]
102
+ end
103
+
104
+ def take_off(source, number_to_take_off = 1)
105
+ result = source.reverse
106
+ (0...number_to_take_off).each { result.chop! }
107
+ result.reverse
108
+ end
109
+
110
+ def decompose(amount, unit, in_words)
111
+ bez = (amount.to_i / KOEFS.fetch(unit)).to_s
112
+ in_words += translate_to_words(bez.to_s, '', unit) unless bez == "1"
113
+ in_words += AmountInflector.inflect_unit(bez.to_i, unit)
114
+ translate_to_words(amount[bez.size..amount.size - 1], in_words, unit)
115
+ end
116
+ end
@@ -1,4 +1,4 @@
1
- require 'amount_inflector.rb'
1
+ require 'amount_inflector/amount_inflector.rb'
2
2
 
3
3
  describe AmountInflector do
4
4
 
@@ -0,0 +1,42 @@
1
+ #encoding:utf-8
2
+ require 'amount_inflector/number_to_kune.rb'
3
+
4
+ describe NumberToKune do
5
+ it 'converts to kunas' do
6
+ NumberToKune.convert(21).should == "dvadesetjedna kuna i nula lipa"
7
+ NumberToKune.convert(100).should == "sto kuna i nula lipa"
8
+ NumberToKune.convert(200).should == "dvjesto kuna i nula lipa"
9
+ NumberToKune.convert(200.26).should == "dvjesto kuna i dvadesetšest lipa"
10
+ NumberToKune.convert(1_200.26).should == "tisućudvjesto kuna i dvadesetšest lipa"
11
+ NumberToKune.convert(10).should == "deset kuna i nula lipa"
12
+ NumberToKune.convert(3).should == "tri kune i nula lipa"
13
+ NumberToKune.convert(18).should == "osamnaest kuna i nula lipa"
14
+ NumberToKune.convert(24).should == "dvadesetčetiri kune i nula lipa"
15
+ NumberToKune.convert(27).should == "dvadesetsedam kuna i nula lipa"
16
+ NumberToKune.convert(1000).should == "tisuću kuna i nula lipa"
17
+ NumberToKune.convert(101_000).should == "stojednutisuću kuna i nula lipa"
18
+ NumberToKune.convert(1965).should == "tisućudevetstošezdesetpet kuna i nula lipa"
19
+ NumberToKune.convert(1965.337).should == "tisućudevetstošezdesetpet kuna i tridesetčetiri lipe"
20
+ NumberToKune.convert(9_875).should == "devettisućaosamstosedamdesetpet kuna i nula lipa"
21
+ NumberToKune.convert(18_654).should == "osamnaesttisućašestopedesetčetiri kune i nula lipa"
22
+ NumberToKune.convert(200_0000).should == "dvamilijuna kuna i nula lipa"
23
+ NumberToKune.convert(26_432).should == "dvadesetšesttisućačetiristotridesetdvije kune i nula lipa"
24
+ NumberToKune.convert(97_543).should == "devedesetsedamtisućapetstočetrdesettri kune i nula lipa"
25
+ NumberToKune.convert(197_543).should == "stodevedesetsedamtisućapetstočetrdesettri kune i nula lipa"
26
+ NumberToKune.convert(999_999).should == "devetstodevedesetdevettisućadevetstodevedesetdevet kuna i nula lipa"
27
+ NumberToKune.convert(121_000_000).should == "stodvadesetjedanmilijun kuna i nula lipa"
28
+ NumberToKune.convert(21_000_000_000).should == "dvadesetjednamilijarda kuna i nula lipa"
29
+ NumberToKune.convert(121_000_000_000).should == "stodvadesetjednamilijarda kuna i nula lipa"
30
+ NumberToKune.convert(21_000_000).should == "dvadesetjedanmilijun kuna i nula lipa"
31
+ NumberToKune.convert(1_000_000).should == "milijun kuna i nula lipa"
32
+ NumberToKune.convert(26_543_864).should == "dvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
33
+ NumberToKune.convert(926_543_864).should == "devetstodvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
34
+ NumberToKune.convert(19_926_543_864).should == "devetnaestmilijardidevetstodvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
35
+ NumberToKune.convert(121_926_543_864).should == "stodvadesetjednamilijardadevetstodvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
36
+ NumberToKune.convert(22).should == "dvadesetdvije kune i nula lipa"
37
+ NumberToKune.convert(22_000_000).should == "dvadesetdvamilijuna kuna i nula lipa"
38
+ NumberToKune.convert(0.00).should == "nula kuna i nula lipa"
39
+ NumberToKune.convert(97_543.21).should == "devedesetsedamtisućapetstočetrdesettri kune i dvadesetjedna lipa"
40
+ end
41
+
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amount_inflector
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 3.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,43 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-09 00:00:00.000000000Z
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
16
- requirement: &70192387727880 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 2.3.0
21
+ version: 2.4.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70192387727880
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.4.0
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: bundler
27
- requirement: &70192387726780 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
31
36
  - !ruby/object:Gem::Version
32
- version: 1.0.0
37
+ version: 1.2.1
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70192387726780
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.2.1
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: jeweler
38
- requirement: &70192387726000 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,18 +53,12 @@ dependencies:
43
53
  version: 1.6.4
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70192387726000
47
- - !ruby/object:Gem::Dependency
48
- name: rcov
49
- requirement: &70192387715820 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
50
57
  none: false
51
58
  requirements:
52
- - - ! '>='
59
+ - - ~>
53
60
  - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70192387715820
61
+ version: 1.6.4
58
62
  description: Amount Inflector for year/month/week/day (Croatian)
59
63
  email: kresimir.bojcic@gmail.com
60
64
  executables: []
@@ -65,6 +69,7 @@ extra_rdoc_files:
65
69
  files:
66
70
  - .document
67
71
  - .rspec
72
+ - .travis.yml
68
73
  - Gemfile
69
74
  - LICENSE.txt
70
75
  - README.md
@@ -73,7 +78,9 @@ files:
73
78
  - amount_inflector.gemspec
74
79
  - lib/amount_inflector.rb
75
80
  - lib/amount_inflector/amount_inflector.rb
81
+ - lib/amount_inflector/number_to_kune.rb
76
82
  - spec/amount_inflector/amount_inflector_spec.rb
83
+ - spec/amount_inflector/number_to_kune_spec.rb
77
84
  homepage: http://github.com/drKreso/amount_inflector
78
85
  licenses:
79
86
  - MIT
@@ -89,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
96
  version: '0'
90
97
  segments:
91
98
  - 0
92
- hash: -3694741291819528788
99
+ hash: -1772161144060008263
93
100
  required_rubygems_version: !ruby/object:Gem::Requirement
94
101
  none: false
95
102
  requirements:
@@ -98,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
105
  version: '0'
99
106
  requirements: []
100
107
  rubyforge_project:
101
- rubygems_version: 1.8.10
108
+ rubygems_version: 1.8.24
102
109
  signing_key:
103
110
  specification_version: 3
104
111
  summary: Amount Inflector