Class: Cldr::Export::Data::Plurals::Expression
- Inherits:
-
Object
- Object
- Cldr::Export::Data::Plurals::Expression
- Defined in:
- lib/cldr/export/data/plurals/rules.rb
Instance Attribute Summary collapse
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
-
#negate ⇒ Object
readonly
Returns the value of attribute negate.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(operator = nil, mod = nil, negate = nil, operand = nil, type = nil) ⇒ Expression
constructor
A new instance of Expression.
-
#to_ruby ⇒ Object
Symbol Value n absolute value of the source number (integer and decimals).
Constructor Details
#initialize(operator = nil, mod = nil, negate = nil, operand = nil, type = nil) ⇒ Expression
Returns a new instance of Expression.
118 119 120 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 118 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
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
116 117 118 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 116 def mod @mod end |
#negate ⇒ Object (readonly)
Returns the value of attribute negate.
116 117 118 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 116 def negate @negate end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
116 117 118 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 116 def operand @operand end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
116 117 118 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 116 def operator @operator end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
116 117 118 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 116 def type @type end |
Instance Method Details
#to_ruby ⇒ Object
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
131 132 133 134 135 136 137 138 139 140 141 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 |
# File 'lib/cldr/export/data/plurals/rules.rb', line 131 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 |