Class: LtdTemplate::Proxy::Number

Inherits:
LtdTemplate::Proxy show all
Defined in:
lib/ltdtemplate/proxy/number.rb

Instance Attribute Summary

Attributes included from Value

#runtime_methods

Instance Method Summary collapse

Methods inherited from LtdTemplate::Proxy

#initialize, #rubyverse_original, #rubyversed

Methods included from Value

#do_methods, #do_run_method, included, #initialize, #inspect, #rubyversed, #tpl_boolean

Constructor Details

This class inherits a constructor from LtdTemplate::Proxy

Instance Method Details

#do_compare(opts) ⇒ Object

Implement numeric comparison operators



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

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



58
59
60
61
62
63
64
# 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) ⇒ Object

Implement “-” method (subtraction/negation)



67
68
69
70
71
72
73
# 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 = {}) ⇒ Object

Evaluate supported methods for numeric objects.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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_textObject



35
# File 'lib/ltdtemplate/proxy/number.rb', line 35

def tpl_text; @original.to_s; end