class LtdTemplate::Proxy::Number

Public Instance Methods

do_compare(opts) click to toggle source

Implement numeric comparison operators

# File lib/ltdtemplate/proxy/number.rb, line 40
def do_compare (opts)
    if (params = opts[:parameters]) && params.size(:seq) > 0 &&
      params[0].is_a?(Numeric)
        diff = params[0]
    else diff = 0
    end
    diff = @original - diff
    case opts[:method]
    when '<' then diff < 0
    when '<=' then diff <= 0
    when '==' then diff == 0
    when '!=' then diff != 0
    when '>=' then diff >= 0
    when '>' then diff > 0
    end
end
do_sequential(opts = {}, &block) click to toggle source

Implement sequential operations (+, *, /, %, &, |, ^)

# File lib/ltdtemplate/proxy/number.rb, line 58
def do_sequential (opts = {}, &block)
    if params = opts[:parameters]
        params.values(:seq).select { |val| val.is_a? Numeric }.
          inject(@original, &block)
    else @original
    end
end
do_subtract(opts) click to toggle source

Implement “-” method (subtraction/negation)

# File lib/ltdtemplate/proxy/number.rb, line 67
def do_subtract (opts)
    sum = @original
    params = opts[:parameters]
    if !params || params.size(:seq) == 0 then -@original
    else do_sequential(opts) { |a, b| a - b }
    end
end
evaluate(opts = {}) click to toggle source

Evaluate supported methods for numeric objects.

Calls superclass method LtdTemplate::Value#evaluate
# File lib/ltdtemplate/proxy/number.rb, line 12
def evaluate (opts = {})
    case opts[:method]
    when nil, 'call' then @original
    when 'abs', 'ceil', 'floor'
        @original.send opts[:method].to_sym
    when 'class' then 'Number'
    when 'flt', 'float' then @original.to_f
    when 'int' then @original.to_i
    when 'str', 'string' then @original.to_s
    when 'type' then 'number'
    when '+' then do_sequential(opts) { |a, b| a + b }
    when '-' then do_subtract opts
    when '*' then do_sequential(opts) { |a, b| a * b }
    when '/' then do_sequential(opts) { |a, b| a / b }
    when '%' then do_sequential(opts) { |a, b| a % b }
    when '&' then do_sequential(opts) { |a, b| a & b }
    when '|' then do_sequential(opts) { |a, b| a | b }
    when '^' then do_sequential(opts) { |a, b| a ^ b }
    when '<', '<=', '==', '!=', '>=', '>' then do_compare opts
    else super opts
    end
end
tpl_text() click to toggle source
# File lib/ltdtemplate/proxy/number.rb, line 35
def tpl_text; @original.to_s; end