plurality 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f96196fcf6917c3aa0ac866c3977e7b1a639bea8
4
+ data.tar.gz: ff30f4edc72d331c2132d4bd67da392bc4be13aa
5
+ SHA512:
6
+ metadata.gz: baafef611b2bae560a2d8f2dd87e6f45c62e85f349bfa57b969aeb26a605c2ba6c25b422f3475d786965bf778b9cbf652299b9615de33a4e8ad34dcf9b259728
7
+ data.tar.gz: e176d8575525f9d2020e9f88065401a8f2690975df72e6059a0a8477b301e29c10c7ab2ae138adcf546c866acecbeae567e6578b69339c82a800dd130600e029
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in plurality.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Evan Alter
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Plurality
2
+
3
+ Plurality lets you define different sentence pluralizations based on the number of nouns passed to it.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'plurality'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install plurality
18
+
19
+ ## Usage
20
+
21
+ Plurality relies on on I18n's backend for storing the plural forms. It also follows I18n's own conventions for storing plurals with the added exception of being able to pluralize up to 1000 sentences instead of the the limitation of the current language.
22
+
23
+ If you are using Simple backend then below is what your `en.yml` would look like. The tokens are the ordinals representation of where the noun falls in the array. They are calculated by the gem [`numbers_and_words`](https://github.com/kslazarev/numbers_and_words). The `additonal` token is a special one that is calculated based on the number of nouns minus the ordinal tokens used.
24
+ ``` yaml
25
+ en:
26
+ email:
27
+ subject:
28
+ one: "%{first} was added"
29
+ two: "%{first} and %{second} were added"
30
+ other: "%{first}, %{second} and %{additional} others were added"
31
+ ```
32
+
33
+ Then you simply call `Plurality.t` or `Plurality.translate` with the array of nouns and it'll spit out the rest for you.
34
+
35
+ ``` ruby
36
+ require 'plurality'
37
+
38
+ users = %w(Evan Rob Bill Josh)
39
+
40
+ Plurality.t 'email.subject', nouns: users #=> "Evan, Rob and 3 others were added"
41
+
42
+ ```
43
+
44
+ You may also pass additional options that'll be passed through to `I18n.translate` assuming they don't conflict with the reserved tokens.
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
data/lib/plurality.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "plurality/version"
2
+ require "numbers_and_words"
3
+
4
+ module Plurality
5
+ extend self
6
+
7
+ class MissingPluralData < ArgumentError
8
+ end
9
+
10
+ ORDINALS = (1..1000).each_with_object({}) { |i, h| h[i] = i.to_words(ordinal: true) }
11
+ WORDS = (1..1000).each_with_object({}) { |i, h| h[i] = i.to_words.to_sym }
12
+
13
+ TOKENS = /%?%\{([^\}]+)\}/
14
+
15
+ def translate(*args)
16
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
17
+ key = args.shift
18
+ nouns = options.delete(:nouns).to_a
19
+ translations = I18n.t!(key, scope: options[:scope])
20
+ numbers = translations.keys
21
+ other = numbers.delete(:other)
22
+ count = nouns.count
23
+ threshold = WORDS.key(numbers.last)
24
+
25
+ if other.nil? || count <= threshold
26
+ number = WORDS[count]
27
+ string = translations[number]
28
+ else
29
+ number = :other
30
+ string = translations[:other]
31
+ end
32
+
33
+ raise MissingPluralData, "Missing for #{WORDS[count]} nouns" if string.nil?
34
+
35
+ options[:scope] = generate_scope key, options[:scope]
36
+ options[:additional] = calculate_additonal(count, string)
37
+ options.merge! ordinalized_nouns(nouns, threshold)
38
+
39
+ I18n.t number, options
40
+ end
41
+ alias :t :translate
42
+
43
+ private
44
+
45
+ def calculate_additonal(count, string)
46
+ tokens = string.scan(TOKENS).flatten.count { |t| ORDINALS.has_value? t }
47
+ count - tokens
48
+ end
49
+
50
+ def generate_scope(key, scope)
51
+ I18n.normalize_keys(nil, key, scope)
52
+ end
53
+
54
+ def ordinalized_nouns(nouns, number)
55
+ Hash[ORDINALS.values.take(number).map(&:to_sym).zip(nouns.take(number))].delete_if { |k, v| v.nil? }
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ module Plurality
2
+ VERSION = "0.0.1"
3
+ end
data/plurality.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'plurality/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "plurality"
8
+ spec.version = Plurality::VERSION
9
+ spec.authors = ["Evan Alter"]
10
+ spec.email = ["evan.alter@gmail.com"]
11
+ spec.description = %q{Pluralize sentences based on the number of objects}
12
+ spec.summary = %q{Sentence pluralization}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "i18n", "~> 0.6.8"
22
+ spec.add_dependency "numbers_and_words", "~> 0.10.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.14"
27
+ end
@@ -0,0 +1,15 @@
1
+ en:
2
+ correct:
3
+ email:
4
+ subject:
5
+ one: "%{first} was added"
6
+ two: "%{first} and %{second} were added"
7
+ three: "%{first}, %{second} and %{third} were added"
8
+ four: "%{first} and %{additional} others were added"
9
+ other: "%{first}, %{second} and %{additional} others were added"
10
+ missing:
11
+ email:
12
+ subject:
13
+ one: "%{first} was added"
14
+ three: "%{first}, %{second} and %{third} were added"
15
+ other: "%{first}, %{second} and %{additional} others were added"
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe Plurality do
4
+ let(:users) { %w(Evan Rob Bill Josh Noah) }
5
+
6
+ describe ".translate" do
7
+ context "with correct data" do
8
+ let(:key) { 'correct.email.subject' }
9
+
10
+ context "one noun" do
11
+ let(:nouns) { users.take(1) }
12
+
13
+ subject { Plurality.translate key, nouns: nouns }
14
+
15
+ it { should eq("Evan was added")}
16
+ end
17
+
18
+ context "two nouns" do
19
+ let(:nouns) { users.take(2) }
20
+
21
+ subject { Plurality.translate key, nouns: nouns }
22
+
23
+ it { should eq("Evan and Rob were added")}
24
+ end
25
+
26
+ context "three nouns" do
27
+ let(:nouns) { users.take(3) }
28
+
29
+ subject { Plurality.translate key, nouns: nouns }
30
+
31
+ it { should eq("Evan, Rob and Bill were added")}
32
+ end
33
+
34
+ context "four nouns but only using the first user's name" do
35
+ let(:nouns) { users.take(4) }
36
+
37
+ subject { Plurality.translate key, nouns: nouns }
38
+
39
+ it { should eq("Evan and 3 others were added")}
40
+ end
41
+
42
+ context "other using the first two users but not the rest" do
43
+ let(:nouns) { users.take(5) }
44
+
45
+ subject { Plurality.translate key, nouns: nouns }
46
+
47
+ it { should eq("Evan, Rob and 3 others were added")}
48
+ end
49
+ end
50
+
51
+ context "with some missing data" do
52
+ let(:key) { 'missing.email.subject' }
53
+
54
+ context "one noun" do
55
+ let(:nouns) { users.take(1) }
56
+
57
+ subject { Plurality.translate key, nouns: nouns }
58
+
59
+ it { should eq("Evan was added")}
60
+ end
61
+
62
+ context "missing data for two nouns" do
63
+ let(:nouns) { users.take(2) }
64
+
65
+ subject { Plurality.translate key, nouns: nouns }
66
+
67
+ it { raise_error(Plurality::MissingPluralData, "Missing for two nouns") }
68
+ end
69
+
70
+ context "three nouns" do
71
+ let(:nouns) { users.take(3) }
72
+
73
+ subject { Plurality.translate key, nouns: nouns }
74
+
75
+ it { should eq("Evan, Rob and Bill were added")}
76
+ end
77
+
78
+ context "other using the first two users but not the third" do
79
+ let(:nouns) { users.take(5) }
80
+
81
+ subject { Plurality.translate key, nouns: nouns }
82
+
83
+ it { should eq("Evan, Rob and 3 others were added")}
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,6 @@
1
+ require 'plurality'
2
+
3
+ I18n.config.enforce_available_locales = true
4
+ I18n.load_path = Dir[File.dirname(__FILE__) + '/fixtures/*.yml']
5
+ I18n.backend.load_translations
6
+ I18n.locale = :en
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plurality
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Alter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: numbers_and_words
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.14'
83
+ description: Pluralize sentences based on the number of objects
84
+ email:
85
+ - evan.alter@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/plurality.rb
96
+ - lib/plurality/version.rb
97
+ - plurality.gemspec
98
+ - spec/fixtures/en.yml
99
+ - spec/lib/plurality_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.0.3
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Sentence pluralization
125
+ test_files:
126
+ - spec/fixtures/en.yml
127
+ - spec/lib/plurality_spec.rb
128
+ - spec/spec_helper.rb
129
+ has_rdoc: