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

.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.



155
156
157
158
159
# File 'lib/gloo/objs/basic/string_msgs.rb', line 155

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.



173
174
175
176
177
# File 'lib/gloo/objs/basic/string_msgs.rb', line 173

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.



164
165
166
167
168
# File 'lib/gloo/objs/basic/string_msgs.rb', line 164

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.



244
245
246
247
248
249
# File 'lib/gloo/objs/basic/string_msgs.rb', line 244

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



331
332
333
334
335
336
# File 'lib/gloo/objs/basic/string_msgs.rb', line 331

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.



233
234
235
236
237
238
# File 'lib/gloo/objs/basic/string_msgs.rb', line 233

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:



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

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.



211
212
213
214
215
216
# File 'lib/gloo/objs/basic/string_msgs.rb', line 211

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.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gloo/objs/basic/string_msgs.rb', line 182

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.



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/gloo/objs/basic/string_msgs.rb', line 266

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.



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/gloo/objs/basic/string_msgs.rb', line 304

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.



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/gloo/objs/basic/string_msgs.rb', line 285

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.



254
255
256
257
258
259
# File 'lib/gloo/objs/basic/string_msgs.rb', line 254

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gloo/objs/basic/string_msgs.rb', line 102

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.



341
342
343
344
345
# File 'lib/gloo/objs/basic/string_msgs.rb', line 341

def msg_page
  return unless value

  @engine.platform.page( value )
end

#msg_sizeObject

Get the size of the string.



146
147
148
149
150
# File 'lib/gloo/objs/basic/string_msgs.rb', line 146

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

#msg_starts_with?Boolean

Does the string start with the given string?

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gloo/objs/basic/string_msgs.rb', line 40

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.



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

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:



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gloo/objs/basic/string_msgs.rb', line 129

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.



28
29
30
31
32
33
34
35
# File 'lib/gloo/objs/basic/string_msgs.rb', line 28

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.



222
223
224
225
226
227
# File 'lib/gloo/objs/basic/string_msgs.rb', line 222

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



321
322
323
324
325
326
# File 'lib/gloo/objs/basic/string_msgs.rb', line 321

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