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, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #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

.doc_dataObject

Get the object's documentation data.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gloo/objs/basic/integer.rb', line 128

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'An integer (numeric) value.',
    :messages => [
      'inc — Increment the integer value by 1.',
      'dec — Decrement the integer value by 1.',
      'randomize ({max}) — Set the value of the integer to a random ' \
        'number. By default the range is 0..99 inclusive. Use an ' \
        'optional parameter to set the maximum of the range; the ' \
        'range always starts at 0. To model a 6-sided die, set the ' \
        'range to 6 and add 1 to the result.',
      'format ({fmt}) — With no parameter, adds comma separators (e.g. 1000 -> 1,000). With a parameter, uses it as a sprintf-style format string (e.g. \'%05d\'). It will have the formatted string.'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Integer object.
      #

      i [can] :

        #
        # The integer value.
        #
        x [integer] : 0

        #
        # Do some basic tests with it
        #
        on_load [script] :
          show i.x
          tell i.x to inc
          show i.x
          put i.x * 10 into i.x
          show i.x

          # Show a random number
          tell ^.x to randomize
          show 'Random number (up to 100 by default): ' + ^.x

          tell ^.x to randomize(6)
          tell ^.x to inc
          show '6-sided dice: ' + ^.x

        # An uninitialized integer.
        y [integer] :
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



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

def self.messages
  return super + %w[inc dec randomize format]
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



75
76
77
78
79
80
# File 'lib/gloo/objs/basic/integer.rb', line 75

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

#msg_formatObject

Format the integer. With no parameter, adds comma separators (Ex: 1000 -> 1,000). With a parameter, uses it as a sprintf-style format string (Ex: '%05d').

Parameters:

  • format (String)

    The sprintf-style format to use.



110
111
112
113
114
115
116
117
118
119
# File 'lib/gloo/objs/basic/integer.rb', line 110

def msg_format
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    fmt = expr.evaluate
    formatted = format( fmt, value )
  else
    formatted = value.to_s.reverse.scan( /.{1,3}/ ).join( ',' ).reverse
  end
  @engine.heap.it.set_to formatted
end

#msg_incObject

Increment the integer



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

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gloo/objs/basic/integer.rb', line 88

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

#sql_valueObject

Value for a SQL query.



44
45
46
47
48
# File 'lib/gloo/objs/basic/integer.rb', line 44

def sql_value
  return nil if self.value.blank?
  
  return self.value
end