amount_inflector 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -9,6 +9,6 @@ source "http://rubygems.org"
9
9
 
10
10
  group :development do
11
11
  gem "rspec-rails", "~> 2.4.0"
12
- gem "bundler", "~> 1.2.1"
12
+ gem "bundler"
13
13
  gem "jeweler", "~> 1.6.4"
14
14
  end
data/README.md CHANGED
@@ -27,6 +27,11 @@ And then:
27
27
  bundle
28
28
  ```
29
29
 
30
+ Release notes
31
+ -------------
32
+ v.3.0.1
33
+ - added support for inflection :promet
34
+
30
35
  Contributing to amount_inflector
31
36
  ---------------------------------
32
37
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.0.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "amount_inflector"
8
- s.version = "3.0.0"
8
+ s.version = "3.0.1"
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-11-06"
12
+ s.date = "2012-11-13"
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 = [
@@ -43,16 +43,16 @@ Gem::Specification.new do |s|
43
43
 
44
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
45
  s.add_development_dependency(%q<rspec-rails>, ["~> 2.4.0"])
46
- s.add_development_dependency(%q<bundler>, ["~> 1.2.1"])
46
+ s.add_development_dependency(%q<bundler>, [">= 0"])
47
47
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
48
48
  else
49
49
  s.add_dependency(%q<rspec-rails>, ["~> 2.4.0"])
50
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
50
+ s.add_dependency(%q<bundler>, [">= 0"])
51
51
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
52
52
  end
53
53
  else
54
54
  s.add_dependency(%q<rspec-rails>, ["~> 2.4.0"])
55
- s.add_dependency(%q<bundler>, ["~> 1.2.1"])
55
+ s.add_dependency(%q<bundler>, [">= 0"])
56
56
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
57
57
  end
58
58
  end
@@ -11,6 +11,7 @@ class AmountInflector
11
11
  :tisuca => { one:"tisuću", few:"tisuće", many:"tisuća" },
12
12
  :milijun => { one:"milijun", few:"milijuna", many:"milijuna" },
13
13
  :milijarda => { one:"milijarda", few:"milijarde", many:"milijardi" }
14
+ :promet => { one:"promet", few:"prometa", many:"prometa" }
14
15
  }
15
16
 
16
17
  def self.inflect(amount, unit)
@@ -10,10 +10,8 @@ class NumberToKune
10
10
  WORDS = {
11
11
  "0" => "nula",
12
12
  "1" => "jedna",
13
- "1_tisuca" => "jednu",
14
- "1_milijun" => "jedan",
15
- "2" => "dvije",
16
- "2_milijun" => "dva",
13
+ "1_tisuca" => "jednu", "1_milijun" => "jedan",
14
+ "2" => "dvije", "2_milijun" => "dva",
17
15
  "3" => "tri",
18
16
  "4" => "četiri",
19
17
  "5" => "pet",
@@ -32,10 +30,8 @@ class NumberToKune
32
30
  "18" => "osamnaest",
33
31
  "19" => "devetnaest",
34
32
  "20" => "dvadeset",
35
- "21" => "dvadesetjedna",
36
- "21_milijun" => "dvadesetjedan",
37
- "22" => "dvadesetdvije",
38
- "22_milijun" => "dvadesetdva",
33
+ "21" => "dvadesetjedna", "21_milijun" => "dvadesetjedan",
34
+ "22" => "dvadesetdvije", "22_milijun" => "dvadesetdva",
39
35
  "30" => "trideset",
40
36
  "40" => "četrdeset",
41
37
  "50" => "pedeset",
@@ -69,48 +65,48 @@ class NumberToKune
69
65
  result += " i #{translate_to_words(after_decimal.to_s, '')} #{AmountInflector.inflect_unit(after_decimal.to_i, :lipa)}"
70
66
  end
71
67
 
72
- private
73
-
74
68
  def translate_to_words(amount, in_words, unit = nil)
75
- return in_words if amount.nil? || amount.size == 0 || amount.gsub("0","").size == 0
69
+ return in_words if amount.nil? || amount.size == 0 || amount.gsub("0","").size == 0
70
+ return '' if amount == "1" && !unit.nil?
71
+ raise "Nisu podrzani iznosi preko bilijun, a poslan je iznos #{amount}" if amount.to_i >= 1_000_000_000_000
76
72
 
77
73
  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)
74
+ if amount.to_i >= 1000
75
+ decompose(amount, in_words)
95
76
  else
96
- raise "Nisu podrzani iznosi preko bilijun, a poslan je iznos #{amount}"
77
+ in_words += replaced_word(amount, unit)
78
+ translate_to_words(remove_first_n(amount, replaced_size(amount, unit)), in_words, unit)
97
79
  end
98
80
  end
99
81
 
82
+ def replaced_word(amount, unit)
83
+ as_word(amount, unit) || as_word(zeroed(amount), unit)
84
+ end
85
+
86
+ def replaced_size(amount, unit)
87
+ as_word(amount, unit).nil? ? 1 : amount.size
88
+ end
89
+
90
+ def zeroed(amount)
91
+ amount[0] + "0" * (amount.size - 1)
92
+ end
93
+
100
94
  def as_word(amount, unit = nil)
101
- (!unit.nil? && !WORDS["#{amount}_#{unit}"].nil?) ? WORDS["#{amount}_#{unit}"] : WORDS[amount]
95
+ WORDS["#{amount}_#{unit.nil? ? '' : unit}"] || WORDS[amount]
96
+ end
97
+
98
+ def remove_first_n(source, n = 1)
99
+ source[n..-1]
102
100
  end
103
101
 
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
102
+ def amount_unit(amount)
103
+ KOEFS.select { |key,value| amount.to_i >= value }.map { |key,value| key }.reverse[0]
108
104
  end
109
105
 
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)
106
+ def decompose(amount, in_words)
107
+ unit = amount_unit(amount)
108
+ without_unit = (amount.to_i / KOEFS.fetch(unit)).to_s
109
+ in_words += translate_to_words(without_unit.to_s, '', unit) + AmountInflector.inflect_unit(without_unit.to_i, unit)
110
+ translate_to_words(remove_first_n(amount, without_unit.size), in_words, unit)
115
111
  end
116
112
  end
@@ -2,17 +2,22 @@
2
2
  require 'amount_inflector/number_to_kune.rb'
3
3
 
4
4
  describe NumberToKune do
5
- it 'converts to kunas' do
5
+ it 'converts to kunas under 1000' do
6
6
  NumberToKune.convert(21).should == "dvadesetjedna kuna i nula lipa"
7
7
  NumberToKune.convert(100).should == "sto kuna i nula lipa"
8
8
  NumberToKune.convert(200).should == "dvjesto kuna i nula lipa"
9
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
10
  NumberToKune.convert(10).should == "deset kuna i nula lipa"
12
11
  NumberToKune.convert(3).should == "tri kune i nula lipa"
13
12
  NumberToKune.convert(18).should == "osamnaest kuna i nula lipa"
14
13
  NumberToKune.convert(24).should == "dvadesetčetiri kune i nula lipa"
15
14
  NumberToKune.convert(27).should == "dvadesetsedam kuna i nula lipa"
15
+ NumberToKune.convert(22).should == "dvadesetdvije kune i nula lipa"
16
+ NumberToKune.convert(0.00).should == "nula kuna i nula lipa"
17
+ end
18
+
19
+ it 'converts to kunas over 1000' do
20
+ NumberToKune.convert(1_200.26).should == "tisućudvjesto kuna i dvadesetšest lipa"
16
21
  NumberToKune.convert(1000).should == "tisuću kuna i nula lipa"
17
22
  NumberToKune.convert(101_000).should == "stojednutisuću kuna i nula lipa"
18
23
  NumberToKune.convert(1965).should == "tisućudevetstošezdesetpet kuna i nula lipa"
@@ -33,10 +38,14 @@ describe NumberToKune do
33
38
  NumberToKune.convert(926_543_864).should == "devetstodvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
34
39
  NumberToKune.convert(19_926_543_864).should == "devetnaestmilijardidevetstodvadesetšestmilijunapetstočetrdesettritisućeosamstošezdesetčetiri kune i nula lipa"
35
40
  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
41
  NumberToKune.convert(22_000_000).should == "dvadesetdvamilijuna kuna i nula lipa"
38
- NumberToKune.convert(0.00).should == "nula kuna i nula lipa"
39
42
  NumberToKune.convert(97_543.21).should == "devedesetsedamtisućapetstočetrdesettri kune i dvadesetjedna lipa"
40
43
  end
41
44
 
45
+ it 'reports on a too big number of a number' do
46
+ lambda do
47
+ NumberToKune.convert(10000000000000)
48
+ end.should raise_error 'Nisu podrzani iznosi preko bilijun, a poslan je iznos 10000000000000'
49
+ end
50
+
42
51
  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: 3.0.0
4
+ version: 3.0.1
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-11-06 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
@@ -32,17 +32,17 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 1.2.1
37
+ version: '0'
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 1.2.1
45
+ version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: jeweler
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  segments:
98
98
  - 0
99
- hash: -1772161144060008263
99
+ hash: 866616536439487970
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  none: false
102
102
  requirements: