gloo 3.3.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/gloo.gemspec +8 -3
- data/lib/VERSION +1 -1
- data/lib/VERSION_NOTES +7 -0
- data/lib/gloo/app/engine.rb +1 -1
- data/lib/gloo/app/log.rb +15 -16
- data/lib/gloo/app/platform.rb +11 -90
- data/lib/gloo/app/prompt.rb +90 -0
- data/lib/gloo/app/table.rb +51 -0
- data/lib/gloo/core/gloo_system.rb +7 -14
- data/lib/gloo/objs/basic/container.rb +1 -2
- data/lib/gloo/objs/basic/integer.rb +23 -1
- data/lib/gloo/objs/basic/string.rb +116 -1
- data/lib/gloo/objs/basic/string_generator.rb +49 -0
- data/lib/gloo/objs/basic/text.rb +1 -17
- data/lib/gloo/objs/cli/menu.rb +5 -4
- data/lib/gloo/objs/cli/select.rb +3 -2
- data/lib/gloo/objs/data/markdown.rb +25 -30
- data/lib/gloo/objs/data/mysql.rb +39 -27
- data/lib/gloo/objs/data/pg.rb +1 -1
- data/lib/gloo/objs/data/query_result.rb +4 -9
- data/lib/gloo/objs/data/table.rb +1 -1
- data/lib/gloo/objs/security/cipher.rb +193 -0
- data/lib/gloo/objs/security/password.rb +167 -0
- data/lib/gloo/objs/system/file_handle.rb +1 -3
- data/lib/gloo/objs/web/json.rb +3 -0
- data/lib/gloo/objs/web_svr/page.rb +24 -8
- data/lib/gloo/objs/web_svr/partial.rb +7 -6
- data/lib/gloo/objs/web_svr/svr.rb +267 -14
- data/lib/gloo/verbs/version.rb +1 -1
- data/lib/gloo/web_svr/asset.rb +34 -13
- data/lib/gloo/web_svr/config.rb +1 -1
- data/lib/gloo/web_svr/embedded_renderer.rb +1 -1
- data/lib/gloo/web_svr/handler.rb +6 -4
- data/lib/gloo/web_svr/request.rb +34 -8
- data/lib/gloo/web_svr/response.rb +14 -2
- data/lib/gloo/web_svr/response_code.rb +1 -1
- data/lib/gloo/web_svr/routing/router.rb +1 -2
- data/lib/gloo/web_svr/routing/show_routes.rb +4 -7
- data/lib/gloo/web_svr/server.rb +1 -1
- data/lib/gloo/web_svr/session.rb +161 -0
- data/lib/gloo/web_svr/table_renderer.rb +1 -1
- metadata +58 -25
- data/lib/gloo/objs/cli/banner.rb +0 -118
- data/lib/gloo/objs/cli/bar.rb +0 -133
- data/lib/gloo/objs/cli/pastel.rb +0 -104
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34119d3b6c3fc676b45d7172a6300e1bef50e7bfd480ecaaeab9ad39be040cc4
|
4
|
+
data.tar.gz: b812253883e7589dea4b6588b85661a1cf6e849a61e2c36a658be8279a0a6411
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33d90fe0f4135042d0d5d6cfa4323438183e43e034ce525542bff0aa34bd16baf703ed7819de51c5ad19b38dd51fec8423a4999179e08e41e0dbc89ad0f3d20c
|
7
|
+
data.tar.gz: 017e592805017fc75f776a9a1673eb50aba32ef227260c573f382e8dc83d067ac2429b1f18d722df22672b8f7c57cd941769180d517398db74cde98aeb56a64d
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-3.
|
1
|
+
ruby-3.2.5
|
data/gloo.gemspec
CHANGED
@@ -33,10 +33,14 @@ Gem::Specification.new do |spec|
|
|
33
33
|
|
34
34
|
spec.add_dependency "activesupport", '~> 6.1', ">= 6.1.5"
|
35
35
|
spec.add_dependency 'chronic', '~> 0.10', '>= 0.10.2'
|
36
|
-
|
37
|
-
spec.add_dependency '
|
36
|
+
|
37
|
+
spec.add_dependency 'colorize', '~> 1.1.0', '>= 1.1.0'
|
38
|
+
spec.add_dependency 'inquirer'
|
39
|
+
spec.add_dependency 'reline'
|
40
|
+
spec.add_dependency 'terminal-table'
|
41
|
+
|
38
42
|
spec.add_dependency 'json', '~> 2.1', '>= 2.1.0'
|
39
|
-
spec.add_dependency 'openssl'
|
43
|
+
spec.add_dependency 'openssl'
|
40
44
|
spec.add_dependency 'net-ssh', '~> 6.1', '>= 6.1.0'
|
41
45
|
spec.add_dependency 'mysql2', '~> 0.5', '>= 0.5.3'
|
42
46
|
spec.add_dependency 'sqlite3', '~> 1.4', '>= 1.4.2'
|
@@ -44,4 +48,5 @@ Gem::Specification.new do |spec|
|
|
44
48
|
spec.add_dependency 'pg', '~> 1.5.4'
|
45
49
|
spec.add_dependency 'thin', '~> 1.8.2'
|
46
50
|
spec.add_dependency 'redcarpet', '~> 3.6.0'
|
51
|
+
spec.add_dependency 'bcrypt', '~> 3.1.20'
|
47
52
|
end
|
data/lib/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.4.0
|
data/lib/VERSION_NOTES
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
3.4.0 - 2024.09.22
|
2
|
+
- Refactor for old TTY lib
|
3
|
+
- Additional web server functionality
|
4
|
+
- Web session and security helpers
|
5
|
+
- Other functions in support of serving web pages and running web server
|
6
|
+
|
7
|
+
|
1
8
|
3.3.0 - 2024.08.16
|
2
9
|
- Adds nil converters to string and integer.
|
3
10
|
- Adds render to markdown object to convert to html
|
data/lib/gloo/app/engine.rb
CHANGED
data/lib/gloo/app/log.rb
CHANGED
@@ -34,7 +34,7 @@ module Gloo
|
|
34
34
|
# If quiet is true, then message are written to the log
|
35
35
|
# but not to the console.
|
36
36
|
#
|
37
|
-
def initialize( engine, quiet=true )
|
37
|
+
def initialize( engine, quiet = true )
|
38
38
|
@engine = engine
|
39
39
|
@quite = quiet
|
40
40
|
@debug = engine.settings.debug
|
@@ -59,30 +59,30 @@ module Gloo
|
|
59
59
|
# Files
|
60
60
|
# ---------------------------------------------------------------------
|
61
61
|
|
62
|
-
#
|
62
|
+
#
|
63
63
|
# Get the log file.
|
64
|
-
#
|
64
|
+
#
|
65
65
|
def log_file
|
66
66
|
return File.join( @engine.settings.log_path, LOG_FILE )
|
67
67
|
end
|
68
68
|
|
69
|
-
#
|
69
|
+
#
|
70
70
|
# Get the error log file.
|
71
|
-
#
|
71
|
+
#
|
72
72
|
def err_file
|
73
73
|
return File.join( @engine.settings.log_path, ERROR_FILE )
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
76
|
# ---------------------------------------------------------------------
|
78
77
|
# Static Helpers
|
79
78
|
# ---------------------------------------------------------------------
|
80
79
|
|
81
|
-
#
|
80
|
+
#
|
82
81
|
# Does the given str represent a logging level?
|
83
|
-
#
|
84
|
-
def self.is_level? str
|
82
|
+
#
|
83
|
+
def self.is_level?( str )
|
85
84
|
return false unless str.is_a? String
|
85
|
+
|
86
86
|
return LEVELS.include? str.strip.downcase
|
87
87
|
end
|
88
88
|
|
@@ -90,9 +90,9 @@ module Gloo
|
|
90
90
|
# Log file clearing
|
91
91
|
# ---------------------------------------------------------------------
|
92
92
|
|
93
|
-
#
|
93
|
+
#
|
94
94
|
# Clear the log files.
|
95
|
-
#
|
95
|
+
#
|
96
96
|
def clear
|
97
97
|
File.write( log_file, CLEARED )
|
98
98
|
File.write( err_file, CLEARED )
|
@@ -100,7 +100,6 @@ module Gloo
|
|
100
100
|
create_loggers
|
101
101
|
end
|
102
102
|
|
103
|
-
|
104
103
|
# ---------------------------------------------------------------------
|
105
104
|
# Standard Output
|
106
105
|
# ---------------------------------------------------------------------
|
@@ -108,13 +107,13 @@ module Gloo
|
|
108
107
|
#
|
109
108
|
# Show a message unless we're in quite mode.
|
110
109
|
#
|
111
|
-
def show( msg, color=nil )
|
110
|
+
def show( msg, color = nil )
|
112
111
|
return if @quiet
|
113
112
|
|
114
113
|
if color
|
115
114
|
puts ColorizedString[ msg ].colorize( color.to_sym )
|
116
115
|
else
|
117
|
-
puts msg
|
116
|
+
puts msg
|
118
117
|
end
|
119
118
|
end
|
120
119
|
|
@@ -122,9 +121,9 @@ module Gloo
|
|
122
121
|
# Logging functions
|
123
122
|
# ---------------------------------------------------------------------
|
124
123
|
|
125
|
-
#
|
124
|
+
#
|
126
125
|
# Write to the specified level.
|
127
|
-
#
|
126
|
+
#
|
128
127
|
def write( msg, level )
|
129
128
|
if level == DEBUG
|
130
129
|
debug msg
|
data/lib/gloo/app/platform.rb
CHANGED
@@ -4,16 +4,8 @@
|
|
4
4
|
# Handle input and output for the CLI platform.
|
5
5
|
#
|
6
6
|
require 'active_support'
|
7
|
-
require 'tty-prompt'
|
8
|
-
require 'tty-cursor'
|
9
|
-
require 'tty-screen'
|
10
|
-
require 'tty-pager'
|
11
|
-
require 'tty-markdown'
|
12
|
-
require 'tty-editor'
|
13
|
-
require 'tty-table'
|
14
7
|
require 'colorize'
|
15
8
|
require 'colorized_string'
|
16
|
-
require 'pastel'
|
17
9
|
|
18
10
|
module Gloo
|
19
11
|
module App
|
@@ -21,62 +13,28 @@ module Gloo
|
|
21
13
|
|
22
14
|
DEFAULT_TMP_FILE = 'tmp.txt'.freeze
|
23
15
|
|
24
|
-
attr_reader :prompt
|
16
|
+
attr_reader :prompt, :table
|
25
17
|
|
26
18
|
#
|
27
19
|
# Set up Platform.
|
28
20
|
#
|
29
21
|
def initialize
|
30
|
-
@prompt =
|
22
|
+
@prompt = Gloo::App::Prompt.new( self )
|
23
|
+
@table = Gloo::App::Table.new( self )
|
31
24
|
end
|
32
25
|
|
33
26
|
#
|
34
27
|
# Show a message.
|
35
28
|
#
|
36
|
-
def show( msg
|
37
|
-
|
38
|
-
# 2024.08.16 - TTY::Markdown.parse msg was not working.
|
39
|
-
# Tried with redcarpet and it was not working either.
|
40
|
-
# So just leaving it for now. Not sure I even really need it.
|
41
|
-
# TODO: Revisit and clean up.
|
42
|
-
|
43
|
-
# msg = TTY::Markdown.parse msg
|
44
|
-
# msg = Gloo::Objs::Markdown.md_2_manpage( msg )
|
45
|
-
end
|
46
|
-
|
47
|
-
if page
|
48
|
-
# pager = TTY::Pager::SystemPager.new command: 'less -R'
|
49
|
-
pager = TTY::Pager.new
|
50
|
-
pager.page( msg )
|
51
|
-
else
|
52
|
-
puts msg
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
#
|
57
|
-
# Prompt for the next command.
|
58
|
-
#
|
59
|
-
def prompt_cmd
|
60
|
-
return @prompt.ask( default_prompt )
|
29
|
+
def show( msg )
|
30
|
+
puts msg
|
61
31
|
end
|
62
32
|
|
63
33
|
#
|
64
34
|
# Clear the screen.
|
65
35
|
#
|
66
36
|
def clear_screen
|
67
|
-
|
68
|
-
print @cursor.clear_screen
|
69
|
-
print @cursor.move_to( 0, 0 )
|
70
|
-
end
|
71
|
-
|
72
|
-
#
|
73
|
-
# Edit some temporary text and return the edited text.
|
74
|
-
#
|
75
|
-
def edit initial_value
|
76
|
-
tmp = File.join( $settings.tmp_path, DEFAULT_TMP_FILE )
|
77
|
-
File.open( tmp, 'w' ) { |file| file.write( initial_value ) }
|
78
|
-
TTY::Editor.open( tmp )
|
79
|
-
return File.read( tmp )
|
37
|
+
puts "\e[H\e[2J"
|
80
38
|
end
|
81
39
|
|
82
40
|
#
|
@@ -86,6 +44,7 @@ module Gloo
|
|
86
44
|
return Gloo::Persist::DiscMech.new( engine )
|
87
45
|
end
|
88
46
|
|
47
|
+
|
89
48
|
# ---------------------------------------------------------------------
|
90
49
|
# Color helper
|
91
50
|
# ---------------------------------------------------------------------
|
@@ -98,36 +57,6 @@ module Gloo
|
|
98
57
|
return colorized.to_s
|
99
58
|
end
|
100
59
|
|
101
|
-
# ---------------------------------------------------------------------
|
102
|
-
# Table helper
|
103
|
-
# ---------------------------------------------------------------------
|
104
|
-
|
105
|
-
#
|
106
|
-
# Show the given table data.
|
107
|
-
#
|
108
|
-
def show_table( headers, data, title = nil )
|
109
|
-
pastel = ::Pastel.new
|
110
|
-
if headers
|
111
|
-
table = TTY::Table.new headers, data
|
112
|
-
else
|
113
|
-
table = TTY::Table.new rows: data
|
114
|
-
end
|
115
|
-
pad = [ 0, 1, 0, 1 ]
|
116
|
-
rendered = table.render( :ascii, indent: 2, padding: pad ) do |r|
|
117
|
-
r.border.style = :blue
|
118
|
-
r.filter = proc do |val, row_index, _col_index|
|
119
|
-
# col_index % 2 == 1 ? pastel.red.on_green(val) : val
|
120
|
-
if headers && row_index.zero?
|
121
|
-
pastel.blue( val )
|
122
|
-
else
|
123
|
-
row_index.odd? ? pastel.white( val ) : pastel.yellow( val )
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
puts
|
128
|
-
puts "#{title.white}" if title
|
129
|
-
puts "#{rendered}\n\n"
|
130
|
-
end
|
131
60
|
|
132
61
|
# ---------------------------------------------------------------------
|
133
62
|
# Sceen helpers
|
@@ -136,14 +65,16 @@ module Gloo
|
|
136
65
|
# Get the number of vertical lines on screen.
|
137
66
|
#
|
138
67
|
def lines
|
139
|
-
|
68
|
+
rows, columns = $stdout.winsize
|
69
|
+
return rows
|
140
70
|
end
|
141
71
|
|
142
72
|
#
|
143
73
|
# Get the number of horizontal columns on screen.
|
144
74
|
#
|
145
75
|
def cols
|
146
|
-
|
76
|
+
rows, columns = $stdout.winsize
|
77
|
+
return columns
|
147
78
|
end
|
148
79
|
|
149
80
|
# ---------------------------------------------------------------------
|
@@ -152,16 +83,6 @@ module Gloo
|
|
152
83
|
|
153
84
|
private
|
154
85
|
|
155
|
-
#
|
156
|
-
# Get the default prompt text.
|
157
|
-
#
|
158
|
-
def default_prompt
|
159
|
-
dt = DateTime.now
|
160
|
-
d = dt.strftime( '%Y.%m.%d' )
|
161
|
-
t = dt.strftime( '%I:%M:%S' )
|
162
|
-
return "#{'gloo'.blue} #{d.yellow} #{t.white} >"
|
163
|
-
end
|
164
|
-
|
165
86
|
end
|
166
87
|
end
|
167
88
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# Author:: Eric Crane (mailto:eric.crane@mac.com)
|
2
|
+
# Copyright:: Copyright (c) 2024 Eric Crane. All rights reserved.
|
3
|
+
#
|
4
|
+
# CLI input.
|
5
|
+
#
|
6
|
+
require 'active_support'
|
7
|
+
require 'colorize'
|
8
|
+
require 'colorized_string'
|
9
|
+
require 'inquirer'
|
10
|
+
require "reline"
|
11
|
+
|
12
|
+
module Gloo
|
13
|
+
module App
|
14
|
+
class Prompt
|
15
|
+
|
16
|
+
#
|
17
|
+
# Set up Prompt.
|
18
|
+
#
|
19
|
+
def initialize platform
|
20
|
+
@platform = platform
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Show the prompt and get input.
|
25
|
+
# Use the default prompt if none is provided.
|
26
|
+
#
|
27
|
+
def ask( prompt=nil )
|
28
|
+
prompt ||= default_prompt
|
29
|
+
|
30
|
+
response = Reline.readline( "#{prompt} ", true)
|
31
|
+
|
32
|
+
# I don't like this one because it appends a ':' to the prompt.
|
33
|
+
# response = Ask.input prompt
|
34
|
+
|
35
|
+
# This was just the brute force way to do it.
|
36
|
+
# puts prompt
|
37
|
+
# return $stdin.gets.chomp
|
38
|
+
|
39
|
+
return response
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Prompt for multiline input.
|
44
|
+
#
|
45
|
+
def multiline( prompt )
|
46
|
+
puts 'To end input, type a period on a line by itself.'
|
47
|
+
text = Reline.readmultiline( "#{prompt} ", true ) do |input|
|
48
|
+
input.split.last == '.'
|
49
|
+
end
|
50
|
+
|
51
|
+
return text.lines[0..-2]
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Confirmation prompt.
|
56
|
+
# Answer is yes or no.
|
57
|
+
#
|
58
|
+
def yes?( prompt )
|
59
|
+
value = Ask.confirm "#{prompt} "
|
60
|
+
return value
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Show a selection list to choose from.
|
65
|
+
#
|
66
|
+
def select( prompt, options )
|
67
|
+
i = Ask.list prompt, options
|
68
|
+
return options[ i ]
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# ---------------------------------------------------------------------
|
73
|
+
# Private Functions
|
74
|
+
# ---------------------------------------------------------------------
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
#
|
79
|
+
# Get the default prompt text.
|
80
|
+
#
|
81
|
+
def default_prompt
|
82
|
+
dt = DateTime.now
|
83
|
+
d = dt.strftime( '%Y.%m.%d' )
|
84
|
+
t = dt.strftime( '%I:%M:%S' )
|
85
|
+
return "#{'gloo'.blue} #{d.yellow} #{t.white} >"
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Author:: Eric Crane (mailto:eric.crane@mac.com)
|
2
|
+
# Copyright:: Copyright (c) 2024 Eric Crane. All rights reserved.
|
3
|
+
#
|
4
|
+
# CLI input.
|
5
|
+
#
|
6
|
+
require 'colorize'
|
7
|
+
require 'terminal-table'
|
8
|
+
|
9
|
+
module Gloo
|
10
|
+
module App
|
11
|
+
class Table
|
12
|
+
|
13
|
+
#
|
14
|
+
# Set up Table Helper.
|
15
|
+
#
|
16
|
+
def initialize platform
|
17
|
+
@platform = platform
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Put a box around the given string.
|
22
|
+
def box( str_value )
|
23
|
+
puts str_value
|
24
|
+
box = Terminal::Table.new( :headings => [], :rows => [ [ str_value ] ] )
|
25
|
+
return box.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Show the given table data.
|
30
|
+
#
|
31
|
+
def show( headers, data, title = nil )
|
32
|
+
unless title.blank?
|
33
|
+
table = Terminal::Table.new(
|
34
|
+
:title => title, :headings => headers, :rows => data )
|
35
|
+
else
|
36
|
+
table = Terminal::Table.new( :headings => headers, :rows => data )
|
37
|
+
end
|
38
|
+
puts table.to_s.colorize( color: :white, background: :black )
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# ---------------------------------------------------------------------
|
43
|
+
# Private Functions
|
44
|
+
# ---------------------------------------------------------------------
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -6,7 +6,6 @@
|
|
6
6
|
# system level variables and functions. But it is not
|
7
7
|
# actually an object in the normal sense of the word.
|
8
8
|
#
|
9
|
-
require 'tty-platform'
|
10
9
|
require 'os'
|
11
10
|
|
12
11
|
module Gloo
|
@@ -217,20 +216,17 @@ module Gloo
|
|
217
216
|
|
218
217
|
# Get the platform CPU
|
219
218
|
def msg_platform_cpu
|
220
|
-
|
221
|
-
return platform.cpu
|
219
|
+
return OS.host_cpu
|
222
220
|
end
|
223
221
|
|
224
222
|
# Get the platform Operating System
|
225
223
|
def msg_platform_os
|
226
|
-
|
227
|
-
return platform.os
|
224
|
+
return RUBY_PLATFORM
|
228
225
|
end
|
229
226
|
|
230
227
|
# Get the platform version
|
231
228
|
def msg_platform_version
|
232
|
-
|
233
|
-
return platform.version
|
229
|
+
return 'n/a'
|
234
230
|
end
|
235
231
|
|
236
232
|
# Is the platform Windows?
|
@@ -240,14 +236,12 @@ module Gloo
|
|
240
236
|
|
241
237
|
# Is the platform Unix?
|
242
238
|
def msg_platform_unix?
|
243
|
-
|
244
|
-
return platform.unix?
|
239
|
+
return OS.posix?
|
245
240
|
end
|
246
241
|
|
247
242
|
# Is the platform Linux?
|
248
243
|
def msg_platform_linux?
|
249
|
-
|
250
|
-
return platform.linux?
|
244
|
+
return OS.posix?
|
251
245
|
end
|
252
246
|
|
253
247
|
# Is the platform Mac?
|
@@ -259,9 +253,8 @@ module Gloo
|
|
259
253
|
# Get the command to open a file on this platform.
|
260
254
|
#
|
261
255
|
def self.open_for_platform
|
262
|
-
|
263
|
-
return 'open' if
|
264
|
-
return 'xdg-open' if platform.linux?
|
256
|
+
return 'open' if OS.mac?
|
257
|
+
return 'xdg-open' if OS.posix?
|
265
258
|
|
266
259
|
return 'Start-Process' if OS.windows?
|
267
260
|
|
@@ -10,6 +10,7 @@ module Gloo
|
|
10
10
|
|
11
11
|
KEYWORD = 'integer'.freeze
|
12
12
|
KEYWORD_SHORT = 'int'.freeze
|
13
|
+
DEFAULT_RANDOM_RANGE = 100
|
13
14
|
|
14
15
|
#
|
15
16
|
# The name of the object type.
|
@@ -46,7 +47,7 @@ module Gloo
|
|
46
47
|
# Get a list of message names that this object receives.
|
47
48
|
#
|
48
49
|
def self.messages
|
49
|
-
return super + %w[inc dec]
|
50
|
+
return super + %w[inc dec randomize]
|
50
51
|
end
|
51
52
|
|
52
53
|
#
|
@@ -69,6 +70,27 @@ module Gloo
|
|
69
70
|
return i
|
70
71
|
end
|
71
72
|
|
73
|
+
#
|
74
|
+
# Set the value to a random number.
|
75
|
+
# The range is 0 to DEFAULT_RANDOM_RANGE (not including the range).
|
76
|
+
# To model a 6-sided die,
|
77
|
+
# set range to 6 and add 1 to the result.
|
78
|
+
#
|
79
|
+
def msg_randomize
|
80
|
+
range = DEFAULT_RANDOM_RANGE
|
81
|
+
|
82
|
+
# Check for a range.
|
83
|
+
if @params&.token_count&.positive?
|
84
|
+
expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
|
85
|
+
range = expr.evaluate
|
86
|
+
end
|
87
|
+
|
88
|
+
rand_value = rand( range )
|
89
|
+
set_value rand_value
|
90
|
+
@engine.heap.it.set_to rand_value
|
91
|
+
return rand_value
|
92
|
+
end
|
93
|
+
|
72
94
|
end
|
73
95
|
end
|
74
96
|
end
|