Class: LtdTemplate::Value::Number
- Defined in:
- lib/ltdtemplate/value/number.rb
Instance Attribute Summary
Attributes inherited from Code
Instance Method Summary collapse
-
#do_compare(opts) ⇒ Object
Implement numeric comparison operators.
-
#do_sequential(opts = {}, &block) ⇒ Object
Implement sequential operations (+, *, /, %, &, |, ^).
-
#do_subtract(opts) ⇒ Object
Implement “-” method (subtraction/negation).
- #get_value(opts = {}) ⇒ Object
-
#initialize(template, value = 0) ⇒ Number
constructor
A new instance of Number.
- #to_boolean ⇒ Object
- #to_native ⇒ Object
- #to_text ⇒ Object
Methods inherited from Code
#do_method, #do_set, #get_item, #has_item?, instance, #is_set?, #set_item, #set_value
Constructor Details
#initialize(template, value = 0) ⇒ Number
Returns a new instance of Number.
11 12 13 14 15 16 17 |
# File 'lib/ltdtemplate/value/number.rb', line 11 def initialize (template, value = 0) super template case value when Numeric then @value = value when String then @value = (value =~ /\./) ? value.to_f : value.to_i end end |
Instance Method Details
#do_compare(opts) ⇒ Object
Implement numeric comparison operators
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ltdtemplate/value/number.rb', line 76 def do_compare (opts) diff = 0 if params = opts[:parameters] and params.positional.size > 0 diff = params.positional[0].to_native diff = 0 unless diff.is_a? Numeric end diff = @value - diff @template.factory :boolean, 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 (+, *, /, %, &, |, ^)
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ltdtemplate/value/number.rb', line 49 def do_sequential (opts = {}, &block) if params = opts[:parameters] @template.factory(:number, params.positional.map { |tval| tval.to_native }. select { |nval| nval.is_a? Numeric }. inject(@value, &block)) else @value end end |
#do_subtract(opts) ⇒ Object
Implement “-” method (subtraction/negation)
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ltdtemplate/value/number.rb', line 61 def do_subtract (opts) sum = @value params = params.positional if params = opts[:parameters] if !params or params.size == 0 sum = -sum else params.each do |tval| nval = tval.to_native sum -= nval if nval.is_a? Numeric end end @template.factory :number, sum end |
#get_value(opts = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ltdtemplate/value/number.rb', line 23 def get_value (opts = {}) case opts[:method] when nil, 'call' then self when 'abs' then (@value >= 0) ? self : @template.factory(:number, -@value) when 'ceil' then @template.factory :number, @value.ceil when 'class' then @template.factory :string, 'Number' when 'floor' then @template.factory :number, @value.floor when 'flt', 'float' then @template.factory :number, @value.to_f when 'int' then @template.factory :number, @value.to_i when 'str', 'string' then @template.factory :number, @value.to_s when 'type' then @template.factory :string, '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 do_method opts, 'Number' end end |
#to_boolean ⇒ Object
19 |
# File 'lib/ltdtemplate/value/number.rb', line 19 def to_boolean; true; end |
#to_native ⇒ Object
20 |
# File 'lib/ltdtemplate/value/number.rb', line 20 def to_native; @value; end |
#to_text ⇒ Object
21 |
# File 'lib/ltdtemplate/value/number.rb', line 21 def to_text; @value.to_s; end |