Class: Cldr::Export::Data::Plurals::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/cldr/export/data/plurals/rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator = nil, mod = nil, negate = nil, operand = nil, type = nil) ⇒ Expression

Returns a new instance of Expression.



129
130
131
# File 'lib/cldr/export/data/plurals/rules.rb', line 129

def initialize(operator = nil, mod = nil, negate = nil, operand = nil, type = nil)
  @operator, @mod, @negate, @operand, @type = operator, mod, negate, operand, type
end

Instance Attribute Details

#modObject (readonly)

Returns the value of attribute mod.



127
128
129
# File 'lib/cldr/export/data/plurals/rules.rb', line 127

def mod
  @mod
end

#negateObject (readonly)

Returns the value of attribute negate.



127
128
129
# File 'lib/cldr/export/data/plurals/rules.rb', line 127

def negate
  @negate
end

#operandObject (readonly)

Returns the value of attribute operand.



127
128
129
# File 'lib/cldr/export/data/plurals/rules.rb', line 127

def operand
  @operand
end

#operatorObject (readonly)

Returns the value of attribute operator.



127
128
129
# File 'lib/cldr/export/data/plurals/rules.rb', line 127

def operator
  @operator
end

#typeObject (readonly)

Returns the value of attribute type.



127
128
129
# File 'lib/cldr/export/data/plurals/rules.rb', line 127

def type
  @type
end

Instance Method Details

#to_rubyObject

Symbol Value n absolute value of the source number (integer and decimals). i integer digits of n. v number of visible fraction digits in n, with trailing zeros. w number of visible fraction digits in n, without trailing zeros. f visible fractional digits in n, with trailing zeros. t visible fractional digits in n, without trailing zeros.

www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cldr/export/data/plurals/rules.rb', line 142

def to_ruby
  @ruby ||= begin
    return nil unless @operator
    enclose = false
    fraction = false
    case @type
    when 'i'
      op = 'n.to_i'
    when 'f'
      op = '(f = n.to_s.split(".")[1]) ? f.to_i : 0'
      enclose = true
    when 't'
      op = '(t = n.to_s.split(".")[1]) ? t.gsub(/0+$/, "").to_i : 0'
      enclose = true
    when 'v'
      op = '(v = n.to_s.split(".")[1]) ? v.length : 0'
      enclose = true
    when 'w'
      op = '(w = n.to_s.split(".")[1]) ? w.gsub(/0+$/, "").length : 0'
      enclose = true
    else
      fraction = true
      op = 'n.to_f'
    end
    if @mod
      op = '(' << op << ')' if enclose
      op << ' % ' << @mod.to_s
      enclose = false
    end
    case @operator
    when :is
      op = '(' << op << ')' if enclose
      op << (@negate ? ' != ' : ' == ') << @operand.to_s
    when :in
      values = @operand.first
      ranges = @operand.last
      prepend = (@negate ? '!' : '')
      str = ''
      bop = op
      bop = '(' << bop << ')' if enclose || @mod
      if values.count == 1
        str = bop + (@negate ? ' != ' : ' == ') << values.first.to_s
      elsif values.count > 1
        str = prepend + "#{values.inspect}.include?(#{op})"
      end
      enclose = ranges.count > 1 || (values.count > 0 && ranges.count > 0)
      if ranges.count > 0
        str << ' || ' if values.count > 0
        str << "((#{bop} % 1).zero? && " if fraction
        str << '(' if ranges.count > 1
        str << prepend + "(#{ranges.shift.inspect}).include?(#{op})"
        ranges.each do |range|
          str << ' || ' << prepend + "(#{range.inspect}).include?(#{op})"
        end
        str << ')' if ranges.count > 0
        str << ')' if fraction
      end
      str = '(' << str << ')' if enclose
      str
    when :within
      op = '(' << op << ')' if enclose || @mod
      (@negate ? '!' : '') + "#{op}.between?(#{@operand.first}, #{@operand.last})"
    else
      raise "unknown operator '#{@operator}'"
    end
  end
end