Class: Simrpc::Schema::ClassDef

Inherits:
Object
  • Object
show all
Defined in:
lib/simrpc/schema.rb

Overview

A class definition, containing data members. Right now we build into this the assumption that the ‘name’ attribute will share the same name as the actual class name which data will be mapped to / from and accessors exist on it corresponding to the names of each of the members

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClassDef

Returns a new instance of ClassDef.



202
203
204
# File 'lib/simrpc/schema.rb', line 202

def initialize
  @members    = []
end

Instance Attribute Details

#inheritsObject

class name array of DataFieldDef base class name



200
201
202
# File 'lib/simrpc/schema.rb', line 200

def inherits
  @inherits
end

#membersObject

class name array of DataFieldDef base class name



200
201
202
# File 'lib/simrpc/schema.rb', line 200

def members
  @members
end

#nameObject

class name array of DataFieldDef base class name



200
201
202
# File 'lib/simrpc/schema.rb', line 200

def name
  @name
end

Instance Method Details

#base_class_def(schema_def) ⇒ Object



206
207
208
# File 'lib/simrpc/schema.rb', line 206

def base_class_def(schema_def)
 return schema_def.classes.find { |cl| cl.name == inherits.to_s } unless inherits.nil? || schema_def.nil?
end

#from_s(str, schema_def = nil, converted_classes = []) ⇒ Object

convert string instance of class represented by this ClassDef into actual class instance. schema_def must be provided if this ClassDef contains any associated class members. The converted_classes recursive helper array is used internally.

Raises:



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/simrpc/schema.rb', line 251

def from_s(str, schema_def = nil, converted_classes = [])
   return nil if str == "I"

   if str[0] == "O" # if we already converted the class, simply return that
      return converted_classes[str[1...5].to_i]
   end

   # construct an instance of the class
   cl = Object.module_eval("::#{@name}", __FILE__, __LINE__)
   obj = cl.new if cl.respond_to? :new
   obj = cl.instance if obj.nil? && cl.respond_to?(:instance)
   raise InvalidSchemaClass.new("cannot create schema class #{@name}") if obj.nil?

   # decode each member
   mpos = 1 # start at 1 to skip the 'I'
   @members.each { |member|
     mlen = str[mpos...mpos+4].to_i
     parsed = str[mpos+4...mpos+4+mlen]
     parsed_o = member.from_s(parsed, schema_def, converted_classes)
     unless parsed_o.nil? && member.ignore_null
        member_method = (member.name + "=").intern
        obj.send(member_method, parsed_o) if obj.respond_to? member_method # invoke member setter
     end
     mpos = mpos+4+mlen
   }

   # decode base object from string
   base_class = base_class_def(schema_def)
   until base_class.nil?
     base_class.members.each { |member|
       mlen = str[mpos...mpos+4].to_i
       parsed = str[mpos+4...mpos+4+mlen]
       parsed_o = member.from_s(parsed, schema_def, converted_classes)
       unless parsed_o.nil? && member.ignore_null
          member_method = (member.name + "=").intern
          obj.send(member_method, parsed_o) if obj.respond_to? member_method # invoke member setter
       end
       mpos = mpos+4+mlen
     }

     base_class = base_class.base_class_def(schema_def)
   end

   return obj
end

#to_s(value, schema_def = nil, converted_classes = []) ⇒ Object

convert value instance of class represented by this ClassDef into a string. schema_def must be provided if this ClassDef contains any associated class members. converted_classes is a recursive helper array used internally



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/simrpc/schema.rb', line 214

def to_s(value, schema_def = nil, converted_classes = [])
   return "O" + ("%04d" % converted_classes.index(value)) if converted_classes.include? value # if we already converted the class, store 'O' + its index
   converted_classes.push value

   # just encode each member w/ length
   str = "I" # NEED to have something here incase the length of the first member is the same as the ascii character for 'O'
   unless value.nil?
     @members.each { |member|
        mval = value.send(member.name.intern) if value.respond_to? member.name.intern
        #mval = value.method(member.name.intern).call # invoke member getter
        mstr = member.to_s(mval, schema_def, converted_classes)
        mlen = "%04d" % mstr.size
        #unless mstr == "" && member.ignore_null
        str += mlen + mstr
        #end
     }

     # encode and append base class
     base_class = base_class_def(schema_def)
     until base_class.nil?
       base_class.members.each { |member|
          mval = value.send(member.name.intern) if value.respond_to? member.name.intern
          mstr = member.to_s(mval, schema_def, converted_classes)
          mlen = "%04d" % mstr.size
          str += mlen + mstr
       }
       base_class = base_class.base_class_def(schema_def)
     end
   end

   return str
end