amount_inflector 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ #
9
+
10
+ group :development do
11
+ gem "rspec-rails", "~> 2.3.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.6.4"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kresimir Bojcic
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ Amount Inflector
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.
4
+
5
+ In English it is quite simple:
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:
14
+
15
+ ```
16
+ 1 tjedan
17
+ 2 tjedna
18
+ 3 tjedna
19
+ 4 tjedna
20
+ 5 tjedana....
21
+ ```
22
+
23
+ Usage
24
+ ----------
25
+
26
+ Put in your GemFile:
27
+
28
+ ```
29
+ gem 'amount_inflector'
30
+ ```
31
+
32
+ And then:
33
+
34
+ ```
35
+ bundle
36
+
37
+ rails g amount_inflector:install
38
+ ```
39
+
40
+ In your view:
41
+ ```ruby
42
+ = AmountInflector.new(77, :godina) #=> 77 godina
43
+ ```
44
+
45
+ Tweaking
46
+ -----------
47
+ As for now only Croatian year/month/week/day is supported(config/locales/amount_inflections.yml)
48
+
49
+ ```
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
+
90
+
91
+ If you have 77 years for example (godina) the algorithm is:
92
+
93
+ * Check for 77 in inflections
94
+ * Check for 7 in inflections
95
+ * Check for :default in inflections => match it is "7 godina"
96
+
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.
100
+
101
+ Contributing to amount_inflector
102
+ ---------------------------------
103
+
104
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
105
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
106
+ * Fork the project
107
+ * Start a feature/bugfix branch
108
+ * Commit and push until you are happy with your contribution
109
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
110
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
111
+
112
+ Copyright
113
+ -----------
114
+
115
+ Copyright (c) 2012 Kresimir Bojcic. See LICENSE.txt for
116
+ further details.
117
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "amount_inflector"
18
+ gem.homepage = "http://github.com/drKreso/amount_inflector"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Amount Inflector}
21
+ gem.description = %Q{Amount Inflector for year/month/week/day (Croatian)}
22
+ gem.email = "kresimir.bojcic@gmail.com"
23
+ gem.authors = ["Kresimir Bojcic"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "emenu #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "amount_inflector"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kresimir Bojcic"]
12
+ s.date = "2012-01-06"
13
+ s.description = "Amount Inflector for year/month/week/day (Croatian)"
14
+ s.email = "kresimir.bojcic@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "amount_inflector.gemspec",
28
+ "app/config/locales/amount_inflections.yml",
29
+ "lib/amount_inflector.rb",
30
+ "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
+ "spec/amount_inflector_spec.rb"
35
+ ]
36
+ s.homepage = "http://github.com/drKreso/amount_inflector"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "1.8.10"
40
+ s.summary = "Amount Inflector"
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec-rails>, ["~> 2.3.0"])
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
49
+ else
50
+ s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec-rails>, ["~> 2.3.0"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,44 @@
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
@@ -0,0 +1,29 @@
1
+ require 'yaml'
2
+
3
+ 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
+
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
17
+
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"
21
+ end
22
+
23
+ def load_from_yaml_config
24
+ YAML::load(File.open(File.expand_path(INFLECTIONS_CONFIG)))
25
+ end
26
+
27
+ def to_s() "#@amount #{inflected}" end
28
+ def amount_length() @amount.to_s.length end
29
+ end
@@ -0,0 +1,2 @@
1
+ require 'amount_inflector/amount_inflector.rb'
2
+
@@ -0,0 +1 @@
1
+ Copy inflections config to config/locales/amount_inflections.yml
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,44 @@
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
@@ -0,0 +1,119 @@
1
+ require_relative '../lib/amount_inflector.rb'
2
+
3
+ describe AmountInflector do
4
+
5
+ it 'should say 1 godina' do
6
+ trajanje = AmountInflector.new(1, :godina)
7
+ trajanje.to_s.should == "1 godina"
8
+ end
9
+
10
+ it 'should say 2 godine' do
11
+ trajanje = AmountInflector.new(2, :godina)
12
+ trajanje.to_s.should == "2 godine"
13
+ end
14
+
15
+ it 'should say 92 godine' do
16
+ trajanje = AmountInflector.new(92, :godina)
17
+ trajanje.to_s.should == "92 godine"
18
+ end
19
+
20
+ it 'should say 12 godina' do
21
+ trajanje = AmountInflector.new(12, :godina)
22
+ trajanje.to_s.should == "12 godina"
23
+ end
24
+
25
+ it 'should say 2012 godina' do
26
+ trajanje = AmountInflector.new(2012, :godina)
27
+ trajanje.to_s.should == "2012 godina"
28
+ end
29
+
30
+ it 'should say 5 godina' do
31
+ trajanje = AmountInflector.new(5, :godina)
32
+ trajanje.to_s.should == "5 godina"
33
+ end
34
+
35
+ it 'should say 1 tjedan' do
36
+ trajanje = AmountInflector.new(1, :tjedan)
37
+ trajanje.to_s.should == "1 tjedan"
38
+ end
39
+
40
+ it 'should say 5 tjedana' do
41
+ trajanje = AmountInflector.new(5, :tjedan)
42
+ trajanje.to_s.should == "5 tjedana"
43
+ end
44
+
45
+ it 'should say 11 tjedana' do
46
+ trajanje = AmountInflector.new(11, :tjedan)
47
+ trajanje.to_s.should == "11 tjedana"
48
+ end
49
+
50
+ it 'should sayl 55 tjedana' do
51
+ trajanje = AmountInflector.new(55, :tjedan)
52
+ trajanje.to_s.should == "55 tjedana"
53
+ end
54
+
55
+ it 'should say 3 tjedna' do
56
+ trajanje = AmountInflector.new(3, :tjedan)
57
+ trajanje.to_s.should == "3 tjedna"
58
+ end
59
+
60
+ it 'should sayl 53 tjedana' do
61
+ trajanje = AmountInflector.new(53, :tjedan)
62
+ trajanje.to_s.should == "53 tjedna"
63
+ end
64
+
65
+ it 'should say 4 tjedna' do
66
+ trajanje = AmountInflector.new(4, :tjedan)
67
+ trajanje.to_s.should == "4 tjedna"
68
+ end
69
+
70
+ it 'should say 50 tjedana' do
71
+ trajanje = AmountInflector.new(50, :tjedan)
72
+ trajanje.to_s.should == "50 tjedana"
73
+ end
74
+
75
+ it 'should say 1 dan' do
76
+ trajanje = AmountInflector.new(1, :dan)
77
+ trajanje.to_s.should == "1 dan"
78
+ end
79
+
80
+ it 'should say 2 dana' do
81
+ trajanje = AmountInflector.new(2, :dan)
82
+ trajanje.to_s.should == "2 dana"
83
+ end
84
+
85
+ it 'should say 3 dana' do
86
+ trajanje = AmountInflector.new(3, :dan)
87
+ trajanje.to_s.should == "3 dana"
88
+ end
89
+
90
+ it 'should say 11 dana' do
91
+ trajanje = AmountInflector.new(11, :dan)
92
+ trajanje.to_s.should == "11 dana"
93
+ end
94
+
95
+ it 'should say 12 dana' do
96
+ trajanje = AmountInflector.new(12, :dan)
97
+ trajanje.to_s.should == "12 dana"
98
+ end
99
+
100
+ it 'should say 13 dana' do
101
+ trajanje = AmountInflector.new(13, :dan)
102
+ trajanje.to_s.should == "13 dana"
103
+ end
104
+
105
+ it 'should say 21 dan' do
106
+ trajanje = AmountInflector.new(21, :dan)
107
+ trajanje.to_s.should == "21 dan"
108
+ end
109
+
110
+ 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'
117
+ end
118
+
119
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amount_inflector
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kresimir Bojcic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: &70187527724260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70187527724260
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70187527723500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70187527723500
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70187527722720 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70187527722720
47
+ description: Amount Inflector for year/month/week/day (Croatian)
48
+ email: kresimir.bojcic@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.md
54
+ files:
55
+ - .document
56
+ - .rspec
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - VERSION
62
+ - amount_inflector.gemspec
63
+ - app/config/locales/amount_inflections.yml
64
+ - lib/amount_inflector.rb
65
+ - 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
+ - spec/amount_inflector_spec.rb
70
+ homepage: http://github.com/drKreso/amount_inflector
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -409522429409563961
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.10
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Amount Inflector
98
+ test_files: []