pluralize 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +38 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/pluralize.rb +3 -0
- data/lib/pluralize/action_view/helpers/text_helper.rb +9 -0
- data/lib/pluralize/active_support/inflector.rb +49 -0
- data/lib/pluralize/config/inflections.rb +13 -0
- data/tasks/pluralize_tasks.rake +4 -0
- data/test/config/inflections.rb +35 -0
- data/test/pluralize_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +70 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
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.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
= Pluralize - Better pluralization
|
2
|
+
|
3
|
+
http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html
|
4
|
+
|
5
|
+
= Usage
|
6
|
+
|
7
|
+
=== Configuration
|
8
|
+
|
9
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
10
|
+
inflect.proc :pl, lambda{|count, singular, inflections|
|
11
|
+
if count == 1
|
12
|
+
singular
|
13
|
+
elsif (2..4).include?(count%10) && !((12..14).to_a + (22..24).to_a).include?(count%100)
|
14
|
+
inflections[:few]
|
15
|
+
else
|
16
|
+
inflections[:other]
|
17
|
+
end
|
18
|
+
}
|
19
|
+
|
20
|
+
inflect.plural "chleb", :few => "chleby", :other => "chlebów", :proc => :pl
|
21
|
+
inflect.plural "szklanka", :few => "szklanki", :other => "szklanek" # will user first defined proc
|
22
|
+
inflect.plural "one potatoe",
|
23
|
+
:two => "two potatoes",
|
24
|
+
:three => "three potatoes",
|
25
|
+
:four => "four",
|
26
|
+
:five => "five potatoes",
|
27
|
+
:six => "six potatoes",
|
28
|
+
:seven => "seven potatoes",
|
29
|
+
:more => "more!",
|
30
|
+
:proc => lambda{|count, singular, inflections|
|
31
|
+
case count%8
|
32
|
+
when 1 then singular
|
33
|
+
when 2 then inflections[:two]
|
34
|
+
when 3 then inflections[:three]
|
35
|
+
when 4 then inflections[:four]
|
36
|
+
when 5 then inflections[:five]
|
37
|
+
when 6 then inflections[:six]
|
38
|
+
when 7 then inflections[:seven]
|
39
|
+
else inflections[:more]
|
40
|
+
end
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
Copyright (c) 2009 G-Forces Polska, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the pluralize plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the pluralize plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Pluralize'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler::Tasks.new do |gemspec|
|
28
|
+
gemspec.name = "pluralize"
|
29
|
+
gemspec.summary = "Language Plural Rules implementation"
|
30
|
+
gemspec.email = "marcin.ciunelis@gmail.com"
|
31
|
+
gemspec.homepage = "http://github.com/gforces/pluralize"
|
32
|
+
gemspec.authors = ["Marcin Ciunelis"]
|
33
|
+
gemspec.version = "0.1"
|
34
|
+
end
|
35
|
+
Jeweler::GemcutterTasks.new
|
36
|
+
rescue LoadError
|
37
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
38
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pluralize'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/pluralize.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module Inflector
|
3
|
+
def pluralize(word, count = 2)
|
4
|
+
result = word.to_s.dup
|
5
|
+
|
6
|
+
if word.empty? || inflections.uncountables.include?(result.downcase)
|
7
|
+
result
|
8
|
+
else
|
9
|
+
inflections.plurals.each do |(rule, replacement)|
|
10
|
+
if replacement.is_a?(Hash)
|
11
|
+
if result == rule
|
12
|
+
if replacement.has_key?(:proc) && replacement[:proc].is_a?(Proc)
|
13
|
+
proc = replacement[:proc]
|
14
|
+
elsif replacement.has_key?(:proc) && inflections.procs[replacement.has_key?(:proc).to_sym].is_a?(Proc)
|
15
|
+
proc = inflections.procs[replacement.has_key?(:proc).to_sym]
|
16
|
+
elsif inflections.default_proc.is_a?(Proc)
|
17
|
+
proc = inflections.default_proc
|
18
|
+
end
|
19
|
+
if proc
|
20
|
+
result = proc.call(count, result, replacement)
|
21
|
+
break
|
22
|
+
end
|
23
|
+
end
|
24
|
+
else
|
25
|
+
break if result.gsub!(rule, replacement)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
result
|
29
|
+
end
|
30
|
+
result
|
31
|
+
end
|
32
|
+
class Inflections
|
33
|
+
attr_reader :procs
|
34
|
+
|
35
|
+
# def initialize
|
36
|
+
# @plurals, @singulars, @uncountables, @humans, @procs = [], [], [], [], {}
|
37
|
+
# end
|
38
|
+
|
39
|
+
def proc(locale, proc)
|
40
|
+
@procs = {} if @procs.nil?
|
41
|
+
@procs[locale.to_sym] = proc
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_proc
|
45
|
+
@procs[@procs.keys.first]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#polish plural rules
|
2
|
+
#more language plural rules here: http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html
|
3
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
4
|
+
inflect.proc :pl, lambda{|count, singular, inflections|
|
5
|
+
if count == 1
|
6
|
+
singular
|
7
|
+
elsif (2..4).include?(count%10) && !(12..14).include?(count%100)
|
8
|
+
inflections[:few]
|
9
|
+
else
|
10
|
+
inflections[:other]
|
11
|
+
end
|
12
|
+
}
|
13
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
2
|
+
inflect.proc :pl, lambda{|count, singular, inflections|
|
3
|
+
if count == 1
|
4
|
+
singular
|
5
|
+
elsif (2..4).include?(count%10) && !(12..14).include?(count%100)
|
6
|
+
inflections[:few]
|
7
|
+
else
|
8
|
+
inflections[:other]
|
9
|
+
end
|
10
|
+
}
|
11
|
+
|
12
|
+
inflect.plural "chleb", :few => "chleby", :other => "chlebów", :proc => :pl
|
13
|
+
inflect.plural "szklanka", :few => "szklanki", :other => "szklanek" # will user first defined proc
|
14
|
+
inflect.plural "one potatoe",
|
15
|
+
:two => "two potatoes",
|
16
|
+
:three => "three potatoes",
|
17
|
+
:four => "four",
|
18
|
+
:five => "five potatoes",
|
19
|
+
:six => "six potatoes",
|
20
|
+
:seven => "seven potatoes",
|
21
|
+
:more => "more!",
|
22
|
+
:proc => lambda{|count, singular, inflections|
|
23
|
+
case count%8
|
24
|
+
when 1 then singular
|
25
|
+
when 2 then inflections[:two]
|
26
|
+
when 3 then inflections[:three]
|
27
|
+
when 4 then inflections[:four]
|
28
|
+
when 5 then inflections[:five]
|
29
|
+
when 6 then inflections[:six]
|
30
|
+
when 7 then inflections[:seven]
|
31
|
+
else inflections[:more]
|
32
|
+
end
|
33
|
+
}
|
34
|
+
end
|
35
|
+
#(1..20).each {|n| p ActiveSupport::Inflector.pluralize("one potatoe", n)}
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pluralize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Ciunelis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-27 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: marcin.ciunelis@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README.rdoc
|
27
|
+
- Rakefile
|
28
|
+
- init.rb
|
29
|
+
- install.rb
|
30
|
+
- lib/pluralize.rb
|
31
|
+
- lib/pluralize/action_view/helpers/text_helper.rb
|
32
|
+
- lib/pluralize/active_support/inflector.rb
|
33
|
+
- lib/pluralize/config/inflections.rb
|
34
|
+
- tasks/pluralize_tasks.rake
|
35
|
+
- test/config/inflections.rb
|
36
|
+
- test/pluralize_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- uninstall.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/gforces/pluralize
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.5
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Language Plural Rules implementation
|
67
|
+
test_files:
|
68
|
+
- test/config/inflections.rb
|
69
|
+
- test/pluralize_test.rb
|
70
|
+
- test/test_helper.rb
|