Class: NEAT::NeatOb

Inherits:
Object
  • Object
show all
Extended by:
QueueDing
Includes:
DeepDive
Defined in:
lib/rubyneat/rubyneat.rb

Overview

Basis of all NEAT objects. NeatOb has support for NEAT attributes with special support for hooks and queues.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller = nil, name = nil) ⇒ NeatOb

Initializer for all NEAT objects. Requires that the controller object is specified for all classes with the exception of the Controller itself or the Controller’s NeatSettings.



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rubyneat/rubyneat.rb', line 115

def initialize(controller = nil, name = nil)
  @name = unless name.nil?
            name.to_sym
          else
            NEAT::random_name_generator
          end
  unless controller.nil?
    @controller = controller
  else
    raise NeatException.new "Controller Needed!" unless self.is_a?(Controller) or self.is_a?(Controller::NeatSettings)
    @controller = self unless self.is_a? Controller::NeatSettings
  end
end

Instance Attribute Details

#controllerObject (readonly)

Who’s your daddy?



106
107
108
# File 'lib/rubyneat/rubyneat.rb', line 106

def controller
  @controller
end

#nameObject (readonly)

Designation of this particular object instance



103
104
105
# File 'lib/rubyneat/rubyneat.rb', line 103

def name
  @name
end

Class Method Details

.attr_neat(sym, default: nil, cloneable: nil, hooks: false, queue: false) ⇒ Object

Defaultable attributes of neat attributes.

If hooks: true is given, two hook functions are created: <sym>_add() – add a hook <sym>_set() – set a hook, overwriting all other hooks set or added. <sym>_clear – clear all hooks <sym>_none? – return true if no hooks are defined. <sym>_one? – return true if exactly hook is defined. <sym>_hook() – for passing unnamed parameters to a singular hook. <sym>_np_hook() – for passing unnamed parameters to a singular hook. <sym>_hook_itself() – for getting the proc reference to the hook. <sym>_hooks() – for passing unnamed parameters. <sym>_np_hooks() – for passing a named parameter list.

For *_hook(), the function returns the single result. For *_hooks(), the hook function return an array of results from all the actual registered hooks called.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rubyneat/rubyneat.rb', line 153

def attr_neat(sym,
              default: nil,
              cloneable: nil,
              hooks: false,
              queue: false)
  svar = "@#{sym}"

  # Guess what clonable should be.
  # This is meant to cover "90%" of the cases.
  cloneable = case
                when default.nil?
                  false
                when default.kind_of?(Numeric)
                  false
                else
                  true
              end if cloneable.nil?

  # Sanity checks
  raise NeatException("Both hooks and queue cannot both be set for #{sym}.") if hooks and queue
  raise NeatException("Defaults cannot be defined for hooks and queues for #{sym}.") if (hooks or queue) and not default.nil?

  if hooks
    default = []
    cloneable = true
    hook_setup sym
  end

  if queue
    default = QDing.new
    cloneable = true
    queue_setup sym
  end

  define_method("#{sym}=") do |v|
    instance_variable_set(svar, v)
  end unless hooks or queue

  # TODO: Enhance this getter method for performance.
  define_method(sym) do
    instance_variable_set(svar,
                          instance_variable_get(svar) ||
                              ((cloneable) ? default.clone
                                           : default))
  end
end

.logObject



109
# File 'lib/rubyneat/rubyneat.rb', line 109

def self.log ; $log; end

Instance Method Details

#logObject



108
# File 'lib/rubyneat/rubyneat.rb', line 108

def log ; $log ; end

#to_sObject



129
130
131
# File 'lib/rubyneat/rubyneat.rb', line 129

def to_s
  "%s<%s>" % [self.class, self.name]
end