Module: Gloo::Objs::StringMsgs

Included in:
String, Text
Defined in:
lib/gloo/objs/basic/string_msgs.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.message_docsObject

Documentation for each message this mixin implements, for use in String and Text's doc_data (see help_shell). Kept here as the single source, since String and Text share the exact same message set and would otherwise have to keep two hand-written copies of this list in sync.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gloo/objs/basic/string_msgs.rb', line 31

def self.message_docs
  return [
    'up — Convert the string to uppercase. This message changes the value of the string.',
    'down — Convert the string to lowercase. This message changes the value of the string.',
    'size — Get the size of the string. It will have the string size.',
    'count_chars — Count the number of characters in the string. It will have the character count.',
    'count_words — Count the number of words in the string. It will have the word count.',
    'count_lines — Count the number of lines in the string. It will have the line count.',
    'starts_with? ({str}) — Check if the string starts with the given string. A parameter is required: the string to look for at the beginning of this string. It will have a boolean.',
    'ends_with? ({str}) — Check if the string ends with the given string. A parameter is required: the string to look for at the end of this string. It will have a boolean.',
    'substring? ({str}) — Check if the string includes the given sub-string. A parameter is required: the string to look for in this string. It will have a boolean.',
    'format_for_html — Format this string for HTML output. Tabs, spaces and returns are converted to HTML elements. The value of the string is changed.',
    'encode64 — Base64 encode the string. This message changes the value of the string. It will have the encoded string.',
    'decode64 — Decode the string from Base64. This message changes the value of the string. It will have the decoded string.',
    'escape — Escape the string to make it URL safe. This message changes the value of the string. It will have the escaped string.',
    'unescape — Unescape the string (from URL safe format). This message changes the value of the string. It will have the unescaped string.',
    'gen_uuid — Set the value of the string to a newly generated, random UUID. This message changes the value of the string.',
    'gen_alphanumeric ({len}) — Set the value of the string to a newly generated, random alphanumeric string. The {len} parameter is optional; the length is 10 if not specified. This message changes the value of the string.',
    'gen_hex ({len}) — Set the value of the string to a newly generated, random hex string. The {len} parameter is optional; the length is 10 if not specified. This message changes the value of the string.',
    'gen_base64 ({len}) — Set the value of the string to a newly generated, random base64 string. The {len} parameter is optional; the length is 12 if not specified. This message changes the value of the string.',
    'trim — Strip whitespace from the beginning and end of the string. This message changes the value of the string. It will have the trimmed string.',
    'sub ({from} {to}) — Substitute the first occurrence of {from} with {to}. Both parameters are required. This message changes the value of the string. It will have the result.',
    'gsub ({from} {to}) — Substitute all occurrences of {from} with {to}. Both parameters are required. This message changes the value of the string. It will have the result.',
    'split ({from} {to}) — Get the substring from index {from} up to (not including) index {to}. Indexes are 0-based; out-of-range indexes are clamped to the start or end of the string. Both parameters are required. Does not change the value of the string. It will have the substring.',
    'splitl ({index}) — Get the substring to the left of index {index} (same as split (0, {index})). A parameter is required. Does not change the value of the string. It will have the substring.',
    'splitr ({index}) — Get the substring from index {index} to the end of the string (same as split ({index}, size)). A parameter is required. Does not change the value of the string. It will have the substring.',
    'split_list ({delim} {dst.path}) — Split the string by {delim} and put the parts into children of the container at {dst.path} (or an alias that points to one), one part per child, in order. Existing children are matched by position and have their values set; extra parts get new (untyped) children, numbered from 1; extra existing children are left alone. Both parameters are required. Does not change the value of the string. It will have the number of parts.',
    'page — Show the value in a pager (less), for viewing long content a screen at a time.'
  ]
end

.messagesObject

Get the list of message names this mixin implements, derived from its own msg_* methods so String/Text don't have to hand-duplicate the list (and can't drift out of sync with it).



18
19
20
21
22
# File 'lib/gloo/objs/basic/string_msgs.rb', line 18

def self.messages
  return instance_methods( false )
    .select { |m| m.to_s.start_with?( 'msg_' ) }
    .map { |m| m.to_s.sub( /\Amsg_/, '' ) }
end

Instance Method Details

#msg_count_charsObject

Get the number of characters in the string.



301
302
303
304
305
# File 'lib/gloo/objs/basic/string_msgs.rb', line 301

def msg_count_chars
  s = value.chars.count
  @engine.heap.it.set_to s
  return s
end

#msg_count_linesObject

Get the number of lines in the string.



319
320
321
322
323
# File 'lib/gloo/objs/basic/string_msgs.rb', line 319

def msg_count_lines
  s = value.split( "\n" ).count
  @engine.heap.it.set_to s
  return s
end

#msg_count_wordsObject

Get the number of words in the string.



310
311
312
313
314
# File 'lib/gloo/objs/basic/string_msgs.rb', line 310

def msg_count_words
  s = value.split( " " ).count
  @engine.heap.it.set_to s
  return s
end

#msg_decode64Object

Decode the string from base64. Changes the value of the string.



390
391
392
393
394
395
# File 'lib/gloo/objs/basic/string_msgs.rb', line 390

def msg_decode64
  s = Base64.decode64( value )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_downObject

Convert string to lower case



477
478
479
480
481
482
# File 'lib/gloo/objs/basic/string_msgs.rb', line 477

def msg_down
  s = value.downcase
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_encode64Object

Encode the string as base64. Changes the value of the string.



379
380
381
382
383
384
# File 'lib/gloo/objs/basic/string_msgs.rb', line 379

def msg_encode64
  s = Base64.encode64( value )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_ends_with?Boolean

Does the string end with the given string?

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gloo/objs/basic/string_msgs.rb', line 97

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

    result = value.end_with?( data )
    @engine.heap.it.set_to result
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_escapeObject

Escape the string. Make it URL safe. The value of the string is changed.



357
358
359
360
361
362
# File 'lib/gloo/objs/basic/string_msgs.rb', line 357

def msg_escape
  s = URI::DEFAULT_PARSER.escape( value )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_format_for_htmlObject

Convert whitespace to HTML friendly spaces.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/gloo/objs/basic/string_msgs.rb', line 328

def msg_format_for_html
  text = self.value
  out = ""
  return out unless text

  # indentation
  text.each_line do |line|
    i = 0
    while line[i] == ' '
      i += 1
      out << "&nbsp;"
    end
    
    i = 0
    while line[i] == "\t"
      i += 1
      out << "&nbsp;&nbsp;&nbsp;&nbsp;"
    end
    out << line
  end
    
  self.value = out.gsub( "\n", "<br/>" )
end

#msg_gen_alphanumericObject

Generate a random alphanumeric string. By default the length is 10 characters. Set the length with an optional parameter.



412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/gloo/objs/basic/string_msgs.rb', line 412

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

  s = StringGenerator.alphanumeric( len )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_gen_base64Object

Generate a random base64 string. By default the length is 12 characters. Set the length with an optional parameter.



450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/gloo/objs/basic/string_msgs.rb', line 450

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

  s = StringGenerator.base64( len )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_gen_hexObject

Generate a random hex string. By default the length is 10 hex characters. Set the length with an optional parameter.



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/gloo/objs/basic/string_msgs.rb', line 431

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

  s = StringGenerator.hex( len )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_gen_uuidObject

Generate a new UUID in the string.



400
401
402
403
404
405
# File 'lib/gloo/objs/basic/string_msgs.rb', line 400

def msg_gen_uuid
  s = StringGenerator.uuid
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_gsubObject

Substitute the given string with another string. Find all occurrences and replace them.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gloo/objs/basic/string_msgs.rb', line 140

def msg_gsub
  return '' unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
    from = expr.evaluate
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
    to = expr.evaluate

    result = value.gsub(from, to) 
    @engine.heap.it.set_to result
    set_value(result)
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_pageObject

Show the value in a pager, for long content.



487
488
489
490
491
# File 'lib/gloo/objs/basic/string_msgs.rb', line 487

def msg_page
  return unless value

  @engine.platform.page( value )
end

#msg_sizeObject

Get the size of the string.



292
293
294
295
296
# File 'lib/gloo/objs/basic/string_msgs.rb', line 292

def msg_size
  s = value.size
  @engine.heap.it.set_to s
  return s
end

#msg_splitObject

Get the substring from index from up to (not including) index to. Indexes are 0-based. Out-of-range indexes are clamped to the beginning or end of the string. Does not change the string's value.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gloo/objs/basic/string_msgs.rb', line 166

def msg_split
  return '' unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
    from = expr.evaluate.to_i
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
    to = expr.evaluate.to_i

    result = clamped_substring( from, to )
    @engine.heap.it.set_to result
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_split_listObject

Split the string by the given delimiter and put the parts into children of the target container, one part per child, in order. The target is a path to a container, or to an alias that points to one. Existing children are matched by position (not name) and have their values set; if there are more parts than children, new (untyped) children are created for the extras, numbered from 1. Existing children beyond the part count are left alone. The string's own value is unchanged; the number of parts is put into 'it'.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/gloo/objs/basic/string_msgs.rb', line 241

def msg_split_list
  return unless value

  if @params&.token_count.to_i < 2
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end

  expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
  delim = expr.evaluate

  target = split_list_target( @params.tokens.last )
  return false unless target

  parts = value.split( delim )
  existing = target.children
  parts.each_with_index do |part, index|
    child = existing[ index ]
    child ||= target.find_add_child( ( index + 1 ).to_s, 'untyped' )
    child.set_value part
  end

  count = parts.count
  @engine.heap.it.set_to count
  return count
end

#msg_splitlObject

Get the substring to the left of (not including) index index. Same as split( 0, index ). Does not change the string's value.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/gloo/objs/basic/string_msgs.rb', line 190

def msg_splitl
  return '' unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    index = expr.evaluate.to_i

    result = clamped_substring( 0, index )
    @engine.heap.it.set_to result
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_splitrObject

Get the substring from index index to the end of the string. Same as split( index, size ). Does not change the string's value.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/gloo/objs/basic/string_msgs.rb', line 212

def msg_splitr
  return '' unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    index = expr.evaluate.to_i

    result = clamped_substring( index, value.length )
    @engine.heap.it.set_to result
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_starts_with?Boolean

Does the string start with the given string?

Returns:



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

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

    result = self.value.start_with?( data )
    @engine.heap.it.set_to result
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_subObject

Substitute the given string with another string.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gloo/objs/basic/string_msgs.rb', line 116

def msg_sub
  return '' unless value
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
    from = expr.evaluate
    expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
    to = expr.evaluate

    result = value.sub(from, to)
    @engine.heap.it.set_to result
    set_value(result)
    return result
  else
    # Error
    @engine.log.error MISSING_PARAM_MSG
    @engine.heap.it.set_to false
    return false
  end
end

#msg_substring?Boolean

Does the string contain the given string?

This was formerly an overload of obj.contains? Contains? for the Obj checks for the presense of children.

Returns:



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/gloo/objs/basic/string_msgs.rb', line 275

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

    result = value.include?( data )
    @engine.heap.it.set_to result
    return result
  else
    @engine.heap.it.set_to false
    return false
  end
end

#msg_trimObject

Strip whitespace from the beginning and end of the string.



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

def msg_trim
  return '' unless value
  
  result = value.strip
  @engine.heap.it.set_to result
  set_value(result)
  return result
end

#msg_unescapeObject

Unescape the string. The value of the string is changed.



368
369
370
371
372
373
# File 'lib/gloo/objs/basic/string_msgs.rb', line 368

def msg_unescape
  s = URI::DEFAULT_PARSER.unescape( value )
  set_value s
  @engine.heap.it.set_to s
  return s
end

#msg_upObject

Convert string to upper case



467
468
469
470
471
472
# File 'lib/gloo/objs/basic/string_msgs.rb', line 467

def msg_up
  s = value.upcase
  set_value s
  @engine.heap.it.set_to s
  return s
end