amount_inflector 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -11,4 +11,5 @@ group :development do
11
11
  gem "rspec-rails", "~> 2.3.0"
12
12
  gem "bundler", "~> 1.0.0"
13
13
  gem "jeweler", "~> 1.6.4"
14
+ gem "rcov", ">= 0"
14
15
  end
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Amount Inflector
2
2
  ================
3
- This is one of those things that are kinda cool but it never seems to be enough time to implement it. The basic idea is engine that will enable you to have amounts of certain units "humanized" in your native language.
3
+ Small gem for pluralizing year/month/week/day words in Croatian.
4
4
 
5
- In English it is quite simple:
5
+ In English it is straightforward:
6
6
 
7
7
  ```
8
8
  1 week
@@ -20,6 +20,8 @@ In some other language it is not as simple, for example "week" in Croatian:
20
20
  5 tjedana....
21
21
  ```
22
22
 
23
+ It turns out in Croatian it is dependable on trailing digits, and rather simple (3 lines rule).
24
+
23
25
  Usage
24
26
  ----------
25
27
 
@@ -33,70 +35,34 @@ And then:
33
35
 
34
36
  ```
35
37
  bundle
36
-
37
- rails g amount_inflector:install
38
- ```
39
-
40
- In your view:
41
- ```ruby
42
- = AmountInflector.new(77, :godina) #=> 77 godina
43
38
  ```
44
39
 
45
- Tweaking
46
- -----------
47
- As for now only Croatian year/month/week/day is supported(config/locales/amount_inflections.yml)
40
+ In your code
48
41
 
42
+ ```ruby
43
+ AmountInflector.inflect(77, :godina).to_s #=> 77 godina
49
44
  ```
50
- godina:
51
- default: godina
52
- 2: godine
53
- 3: godine
54
- 4: godine
55
- 12: godina
56
- 13: godina
57
- 14: godina
58
- mjesec:
59
- default: mjeseci
60
- 1: mjesec
61
- 2: mjeseca
62
- 3: mjeseca
63
- 4: mjeseca
64
- 11: mjeseci
65
- 12: mjeseci
66
- 13: mjeseci
67
- 14: mjeseci
68
- tjedan:
69
- default: tjedana
70
- 1: tjedan
71
- 2: tjedna
72
- 3: tjedna
73
- 4: tjedna
74
- 11: tjedana
75
- 12: tjedana
76
- 13: tjedana
77
- 14: tjedana
78
- dan:
79
- default: dana
80
- 1: dan
81
- 11: dana
82
- ```
83
-
84
- If you have 2011 months for example (mjesec) the algorithm is:
85
-
86
- * Check for 2011 in inflections
87
- * Check for 011 in inflections
88
- * Check for 11 in inflections => match it is "2011 mjeseci"
89
45
 
46
+ Is It For Me?
47
+ --------------
48
+ This is basically reimplementation of stuff I18n provides with :one, :few and :many
90
49
 
91
- If you have 77 years for example (godina) the algorithm is:
50
+ [Example for Polish](http://dev.netizer.pl/rails-i18n-and-zero-one-two-few-many-other.html)
92
51
 
93
- * Check for 77 in inflections
94
- * Check for 7 in inflections
95
- * Check for :default in inflections => match it is "7 godina"
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>
96
62
 
97
- Is It For Me?
98
- --------------
99
- I have a suspicion that Croatian model might work quite well for some other languages. Anyways if you have some exceptions but not that many and if they tend to depend on "trailing digits" then amount_inflector is a right way to go.
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.
100
66
 
101
67
  Contributing to amount_inflector
102
68
  ---------------------------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 2.0.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "amount_inflector"
8
- s.version = "1.0.0"
8
+ s.version = "2.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-01-06"
12
+ s.date = "2012-01-08"
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 = [
@@ -25,12 +25,8 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "amount_inflector.gemspec",
28
- "app/config/locales/amount_inflections.yml",
29
28
  "lib/amount_inflector.rb",
30
29
  "lib/amount_inflector/amount_inflector.rb",
31
- "lib/generators/amount_inflector/USAGE",
32
- "lib/generators/amount_inflector/install_generator.rb",
33
- "lib/generators/amount_inflector/templates/amount_inflections.yml",
34
30
  "spec/amount_inflector_spec.rb"
35
31
  ]
36
32
  s.homepage = "http://github.com/drKreso/amount_inflector"
@@ -46,15 +42,18 @@ Gem::Specification.new do |s|
46
42
  s.add_development_dependency(%q<rspec-rails>, ["~> 2.3.0"])
47
43
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
44
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
45
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
46
  else
50
47
  s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
51
48
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
49
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
50
+ s.add_dependency(%q<rcov>, [">= 0"])
53
51
  end
54
52
  else
55
53
  s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
56
54
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
55
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
58
57
  end
59
58
  end
60
59
 
@@ -1,29 +1,22 @@
1
- require 'yaml'
2
-
3
1
  class AmountInflector
4
- INFLECTIONS_CONFIG = 'app/config/locales/amount_inflections.yml'
5
-
6
- def initialize(amount, unit, inflections=nil)
7
- @amount = amount
8
- @unit_inflections = (inflections.nil? ? load_from_yaml_config : inflections)[unit.to_s]
9
- raise "Inflection :#{unit} is unsupported" if @unit_inflections.nil?
10
- raise "No default inflection for :#{unit}" if @unit_inflections["default"].nil?
11
- end
12
2
 
13
- # first non nil match wins, there is guaranteed to be at least one (default)
14
- def inflected
15
- trailing_digits.map { |key| @unit_inflections[key] }.compact[0]
16
- end
3
+ AMOUNT_CONFIG = {
4
+ :godina => { one:"godina", few:"godine", many:"godina" },
5
+ :mjesec => { one:"mjesec", few:"mjeseca", many:"mjeseci" },
6
+ :tjedan => { one:"tjedan", few:"tjedna", many:"tjedana" },
7
+ :dan => { one:"dan", few:"dana", many:"dana"}
8
+ }
17
9
 
18
- # for example 211 result is [211,11,1,"default"]
19
- def trailing_digits
20
- (amount_length.downto 1).map { |n| @amount % 10**n } << "default"
10
+ def self.inflect(amount, unit)
11
+ unit_inflections = AMOUNT_CONFIG[unit]
12
+ raise "Inflection :#{unit} is unsupported" if unit_inflections.nil?
13
+ "#{amount} #{unit_inflections[pluralize_rule(amount)]}"
21
14
  end
22
15
 
23
- def load_from_yaml_config
24
- YAML::load(File.open(File.expand_path(INFLECTIONS_CONFIG)))
16
+ def self.pluralize_rule(n)
17
+ return :many if (11..14).include?(n % 100)
18
+ return :few if (2..4).include?(n % 10)
19
+ (n % 10) == 1 ? :one : :many
25
20
  end
26
21
 
27
- def to_s() "#@amount #{inflected}" end
28
- def amount_length() @amount.to_s.length end
29
22
  end
@@ -3,117 +3,91 @@ require_relative '../lib/amount_inflector.rb'
3
3
  describe AmountInflector do
4
4
 
5
5
  it 'should say 1 godina' do
6
- trajanje = AmountInflector.new(1, :godina)
7
- trajanje.to_s.should == "1 godina"
6
+ AmountInflector.inflect(1, :godina).should == "1 godina"
8
7
  end
9
8
 
10
9
  it 'should say 2 godine' do
11
- trajanje = AmountInflector.new(2, :godina)
12
- trajanje.to_s.should == "2 godine"
10
+ AmountInflector.inflect(2, :godina).should == "2 godine"
13
11
  end
14
12
 
15
13
  it 'should say 92 godine' do
16
- trajanje = AmountInflector.new(92, :godina)
17
- trajanje.to_s.should == "92 godine"
14
+ AmountInflector.inflect(92, :godina).should == "92 godine"
18
15
  end
19
16
 
20
17
  it 'should say 12 godina' do
21
- trajanje = AmountInflector.new(12, :godina)
22
- trajanje.to_s.should == "12 godina"
18
+ AmountInflector.inflect(12, :godina).should == "12 godina"
23
19
  end
24
20
 
25
21
  it 'should say 2012 godina' do
26
- trajanje = AmountInflector.new(2012, :godina)
27
- trajanje.to_s.should == "2012 godina"
22
+ AmountInflector.inflect(2012, :godina).should == "2012 godina"
28
23
  end
29
24
 
30
25
  it 'should say 5 godina' do
31
- trajanje = AmountInflector.new(5, :godina)
32
- trajanje.to_s.should == "5 godina"
26
+ AmountInflector.inflect(5, :godina).should == "5 godina"
33
27
  end
34
28
 
35
29
  it 'should say 1 tjedan' do
36
- trajanje = AmountInflector.new(1, :tjedan)
37
- trajanje.to_s.should == "1 tjedan"
30
+ AmountInflector.inflect(1, :tjedan).should == "1 tjedan"
38
31
  end
39
32
 
40
33
  it 'should say 5 tjedana' do
41
- trajanje = AmountInflector.new(5, :tjedan)
42
- trajanje.to_s.should == "5 tjedana"
34
+ AmountInflector.inflect(5, :tjedan).should == "5 tjedana"
43
35
  end
44
36
 
45
37
  it 'should say 11 tjedana' do
46
- trajanje = AmountInflector.new(11, :tjedan)
47
- trajanje.to_s.should == "11 tjedana"
38
+ AmountInflector.inflect(11, :tjedan).should == "11 tjedana"
48
39
  end
49
40
 
50
41
  it 'should sayl 55 tjedana' do
51
- trajanje = AmountInflector.new(55, :tjedan)
52
- trajanje.to_s.should == "55 tjedana"
42
+ AmountInflector.inflect(55, :tjedan).should == "55 tjedana"
53
43
  end
54
44
 
55
45
  it 'should say 3 tjedna' do
56
- trajanje = AmountInflector.new(3, :tjedan)
57
- trajanje.to_s.should == "3 tjedna"
46
+ AmountInflector.inflect(3, :tjedan).should == "3 tjedna"
58
47
  end
59
48
 
60
49
  it 'should sayl 53 tjedana' do
61
- trajanje = AmountInflector.new(53, :tjedan)
62
- trajanje.to_s.should == "53 tjedna"
50
+ AmountInflector.inflect(53, :tjedan).should == "53 tjedna"
63
51
  end
64
52
 
65
53
  it 'should say 4 tjedna' do
66
- trajanje = AmountInflector.new(4, :tjedan)
67
- trajanje.to_s.should == "4 tjedna"
54
+ AmountInflector.inflect(4, :tjedan).should == "4 tjedna"
68
55
  end
69
56
 
70
57
  it 'should say 50 tjedana' do
71
- trajanje = AmountInflector.new(50, :tjedan)
72
- trajanje.to_s.should == "50 tjedana"
58
+ AmountInflector.inflect(50, :tjedan).should == "50 tjedana"
73
59
  end
74
60
 
75
61
  it 'should say 1 dan' do
76
- trajanje = AmountInflector.new(1, :dan)
77
- trajanje.to_s.should == "1 dan"
62
+ AmountInflector.inflect(1, :dan).should == "1 dan"
78
63
  end
79
64
 
80
65
  it 'should say 2 dana' do
81
- trajanje = AmountInflector.new(2, :dan)
82
- trajanje.to_s.should == "2 dana"
66
+ AmountInflector.inflect(2, :dan).should == "2 dana"
83
67
  end
84
68
 
85
69
  it 'should say 3 dana' do
86
- trajanje = AmountInflector.new(3, :dan)
87
- trajanje.to_s.should == "3 dana"
70
+ AmountInflector.inflect(3, :dan).should == "3 dana"
88
71
  end
89
72
 
90
73
  it 'should say 11 dana' do
91
- trajanje = AmountInflector.new(11, :dan)
92
- trajanje.to_s.should == "11 dana"
74
+ AmountInflector.inflect(11, :dan).should == "11 dana"
93
75
  end
94
76
 
95
77
  it 'should say 12 dana' do
96
- trajanje = AmountInflector.new(12, :dan)
97
- trajanje.to_s.should == "12 dana"
78
+ AmountInflector.inflect(12, :dan).should == "12 dana"
98
79
  end
99
80
 
100
81
  it 'should say 13 dana' do
101
- trajanje = AmountInflector.new(13, :dan)
102
- trajanje.to_s.should == "13 dana"
82
+ AmountInflector.inflect(13, :dan).should == "13 dana"
103
83
  end
104
84
 
105
85
  it 'should say 21 dan' do
106
- trajanje = AmountInflector.new(21, :dan)
107
- trajanje.to_s.should == "21 dan"
86
+ AmountInflector.inflect(21, :dan).should == "21 dan"
108
87
  end
109
88
 
110
89
  it 'should raise for unknows inflection 1 danas' do
111
- ->{ trajanje = AmountInflector.new(1, :danas) }.should raise_error 'Inflection :danas is unsupported'
112
- end
113
-
114
- it 'should raise for no default 2 erorr_no_default_dan' do
115
- errors_in_inflections = { "error_no_default_dan" => { 1 => :dan, 2 => :dana } }
116
- ->{ trajanje = AmountInflector.new(1, :error_no_default_dan, errors_in_inflections) }.should raise_error 'No default inflection for :error_no_default_dan'
90
+ ->{ trajanje = AmountInflector.inflect(1, :danas) }.should raise_error 'Inflection :danas is unsupported'
117
91
  end
118
92
 
119
93
  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: 1.0.0
4
+ version: 2.0.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-01-06 00:00:00.000000000Z
12
+ date: 2012-01-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
16
- requirement: &70187527724260 !ruby/object:Gem::Requirement
16
+ requirement: &70092400470100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70187527724260
24
+ version_requirements: *70092400470100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70187527723500 !ruby/object:Gem::Requirement
27
+ requirement: &70092400469140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70187527723500
35
+ version_requirements: *70092400469140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &70187527722720 !ruby/object:Gem::Requirement
38
+ requirement: &70092400468280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,18 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70187527722720
46
+ version_requirements: *70092400468280
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &70092400467300 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70092400467300
47
58
  description: Amount Inflector for year/month/week/day (Croatian)
48
59
  email: kresimir.bojcic@gmail.com
49
60
  executables: []
@@ -60,12 +71,8 @@ files:
60
71
  - Rakefile
61
72
  - VERSION
62
73
  - amount_inflector.gemspec
63
- - app/config/locales/amount_inflections.yml
64
74
  - lib/amount_inflector.rb
65
75
  - lib/amount_inflector/amount_inflector.rb
66
- - lib/generators/amount_inflector/USAGE
67
- - lib/generators/amount_inflector/install_generator.rb
68
- - lib/generators/amount_inflector/templates/amount_inflections.yml
69
76
  - spec/amount_inflector_spec.rb
70
77
  homepage: http://github.com/drKreso/amount_inflector
71
78
  licenses:
@@ -82,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
89
  version: '0'
83
90
  segments:
84
91
  - 0
85
- hash: -409522429409563961
92
+ hash: 2255724437978588266
86
93
  required_rubygems_version: !ruby/object:Gem::Requirement
87
94
  none: false
88
95
  requirements:
@@ -1,44 +0,0 @@
1
- #As for now only Croatian year/month/week/day is supported
2
- #If you have 2011 months for example (mjesec) the alhorithm is:
3
- #
4
- #* Check for 2011 in inflections
5
- #* Check for 011 in inflections
6
- #* Check for 11 in inflections => match it is "2011 mjeseci"
7
- #
8
- #If you have 77 years for example (godina) the alhorithm is:
9
- #
10
- #* Check for 77 in inflections
11
- #* Check for 7 in inflections
12
- #* Check for :default in inflections => match it is "7 godina"
13
- godina:
14
- default: godina
15
- 2: godine
16
- 3: godine
17
- 4: godine
18
- 12: godina
19
- 13: godina
20
- 14: godina
21
- mjesec:
22
- default: mjeseci
23
- 1: mjesec
24
- 2: mjeseca
25
- 3: mjeseca
26
- 4: mjeseca
27
- 11: mjeseci
28
- 12: mjeseci
29
- 13: mjeseci
30
- 14: mjeseci
31
- tjedan:
32
- default: tjedana
33
- 1: tjedan
34
- 2: tjedna
35
- 3: tjedna
36
- 4: tjedna
37
- 11: tjedana
38
- 12: tjedana
39
- 13: tjedana
40
- 14: tjedana
41
- dan:
42
- default: dana
43
- 1: dan
44
- 11: dana
@@ -1 +0,0 @@
1
- Copy inflections config to config/locales/amount_inflections.yml
@@ -1,12 +0,0 @@
1
- require 'rails/generators'
2
-
3
- class AmountInflector
4
- class InstallGenerator < Rails::Generators::Base
5
- source_root File.expand_path('../templates', __FILE__)
6
-
7
- def copy_default_inflections
8
- copy_file 'amount_inflections.yml', 'app/config/locales/amount_inflections.yml'
9
- end
10
-
11
- end
12
- end
@@ -1,44 +0,0 @@
1
- #As for now only Croatian year/month/week/day is supported
2
- #If you have 2011 months for example (mjesec) the alhorithm is:
3
- #
4
- #* Check for 2011 in inflections
5
- #* Check for 011 in inflections
6
- #* Check for 11 in inflections => match it is "2011 mjeseci"
7
- #
8
- #If you have 77 years for example (godina) the alhorithm is:
9
- #
10
- #* Check for 77 in inflections
11
- #* Check for 7 in inflections
12
- #* Check for :default in inflections => match it is "7 godina"
13
- godina:
14
- default: godina
15
- 2: godine
16
- 3: godine
17
- 4: godine
18
- 12: godina
19
- 13: godina
20
- 14: godina
21
- mjesec:
22
- default: mjeseci
23
- 1: mjesec
24
- 2: mjeseca
25
- 3: mjeseca
26
- 4: mjeseca
27
- 11: mjeseci
28
- 12: mjeseci
29
- 13: mjeseci
30
- 14: mjeseci
31
- tjedan:
32
- default: tjedana
33
- 1: tjedan
34
- 2: tjedna
35
- 3: tjedna
36
- 4: tjedna
37
- 11: tjedana
38
- 12: tjedana
39
- 13: tjedana
40
- 14: tjedana
41
- dan:
42
- default: dana
43
- 1: dan
44
- 11: dana