Class: Gloo::Objs::Integer

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

Constant Summary collapse

KEYWORD =
'integer'.freeze
KEYWORD_SHORT =
'int'.freeze
DEFAULT_RANDOM_RANGE =
100

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.



49
50
51
# File 'lib/gloo/objs/basic/integer.rb', line 49

def self.messages
  return super + %w[inc dec randomize]
end

.short_typenameObject

The short name of the object type.



25
26
27
# File 'lib/gloo/objs/basic/integer.rb', line 25

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



18
19
20
# File 'lib/gloo/objs/basic/integer.rb', line 18

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_decObject

Decrement the integer



66
67
68
69
70
71
# File 'lib/gloo/objs/basic/integer.rb', line 66

def msg_dec
  i = value - 1
  set_value i
  @engine.heap.it.set_to i
  return i
end

#msg_incObject

Increment the integer



56
57
58
59
60
61
# File 'lib/gloo/objs/basic/integer.rb', line 56

def msg_inc
  i = value + 1
  set_value i
  @engine.heap.it.set_to i
  return i
end

#msg_randomizeObject

Set the value to a random number. The range is 0 to DEFAULT_RANDOM_RANGE (not including the range). To model a 6-sided die, set range to 6 and add 1 to the result.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gloo/objs/basic/integer.rb', line 79

def msg_randomize
  range = DEFAULT_RANDOM_RANGE

  # Check for a range.
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    range = expr.evaluate
  end

  rand_value = rand( range )
  set_value rand_value
  @engine.heap.it.set_to rand_value
  return rand_value
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



32
33
34
35
36
37
38
39
# File 'lib/gloo/objs/basic/integer.rb', line 32

def set_value( new_value )
  unless new_value.is_a? Numeric
    self.value = @engine.converter.convert( new_value, 'Integer', 0 )
    return
  end

  self.value = new_value.to_i
end