Class: Gloo::Core::Obj

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

Instance Attribute Summary collapse

Attributes inherited from Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObj

Set up the object.



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

def initialize
  @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)


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

def self.can_create?
  true
end

.helpObject

Get help for this object.



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

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

.inherited(subclass) ⇒ Object

Register object types when they are loaded.



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

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

.messagesObject

Get a list of message names that this object receives.



240
241
242
# File 'lib/gloo/core/obj.rb', line 240

def self.messages
  return %w[unload]
end

.typenameObject

The name of the object type.



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

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

Instance Method Details

#add_child(obj) ⇒ Object

Add a child object to the container.



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

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)


222
223
224
# File 'lib/gloo/core/obj.rb', line 222

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.



229
230
231
# File 'lib/gloo/core/obj.rb', line 229

def add_default_children
  # Override this.
end

#can_receive_message?(msg) ⇒ Boolean

Can this object receive a message?

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/gloo/core/obj.rb', line 247

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

#child_countObject

Get the number of children.



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

def child_count
  return @children.count
end

#contains_child?(name) ⇒ Boolean

Does this object contain an object with the given name?

Returns:

  • (Boolean)


176
177
178
179
180
181
# File 'lib/gloo/core/obj.rb', line 176

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.



206
207
208
209
210
# File 'lib/gloo/core/obj.rb', line 206

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

#dispatch(msg) ⇒ Object

Dispatch the message to the object.



266
267
268
269
270
271
272
273
274
275
# File 'lib/gloo/core/obj.rb', line 266

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

#display_valueObject

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



86
87
88
# File 'lib/gloo/core/obj.rb', line 86

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.



147
148
149
150
151
152
153
154
155
156
# File 'lib/gloo/core/obj.rb', line 147

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.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/gloo/core/obj.rb', line 186

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 self.type_display == Gloo::Objs::Alias.typename
    ln = Gloo::Core::Pn.new( self.value )
    redirect = ln.resolve
    return redirect.find_child( name )
  end
  return nil
end

#msg_unloadObject

Send the object the unload message.



280
281
282
283
284
285
286
287
288
# File 'lib/gloo/core/obj.rb', line 280

def msg_unload
  if self.root?
    $log.error 'Cannot unload the root object.'
    return
  end

  $engine.event_manager.on_unload self
  $engine.heap.unload self
end

#multiline_value?Boolean

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/gloo/core/obj.rb', line 112

def multiline_value?
  return false
end

#pnObject

Get the path and name to this object.



72
73
74
75
76
77
78
79
80
# File 'lib/gloo/core/obj.rb', line 72

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



215
216
217
# File 'lib/gloo/core/obj.rb', line 215

def remove_child( obj )
  @children.delete obj
end

#root?Boolean

Is this the root object?

Returns:

  • (Boolean)


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

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.



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

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

  $log.error "Object #{self.name} cannot receive message #{msg}"
  return false
end

#set_parent(obj) ⇒ Object

Set the parent for the object.



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

def set_parent( obj )
  @parent = obj
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



97
98
99
# File 'lib/gloo/core/obj.rb', line 97

def set_value( new_value )
  self.value = new_value
end

#type_displayObject

The object type, suitable for display.



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

def type_display
  return self.class.typename
end

#value_displayObject

Get the value for display purposes.



104
105
106
# File 'lib/gloo/core/obj.rb', line 104

def value_display
  return self.value.to_s
end

#value_is_array?Boolean

Is the value an Array?

Returns:

  • (Boolean)


126
127
128
# File 'lib/gloo/core/obj.rb', line 126

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

#value_is_blank?Boolean

Is the value a blank string?

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/gloo/core/obj.rb', line 133

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)


119
120
121
# File 'lib/gloo/core/obj.rb', line 119

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