Made Ogg::Vorbis::Comments usable standalone, too.
[ruby-vorbistagger.git] / ext / comments.c
index cdaf576ae7e697571297a7b2a573525a8edf1e02..fb3ae82f653d727d751cc9975626caabfb1b5f8c 100644 (file)
@@ -35,8 +35,8 @@ comments_init (VALUE self, vcedit_state *state)
 
        vc = vcedit_comments (state);
 
-       items = rb_ary_new2 (vc->comments);
-       rb_iv_set (self, "items", items);
+       items = rb_iv_get (self, "items");
+       rb_ary_clear (items);
 
        for (i = 0; i < vc->comments; i++) {
                VALUE k, v, pair;
@@ -79,6 +79,19 @@ comments_sync (VALUE self, vcedit_state *state)
                                        StringValuePtr (pair->ptr[1]));
        }
 }
+/*
+ * call-seq:
+ *  Ogg::Vorbis::Comments.new -> object
+ *
+ * Creates an Ogg::Vorbis::Comments object.
+ */
+static VALUE
+c_init (VALUE self)
+{
+       rb_iv_set (self, "items", rb_ary_new ());
+
+       return self;
+}
 
 /*
  * call-seq:
@@ -357,6 +370,20 @@ c_merge (VALUE self, VALUE hash)
        return self;
 }
 
+/*
+ * call-seq:
+ *  object.shift(hash) -> array or nil
+ *
+ * Removes the first key-value pair from *object* and returns it
+ * as the two-item array [key, value].
+ * If *object* is empty, +nil+ is returned.
+ */
+static VALUE
+c_shift (VALUE self)
+{
+       return rb_ary_shift (rb_iv_get (self, "items"));
+}
+
 /*
  * call-seq:
  *  object <=> other -> -1, 0 or 1
@@ -479,6 +506,7 @@ Init_Comments (VALUE mVorbis)
 
        c = rb_define_class_under (mVorbis, "Comments", rb_cObject);
 
+       rb_define_method (c, "initialize", c_init, 0);
        rb_define_method (c, "inspect", c_inspect, 0);
        rb_define_method (c, "clear", c_clear, 0);
        rb_define_method (c, "delete", c_delete, 1);
@@ -490,6 +518,7 @@ Init_Comments (VALUE mVorbis)
        rb_define_method (c, "keys", c_keys, 0);
        rb_define_method (c, "values", c_values, 0);
        rb_define_method (c, "merge!", c_merge, 1);
+       rb_define_method (c, "shift", c_shift, 0);
 
        rb_include_module (c, rb_mComparable);
        rb_define_method (c, "<=>", c_compare, 1);