| Class | Object |
| In: |
lib/eet.rb
|
Serializes the receiver to EET format.
/*
* call-seq:
* object.to_eet -> string
*
* Serializes the receiver to EET format.
*/
static VALUE
c_to_eet (VALUE self)
{
VALUE props, name, stream, chunk, args[2];
#ifndef HAVE_RB_HASH_FOREACH
struct RArray *keys;
long i;
#endif
props = rb_funcall (self, id_to_eet_properties, 0);
if (rb_obj_is_kind_of (props, rb_cHash) == Qfalse ||
!RHASH (props)->tbl->num_entries)
rb_raise (ePropError, "invalid EET properties");
name = rb_funcall (self, id_to_eet_name, 0);
StringValue (name);
if (!RSTRING (name)->len ||
rb_funcall (name, id_include, 1, INT2FIX (0)))
rb_raise (eNameError, "invalid EET name");
stream = rb_class_new_instance (0, NULL, cStream);
#ifdef HAVE_RB_HASH_FOREACH
rb_hash_foreach (props, for_each_prop, stream);
#else
keys = RARRAY (rb_funcall (props, id_keys, 0));
for (i = 0; i < keys->len; i++)
for_each_prop (keys->ptr[i],
rb_hash_aref (props, keys->ptr[i]),
stream);
#endif
args[0] = name;
args[1] = rb_ary_to_s (stream);
rb_ary_clear (stream); /* give the GC a hand... */
chunk = rb_class_new_instance (2, args, cChunk);
return rb_funcall (chunk, id_to_s, 0);
}
Returns the tag that‘s stored with the data for object. If your class doesn‘t override this method, the class name will be used.
# File lib/eet.rb, line 38
38: def to_eet_name
39: self.class.name
40: end
Returns a hash that contains the properties that are stored for object. If your class doesn‘t override this method, all instance variables of object will be stored.
# File lib/eet.rb, line 49
49: def to_eet_properties
50: instance_variables.inject({}) do |h, var|
51: h[var[1..-1]] = [instance_variable_get(var)]
52: h
53: end
54: end