Added Ogg::Vorbis::Comments#merge!.
authorTilman Sauerbeck <tilman@code-monkey.de>
Tue, 15 Aug 2006 17:03:18 +0000 (19:03 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Wed, 23 Aug 2006 17:33:53 +0000 (19:33 +0200)
ext/comments.c
test/test_main.rb

index a2569866a62766062a292d9719f0233fd75a1185..cdaf576ae7e697571297a7b2a573525a8edf1e02 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <ruby.h>
  */
 
 #include <ruby.h>
+#include <st.h>
 #include <stdbool.h>
 #include <ctype.h>
 #include <assert.h>
 #include <stdbool.h>
 #include <ctype.h>
 #include <assert.h>
@@ -331,6 +332,31 @@ c_has_key (VALUE self, VALUE key)
        return Qfalse;
 }
 
        return Qfalse;
 }
 
+static int
+merge_cb (VALUE key, VALUE value, VALUE self)
+{
+       c_aset (self, key, value);
+
+       return ST_CONTINUE;
+}
+
+/*
+ * call-seq:
+ *  object.merge!(hash) -> object
+ *
+ * Adds the key-value pairs from *hash* to *object*, overwriting existing
+ * values if a key already existed in *object*.
+ */
+static VALUE
+c_merge (VALUE self, VALUE hash)
+{
+       Check_Type (hash, T_HASH);
+
+       rb_hash_foreach (hash, merge_cb, self);
+
+       return self;
+}
+
 /*
  * call-seq:
  *  object <=> other -> -1, 0 or 1
 /*
  * call-seq:
  *  object <=> other -> -1, 0 or 1
@@ -463,6 +489,7 @@ Init_Comments (VALUE mVorbis)
        rb_define_method (c, "empty?", c_get_empty, 0);
        rb_define_method (c, "keys", c_keys, 0);
        rb_define_method (c, "values", c_values, 0);
        rb_define_method (c, "empty?", c_get_empty, 0);
        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_include_module (c, rb_mComparable);
        rb_define_method (c, "<=>", c_compare, 1);
 
        rb_include_module (c, rb_mComparable);
        rb_define_method (c, "<=>", c_compare, 1);
index 21c4edf662f5ec5056088a63fb5363876a860aa7..be81bb87048ca263bf456854eb0b627553db0f70 100644 (file)
@@ -213,4 +213,21 @@ EOF
                        end
                end
        end
                        end
                end
        end
+
+       def test_merge
+               repl = {
+                       "artist" => "Ballista",
+                       "genre" => "Death Metal",
+               }
+
+               Ogg::Vorbis::Tagger.open(OGG_FILE) do |t|
+                       t.comments.merge!(repl)
+
+                       assert_equal(["artist", "album", "date", "genre"],
+                                    t.comments.keys)
+                       assert_equal(["Ballista", "...For Victory", "1994",
+                                     "Death Metal"],
+                                    t.comments.values)
+               end
+       end
 end
 end