|
1 | | -# frozen_string_literal: false |
| 1 | +# frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require "rubygems" |
4 | 4 | require "nokogiri" |
@@ -105,21 +105,21 @@ def to_ruby |
105 | 105 | if code |
106 | 106 | "#{code} ? :#{key} : #{result}" |
107 | 107 | else |
108 | | - ":" << key.to_s |
| 108 | + ":#{key}" |
109 | 109 | end |
110 | 110 | end + " }" |
111 | 111 | end |
112 | 112 | end |
113 | 113 |
|
114 | 114 | class Proposition < Array |
115 | | - def initialize(type = nil) |
| 115 | + def initialize(type) |
116 | 116 | super() |
117 | 117 |
|
118 | 118 | @type = type |
119 | 119 | end |
120 | 120 |
|
121 | 121 | def to_ruby |
122 | | - @ruby ||= "(" << map(&:to_ruby).join(" #{@type} ") << ")" |
| 122 | + @ruby ||= "(#{map(&:to_ruby).join(" #{@type} ")})" |
123 | 123 | end |
124 | 124 | end |
125 | 125 |
|
@@ -151,76 +151,80 @@ def initialize(operator = nil, mod = nil, negate = nil, operand = nil, type = ni |
151 | 151 | # |
152 | 152 | # http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules |
153 | 153 | def to_ruby |
154 | | - @ruby ||= begin |
155 | | - return nil unless @operator |
| 154 | + return unless @operator |
156 | 155 |
|
| 156 | + @ruby ||= begin |
157 | 157 | enclose = false |
158 | 158 | fraction = false |
159 | | - case @type |
| 159 | + |
| 160 | + op = case @type |
160 | 161 | when "i" |
161 | | - op = "n.to_i" |
| 162 | + "n.to_i" |
162 | 163 | when "f" |
163 | | - op = '(f = n.to_s.split(".")[1]) ? f.to_i : 0' |
164 | 164 | enclose = true |
| 165 | + '(f = n.to_s.split(".")[1]) ? f.to_i : 0' |
165 | 166 | when "t" |
166 | | - op = '(t = n.to_s.split(".")[1]) ? t.gsub(/0+$/, "").to_i : 0' |
167 | 167 | enclose = true |
| 168 | + '(t = n.to_s.split(".")[1]) ? t.gsub(/0+$/, "").to_i : 0' |
168 | 169 | when "v" |
169 | | - op = '(v = n.to_s.split(".")[1]) ? v.length : 0' |
170 | 170 | enclose = true |
| 171 | + '(v = n.to_s.split(".")[1]) ? v.length : 0' |
171 | 172 | when "w" |
172 | | - op = '(w = n.to_s.split(".")[1]) ? w.gsub(/0+$/, "").length : 0' |
173 | 173 | enclose = true |
| 174 | + '(w = n.to_s.split(".")[1]) ? w.gsub(/0+$/, "").length : 0' |
174 | 175 | when "c", "e" |
175 | 176 | # We don't support numbers in the "compact decimal" format. |
176 | 177 | # Since `c`/`e` are always 0 for non-"compact decimal" format |
177 | 178 | # numbers, we just hardcode it to 0 for now. |
178 | 179 | # TODO: https://github.com/ruby-i18n/ruby-cldr/issues/131 |
179 | | - op = "#{@type} = 0" |
180 | 180 | enclose = true |
| 181 | + "#{@type} = 0" |
181 | 182 | when "n" |
182 | 183 | fraction = true |
183 | | - op = "n.to_f" |
| 184 | + "n.to_f" |
184 | 185 | else |
185 | 186 | raise StandardError, "Unknown plural operand `#{@type}`" |
186 | 187 | end |
| 188 | + |
187 | 189 | if @mod |
188 | | - op = "(" << op << ")" if enclose |
189 | | - op << " % " << @mod.to_s |
| 190 | + op = "(#{op})" if enclose |
| 191 | + op = "#{op} % #{@mod}" |
190 | 192 | enclose = false |
191 | 193 | end |
| 194 | + |
192 | 195 | case @operator |
193 | 196 | when :is |
194 | | - op = "(" << op << ")" if enclose |
195 | | - op << (@negate ? " != " : " == ") << @operand.to_s |
| 197 | + op = "(#{op})" if enclose |
| 198 | + op = "#{op} #{@negate ? "!=" : "=="} #{@operand}" |
196 | 199 | when :in |
197 | 200 | values = @operand.first |
198 | 201 | ranges = @operand.last |
199 | 202 | prepend = (@negate ? "!" : "") |
200 | 203 | str = "" |
201 | 204 | bop = op |
202 | | - bop = "(" << bop << ")" if enclose || @mod |
| 205 | + bop = "(#{bop})" if enclose || @mod |
203 | 206 | if values.count == 1 |
204 | | - str = bop + (@negate ? " != " : " == ") << values.first.to_s |
| 207 | + str = "#{bop} #{@negate ? "!=" : "=="} #{values.first}" |
205 | 208 | elsif values.count > 1 |
206 | 209 | str = prepend + "#{values.inspect}.include?(#{op})" |
207 | 210 | end |
208 | 211 | enclose = ranges.count > 1 || (values.count > 0 && ranges.count > 0) |
209 | 212 | if ranges.count > 0 |
210 | | - str << " || " if values.count > 0 |
211 | | - str << "((#{bop} % 1).zero? && " if fraction |
212 | | - str << "(" if ranges.count > 1 |
213 | | - str << prepend + "(#{ranges.shift.inspect}).include?(#{op})" |
214 | | - ranges.each do |range| |
215 | | - str << " || " << prepend + "(#{range.inspect}).include?(#{op})" |
216 | | - end |
217 | | - str << ")" if ranges.count > 0 |
218 | | - str << ")" if fraction |
| 213 | + str += " || " if values.count > 0 |
| 214 | + str += "((#{bop} % 1).zero? && " if fraction |
| 215 | + |
| 216 | + ranges_formatted = ranges.map do |range| |
| 217 | + "#{prepend}(#{range.inspect}).include?(#{op})" |
| 218 | + end.join(" || ") |
| 219 | + ranges_formatted = "(#{ranges_formatted})" if ranges.count > 1 |
| 220 | + str += ranges_formatted |
| 221 | + |
| 222 | + str += ")" if fraction |
219 | 223 | end |
220 | | - str = "(" << str << ")" if enclose |
| 224 | + str = "(#{str})" if enclose |
221 | 225 | str |
222 | 226 | when :within |
223 | | - op = "(" << op << ")" if enclose || @mod |
| 227 | + op = "(#{op})" if enclose || @mod |
224 | 228 | (@negate ? "!" : "") + "#{op}.between?(#{@operand.first}, #{@operand.last})" |
225 | 229 | else |
226 | 230 | raise "unknown operator '#{@operator}'" |
|
0 commit comments