Class: Gloo::Objs::Decimal

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/basic/decimal.rb

Constant Summary collapse

KEYWORD =
'decimal'.freeze
KEYWORD_SHORT =
'num'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #is_alias?, #is_function?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



52
53
54
# File 'lib/gloo/objs/basic/decimal.rb', line 52

def self.messages
  return super + %w[round]
end

.short_typenameObject

The short name of the object type.



24
25
26
# File 'lib/gloo/objs/basic/decimal.rb', line 24

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



17
18
19
# File 'lib/gloo/objs/basic/decimal.rb', line 17

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_roundObject

Round the value to a whole value. If a parameter is included in the message, round to the precision given.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gloo/objs/basic/decimal.rb', line 61

def msg_round
  data = 0
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate.to_i
  end

  i = self.value.round( data )
  set_value i
  @engine.heap.it.set_to i
  return i
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gloo/objs/basic/decimal.rb', line 31

def set_value( new_value )
  if new_value.nil?
    self.value = 0.0
    return
  end

  unless new_value.is_a? Numeric
    self.value = @engine.converter.convert( new_value, 'Decimal', 0.0 )
    return
  end

  self.value = new_value.to_f
end