cldr-plurals-runtime-rb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ab0a42da25484b9efc070180d80a9258e7111be
4
+ data.tar.gz: 22d5e9ba4ba43a0e186749a1e0a200f206274953
5
+ SHA512:
6
+ metadata.gz: 243131d703e4e5624e20550806e33574de9ed5b9cc6d9a5911fed616bf2ce195e2bc2553acb375626e2c4335ed59d010bce0b63054cada1c13a3c8ebb33905e8
7
+ data.tar.gz: d8af452b349c416c37e7aafa27a3e451baec914ce9e5db03495330a0570d931a80f66a5b0ef313711e6f5d3276ea7ea23387227c097dc7f1d2d06d321ec4cecc
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'pry-nav'
7
+ gem 'rake'
8
+ end
9
+
10
+ group :test do
11
+ gem 'rspec'
12
+ gem 'rr'
13
+ end
@@ -0,0 +1,3 @@
1
+ == 1.0.0
2
+
3
+ * Birthday!
@@ -0,0 +1,67 @@
1
+ cldr-plurals-runtime-rb
2
+ =================
3
+
4
+ [![Build Status](https://travis-ci.org/camertron/cldr-plurals-runtime-rb.svg?branch=master)](http://travis-ci.org/camertron/cldr-plurals-runtime-rb)
5
+
6
+ Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
7
+
8
+ ## Installation
9
+
10
+ `gem install cldr-plurals-runtime-rb`
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+ require 'cldr-plurals/ruby_runtime'
16
+ ```
17
+
18
+ ## Functionality
19
+
20
+ The CLDR data set contains [plural information](http://unicode.org/cldr/trac/browser/tags/release-26-d04/common/supplemental/plurals.xml) for numerous languages in an expression-based [format](http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) defined by Unicode's TR35. The document describes how to determine the various parts of a number and how to use those parts to determine the plural rule. The parts as they appear in TR35 are:
21
+
22
+ | Symbol | Value |
23
+ |:-------|----------------------------------------------------------------|
24
+ | n | absolute value of the source number (integer and decimals). |
25
+ | i | integer digits of n. |
26
+ | v | number of visible fraction digits in n, with trailing zeros. |
27
+ | w | number of visible fraction digits in n, without trailing zeros.|
28
+ | f | visible fractional digits in n, with trailing zeros. |
29
+ | t | visible fractional digits in n, without trailing zeros. |
30
+
31
+ cldr-plurals-runtime-rb is an implementation of these calculations in Ruby. You can use them via the `CldrPlurals::RubyRuntime` module. Note that all methods take a stringified number as input:
32
+
33
+ ```ruby
34
+ num = '1.04'
35
+ CldrPlurals::RubyRuntime.n(num) # => 1.04
36
+ CldrPlurals::RubyRuntime.i(num) # => 1
37
+ ```
38
+
39
+ This runtime was created primarily for the [cldr-plurals](https://github.com/camertron/cldr-plurals) project, which can parse and emit Ruby code for a set of plural rules. Here's an example:
40
+
41
+ ```ruby
42
+ require 'cldr-plurals'
43
+ require 'cldr-plurals/ruby_runtime'
44
+
45
+ rules = CldrPlurals::Compiler::RuleList.new(:ru).tap do |rule_list|
46
+ rule_list.add_rule(:one, 'v = 0 and i % 10 = 1 and i % 100 != 11')
47
+ rule_list.add_rule(:few, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14')
48
+ rule_list.add_rule(:many, 'v = 0 and i % 10 = 0 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 11..14')
49
+ end
50
+
51
+ ruby_code = rules.to_code(:ruby)
52
+ rule_proc = eval(ruby_code)
53
+
54
+ rule_proc.call('3', CldrPlurals::RubyRuntime) # => :few
55
+ ```
56
+
57
+ ## Requirements
58
+
59
+ No external requirements.
60
+
61
+ ## Running Tests
62
+
63
+ `bundle exec rake` should do the trick. Alternatively you can run `bundle exec rspec`, which does the same thing.
64
+
65
+ ## Authors
66
+
67
+ * Cameron C. Dutro: http://github.com/camertron
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
4
+
5
+ require 'bundler'
6
+ require 'rspec/core/rake_task'
7
+ require 'rubygems/package_task'
8
+
9
+ require './lib/cldr-plurals/ruby_runtime'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ task :default => :spec
14
+
15
+ desc 'Run specs'
16
+ RSpec::Core::RakeTask.new do |t|
17
+ t.pattern = './spec/**/*_spec.rb'
18
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+ require 'cldr-plurals/ruby-runtime/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "cldr-plurals-runtime-rb"
6
+ s.version = ::CldrPlurals::RUBY_RUNTIME_VERSION
7
+ s.authors = ["Cameron Dutro"]
8
+ s.email = ["camertron@gmail.com"]
9
+ s.homepage = "http://github.com/camertron"
10
+
11
+ s.description = s.summary = 'Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).'
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+
16
+ s.require_path = 'lib'
17
+ s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "README.md", "Rakefile", "cldr-plurals-runtime-rb.gemspec"]
18
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module CldrPlurals
4
+ RUBY_RUNTIME_VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: UTF-8
2
+
3
+ module CldrPlurals
4
+ module RubyRuntime
5
+ class << self
6
+
7
+ def build_args_for(num_str)
8
+ [
9
+ n(num_str), i(num_str), f(num_str),
10
+ t(num_str), v(num_str), w(num_str)
11
+ ]
12
+ end
13
+
14
+ def n(str)
15
+ to_num(
16
+ if str.include?('.')
17
+ _n(str).gsub(/([0]+\z)/, '').chomp('.')
18
+ else
19
+ _n(str)
20
+ end
21
+ )
22
+ end
23
+
24
+ def i(str)
25
+ to_num(_i(str))
26
+ end
27
+
28
+ def f(str)
29
+ to_num(_f(str))
30
+ end
31
+
32
+ def t(str)
33
+ to_num(_t(str))
34
+ end
35
+
36
+ def v(str)
37
+ to_num(_v(str))
38
+ end
39
+
40
+ def w(str)
41
+ to_num(_w(str))
42
+ end
43
+
44
+ private
45
+
46
+ def to_num(str)
47
+ str.include?('.') ? str.to_f : str.to_i
48
+ end
49
+
50
+ # absolute value of the source number (integer and decimals).
51
+ def _n(str)
52
+ str.gsub(/(-)(.*)/, '\2')
53
+ end
54
+
55
+ # integer digits of n.
56
+ def _i(str)
57
+ _n(str).gsub(/([\d]+)(\..*)/, '\1')
58
+ end
59
+
60
+ # visible fractional digits in n, with trailing zeros.
61
+ def _f(str)
62
+ _n(str).gsub(/([\d]+\.?)(.*)/, '\2')
63
+ end
64
+
65
+ # visible fractional digits in n, without trailing zeros.
66
+ def _t(str)
67
+ _f(str).gsub(/([0]+\z)/, '')
68
+ end
69
+
70
+ # number of visible fraction digits in n, with trailing zeros.
71
+ def _v(str)
72
+ _f(str).length.to_s
73
+ end
74
+
75
+ # number of visible fraction digits in n, without trailing zeros.
76
+ def _w(str)
77
+ _t(str).length.to_s
78
+ end
79
+
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,204 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'yaml'
4
+ require 'spec_helper'
5
+
6
+ describe CldrPlurals::RubyRuntime do
7
+ let(:rt) { CldrPlurals::RubyRuntime }
8
+
9
+ context 'with an integer' do
10
+ let(:num) { '1' }
11
+
12
+ it '#n returns n without trailing zeroes' do
13
+ expect(rt.n(num)).to eq(1)
14
+ end
15
+
16
+ it '#i returns the int value' do
17
+ expect(rt.i(num)).to eq(1)
18
+ end
19
+
20
+ it '#v returns num of visible fraction digits (with zeroes)' do
21
+ expect(rt.v(num)).to eq(0)
22
+ end
23
+
24
+ it '#w returns num of visible fraction digits (without zeroes)' do
25
+ expect(rt.w(num)).to eq(0)
26
+ end
27
+
28
+ it '#f returns visible fractional digits (with zeroes)' do
29
+ expect(rt.f(num)).to eq(0)
30
+ end
31
+
32
+ it '#t returns visible fractional digits (without zeroes)' do
33
+ expect(rt.t(num)).to eq(0)
34
+ end
35
+ end
36
+
37
+ context 'with a zero decimal' do
38
+ let(:num) { '1.0' }
39
+
40
+ it '#n returns n without trailing zeroes' do
41
+ expect(rt.n(num)).to eq(1)
42
+ end
43
+
44
+ it '#i returns the int value' do
45
+ expect(rt.i(num)).to eq(1)
46
+ end
47
+
48
+ it '#v returns num of visible fraction digits (with zeroes)' do
49
+ expect(rt.v(num)).to eq(1)
50
+ end
51
+
52
+ it '#w returns num of visible fraction digits (without zeroes)' do
53
+ expect(rt.w(num)).to eq(0)
54
+ end
55
+
56
+ it '#f returns visible fractional digits (with zeroes)' do
57
+ expect(rt.f(num)).to eq(0)
58
+ end
59
+
60
+ it '#t returns visible fractional digits (without zeroes)' do
61
+ expect(rt.t(num)).to eq(0)
62
+ end
63
+ end
64
+
65
+ context 'with a double-precision zero decimal' do
66
+ let(:num) { '1.00' }
67
+
68
+ it '#n returns n without trailing zeroes' do
69
+ expect(rt.n(num)).to eq(1)
70
+ end
71
+
72
+ it '#i returns the int value' do
73
+ expect(rt.i(num)).to eq(1)
74
+ end
75
+
76
+ it '#v returns num of visible fraction digits (with zeroes)' do
77
+ expect(rt.v(num)).to eq(2)
78
+ end
79
+
80
+ it '#w returns num of visible fraction digits (without zeroes)' do
81
+ expect(rt.w(num)).to eq(0)
82
+ end
83
+
84
+ it '#f returns visible fractional digits (with zeroes)' do
85
+ expect(rt.f(num)).to eq(0)
86
+ end
87
+
88
+ it '#t returns visible fractional digits (without zeroes)' do
89
+ expect(rt.t(num)).to eq(0)
90
+ end
91
+ end
92
+
93
+ context 'with a non-zero decimal' do
94
+ let(:num) { '1.3' }
95
+
96
+ it '#n returns n without trailing zeroes' do
97
+ expect(rt.n(num)).to eq(1.3)
98
+ end
99
+
100
+ it '#i returns the int value' do
101
+ expect(rt.i(num)).to eq(1)
102
+ end
103
+
104
+ it '#v returns num of visible fraction digits (with zeroes)' do
105
+ expect(rt.v(num)).to eq(1)
106
+ end
107
+
108
+ it '#w returns num of visible fraction digits (without zeroes)' do
109
+ expect(rt.w(num)).to eq(1)
110
+ end
111
+
112
+ it '#f returns visible fractional digits (with zeroes)' do
113
+ expect(rt.f(num)).to eq(3)
114
+ end
115
+
116
+ it '#t returns visible fractional digits (without zeroes)' do
117
+ expect(rt.t(num)).to eq(3)
118
+ end
119
+ end
120
+
121
+ context 'with a double-precision trailing zero decimal' do
122
+ let(:num) { '1.30' }
123
+
124
+ it '#n returns n without trailing zeroes' do
125
+ expect(rt.n(num)).to eq(1.3)
126
+ end
127
+
128
+ it '#i returns the int value' do
129
+ expect(rt.i(num)).to eq(1)
130
+ end
131
+
132
+ it '#v returns num of visible fraction digits (with zeroes)' do
133
+ expect(rt.v(num)).to eq(2)
134
+ end
135
+
136
+ it '#w returns num of visible fraction digits (without zeroes)' do
137
+ expect(rt.w(num)).to eq(1)
138
+ end
139
+
140
+ it '#f returns visible fractional digits (with zeroes)' do
141
+ expect(rt.f(num)).to eq(30)
142
+ end
143
+
144
+ it '#t returns visible fractional digits (without zeroes)' do
145
+ expect(rt.t(num)).to eq(3)
146
+ end
147
+ end
148
+
149
+ context 'with a double-precision leading zero decimal' do
150
+ let(:num) { '1.03' }
151
+
152
+ it '#n returns n without trailing zeroes' do
153
+ expect(rt.n(num)).to eq(1.03)
154
+ end
155
+
156
+ it '#i returns the int value' do
157
+ expect(rt.i(num)).to eq(1)
158
+ end
159
+
160
+ it '#v returns num of visible fraction digits (with zeroes)' do
161
+ expect(rt.v(num)).to eq(2)
162
+ end
163
+
164
+ it '#w returns num of visible fraction digits (without zeroes)' do
165
+ expect(rt.w(num)).to eq(2)
166
+ end
167
+
168
+ it '#f returns visible fractional digits (with zeroes)' do
169
+ expect(rt.f(num)).to eq(3)
170
+ end
171
+
172
+ it '#t returns visible fractional digits (without zeroes)' do
173
+ expect(rt.t(num)).to eq(3)
174
+ end
175
+ end
176
+
177
+ context 'with a triple-precision decimal' do
178
+ let(:num) { '1.230' }
179
+
180
+ it '#n returns n without trailing zeroes' do
181
+ expect(rt.n(num)).to eq(1.23)
182
+ end
183
+
184
+ it '#i returns the int value' do
185
+ expect(rt.i(num)).to eq(1)
186
+ end
187
+
188
+ it '#v returns num of visible fraction digits (with zeroes)' do
189
+ expect(rt.v(num)).to eq(3)
190
+ end
191
+
192
+ it '#w returns num of visible fraction digits (without zeroes)' do
193
+ expect(rt.w(num)).to eq(2)
194
+ end
195
+
196
+ it '#f returns visible fractional digits (with zeroes)' do
197
+ expect(rt.f(num)).to eq(230)
198
+ end
199
+
200
+ it '#t returns visible fractional digits (without zeroes)' do
201
+ expect(rt.t(num)).to eq(23)
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ require 'pry-nav'
5
+ require 'cldr-plurals/ruby_runtime'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rr
9
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cldr-plurals-runtime-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
14
+ email:
15
+ - camertron@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/cldr-plurals/ruby-runtime/version.rb
21
+ - lib/cldr-plurals/ruby_runtime.rb
22
+ - spec/ruby_runtime_spec.rb
23
+ - spec/spec_helper.rb
24
+ - Gemfile
25
+ - History.txt
26
+ - README.md
27
+ - Rakefile
28
+ - cldr-plurals-runtime-rb.gemspec
29
+ homepage: http://github.com/camertron
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.14
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Ruby runtime methods for CLDR plural rules (see camertron/cldr-plurals).
52
+ test_files: []