Class: Gloo::Core::Obj

Inherits:
Baseo
  • Object
show all
Defined in:
lib/gloo/core/obj.rb

Constant Summary

Constants inherited from Baseo

Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary collapse

Attributes inherited from Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Obj

Set up the object.



18
19
20
21
22
23
# File 'lib/gloo/core/obj.rb', line 18

def initialize( engine )
  @engine = engine
  @value = ''
  @children = []
  @parent = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/gloo/core/obj.rb', line 13

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/gloo/core/obj.rb', line 13

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/gloo/core/obj.rb', line 12

def value
  @value
end

Class Method Details

.can_create?Boolean

Can this object be created? This is true by default and only false for some special cases such as the System object.

Returns:

  • (Boolean)


66
67
68
# File 'lib/gloo/core/obj.rb', line 66

def self.can_create?
  true
end

.helpObject

Get help for this object.



431
432
433
# File 'lib/gloo/core/obj.rb', line 431

def self.help
  return 'No help found.'
end

.inherited(subclass) ⇒ Object

Register object types when they are loaded.



28
29
30
# File 'lib/gloo/core/obj.rb', line 28

def self.inherited( subclass )
  Dictionary.instance.register_obj( subclass )
end

.messagesObject

Get a list of message names that this object receives.



308
309
310
# File 'lib/gloo/core/obj.rb', line 308

def self.messages
  return %w[reload unload blank? contains? responds_to?]
end

.typenameObject

The name of the object type.



35
36
37
# File 'lib/gloo/core/obj.rb', line 35

def self.typename
  raise 'this method should be overriden'
end

Instance Method Details

#add_child(obj) ⇒ Object

Add a child object to the container.



195
196
197
198
# File 'lib/gloo/core/obj.rb', line 195

def add_child( obj )
  @children << obj
  obj.set_parent self
end

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


290
291
292
# File 'lib/gloo/core/obj.rb', line 290

def add_children_on_create?
  return false
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



297
298
299
# File 'lib/gloo/core/obj.rb', line 297

def add_default_children
  # Override this.
end

#can_receive_message?(msg) ⇒ Boolean

Can this object receive a message?

Returns:

  • (Boolean)


315
316
317
318
# File 'lib/gloo/core/obj.rb', line 315

def can_receive_message?( msg )
  msgs = self.class.messages
  return msgs.include?( msg.strip.downcase )
end

#child_countObject

Get the number of children.



203
204
205
# File 'lib/gloo/core/obj.rb', line 203

def child_count
  return @children.count
end

#child_index(name) ⇒ Object

Get the index of the child with the given name.



264
265
266
267
268
269
# File 'lib/gloo/core/obj.rb', line 264

def child_index( name )
  @children.each_with_index do |o, i|
    return i if name.downcase == o.name.downcase
  end
  return nil
end

#contains_child?(name) ⇒ Boolean

Does this object contain an object with the given name?

Returns:

  • (Boolean)


210
211
212
213
214
215
# File 'lib/gloo/core/obj.rb', line 210

def contains_child?( name )
  @children.each do |o|
    return true if name.downcase == o.name.downcase
  end
  return false
end

#delete_childrenObject

Delete all children from the container.



274
275
276
277
278
# File 'lib/gloo/core/obj.rb', line 274

def delete_children
  @children.reverse.each do |o|
    self.remove_child o
  end
end

#dispatch(msg) ⇒ Object

Dispatch the message to the object.



334
335
336
337
338
339
340
341
342
343
# File 'lib/gloo/core/obj.rb', line 334

def dispatch( msg )
  o = "msg_#{msg}"
  if self.respond_to? o
    self.public_send( o )
    return true
  else
    @engine.err "Message #{msg} not implemented"
    return false
  end
end

#display_valueObject

Generic function to get display value. Can be used for debugging, etc.



91
92
93
# File 'lib/gloo/core/obj.rb', line 91

def display_value
  return self.pn
end

#find_add_child(name, type) ⇒ Object

Find a child of the given name. If found, return it. If not found create it.



181
182
183
184
185
186
187
188
189
190
# File 'lib/gloo/core/obj.rb', line 181

def find_add_child( name, type )
  child = self.find_child( name )
  return child if child

  params = { :name => name,
             :type => type,
             :value => nil,
             :parent => self }
  return @engine.factory.create params
end

#find_child(name) ⇒ Object

Find a child object with the given name.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gloo/core/obj.rb', line 220

def find_child( name )
  if name.end_with?( Gloo::Objs::Alias::ALIAS_REFERENCE )
    name = name[ 0..-2 ]
  end

  @children.each do |o|
    return o if name.downcase == o.name.downcase
  end

  if is_alias?
    ln = Gloo::Core::Pn.new( @engine, self.value )
    redirect = ln.resolve
    return redirect.find_child( name )
  end
  return nil
end

#find_child_resolve_alias(name) ⇒ Object

Find a child, resolve any alias references. This returns the object, not the value.



241
242
243
244
245
246
247
# File 'lib/gloo/core/obj.rb', line 241

def find_child_resolve_alias( name )
  o = find_child name
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#find_child_value(name) ⇒ Object

Find a child, resolve any alias references, and return the object's value.



253
254
255
256
257
258
259
# File 'lib/gloo/core/obj.rb', line 253

def find_child_value( name )
  o = find_child name
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#is_alias?Boolean

Is this an alias to another object?

Returns:

  • (Boolean)


154
155
156
# File 'lib/gloo/core/obj.rb', line 154

def is_alias?
  return self.type_display == Gloo::Objs::Alias.typename
end

#is_container?Boolean

Is this a container object?

Returns:

  • (Boolean)


161
162
163
# File 'lib/gloo/core/obj.rb', line 161

def is_container?
  return self.type_display == Gloo::Objs::Container.typename
end

#is_function?Boolean

Is this a function object?

Returns:

  • (Boolean)


168
169
170
# File 'lib/gloo/core/obj.rb', line 168

def is_function?
  return self.type_display == Gloo::Objs::Function.typename
end

#msg_blank?Boolean

Check to see if the value is blank.

Returns:

  • (Boolean)


377
378
379
380
381
# File 'lib/gloo/core/obj.rb', line 377

def msg_blank?
  val_blank = value.blank?
  @engine.heap.it.set_to val_blank
  return val_blank
end

#msg_contains?Boolean

Check to see if there are children.

Returns:

  • (Boolean)


386
387
388
389
390
# File 'lib/gloo/core/obj.rb', line 386

def msg_contains?
  has_children = child_count.positive?
  @engine.heap.it.set_to has_children
  return has_children
end

#msg_reloadObject

Send the object the reload message. Note that this will only work for objects with file assoications. This will reload the object but the rest of the engine state will remain unchanged.



365
366
367
368
369
370
371
372
# File 'lib/gloo/core/obj.rb', line 365

def msg_reload
  if self.root?
    @engine.err 'Cannot reload the root object.'
    return
  end

  @engine.persist_man.reload self
end

#msg_responds_to?Boolean

Check to see if the object responds to a message.

Returns:

  • (Boolean)


395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/gloo/core/obj.rb', line 395

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

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

#msg_unloadObject

Send the object the unload message. This will unload the object but the rest of the engine state will remain unchanged.



350
351
352
353
354
355
356
357
# File 'lib/gloo/core/obj.rb', line 350

def msg_unload
  if self.root?
    @engine.err 'Cannot unload the root object.'
    return
  end

  @engine.persist_man.unload self
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:

  • (Boolean)


117
118
119
# File 'lib/gloo/core/obj.rb', line 117

def multiline_value?
  return false
end

#pnObject

Get the path and name to this object. Paths are always relative to root, so root itself has no path/name segment of its own.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gloo/core/obj.rb', line 75

def pn
  return '' if self.root?

  str = self.name
  p = self.parent
  while p && !p.root?
    str = "#{p.name}.#{str}"
    p = p.parent
  end
  return str
end

#remove_child(obj) ⇒ Object

Remove the object from the children collection.



283
284
285
# File 'lib/gloo/core/obj.rb', line 283

def remove_child( obj )
  @children.delete obj
end

#render(render_ƒ) ⇒ Object

Render the object. By default this is just the object's value. The render_ƒ is 'render_html', 'render_text', 'render_json', etc.



419
420
421
# File 'lib/gloo/core/obj.rb', line 419

def render render_ƒ
  return self.value.to_s
end

#root?Boolean

Is this the root object?

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/gloo/core/obj.rb', line 56

def root?
  return false if @parent
  return false unless name&.downcase == 'root'

  return true
end

#send_message(msg, params = nil) ⇒ Object

Sent this object the given message.



323
324
325
326
327
328
329
# File 'lib/gloo/core/obj.rb', line 323

def send_message( msg, params = nil )
  @params = params
  return self.dispatch msg if self.can_receive_message? msg

  @engine.err "Object #{self.name} cannot receive message #{msg}"
  return false
end

#set_parent(obj) ⇒ Object

Set the parent for the object.



49
50
51
# File 'lib/gloo/core/obj.rb', line 49

def set_parent( obj )
  @parent = obj
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



102
103
104
# File 'lib/gloo/core/obj.rb', line 102

def set_value( new_value )
  self.value = new_value
end

#sql_valueObject

Value for a SQL query.



147
148
149
# File 'lib/gloo/core/obj.rb', line 147

def sql_value
  return self.value
end

#type_displayObject

The object type, suitable for display.



42
43
44
# File 'lib/gloo/core/obj.rb', line 42

def type_display
  return self.class.typename
end

#value_displayObject

Get the value for display purposes.



109
110
111
# File 'lib/gloo/core/obj.rb', line 109

def value_display
  return self.value.to_s
end

#value_is_array?Boolean

Is the value an Array?

Returns:

  • (Boolean)


131
132
133
# File 'lib/gloo/core/obj.rb', line 131

def value_is_array?
  return self.value.is_a? Array
end

#value_is_blank?Boolean

Is the value a blank string?

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/gloo/core/obj.rb', line 138

def value_is_blank?
  return true if value.nil?

  return self.value.to_s.strip.empty?
end

#value_string?Boolean

Is the value a String?

Returns:

  • (Boolean)


124
125
126
# File 'lib/gloo/core/obj.rb', line 124

def value_string?
  return self.value.is_a? String
end