[Groonga-commit] groonga/gcs [master] Add IndexField#clearAllOptions()

Back to archive index

YUKI Hiroshi null+****@clear*****
Wed Dec 5 16:36:00 JST 2012


YUKI Hiroshi	2012-12-05 16:36:00 +0900 (Wed, 05 Dec 2012)

  New Revision: 51c843dc5966a2bb2e36ed2395075df5b4fe41c3
  https://github.com/groonga/gcs/commit/51c843dc5966a2bb2e36ed2395075df5b4fe41c3

  Log:
    Add IndexField#clearAllOptions()

  Modified files:
    lib/database/index-field.js
    test/database-index-field.test.js

  Modified: lib/database/index-field.js (+16 -3)
===================================================================
--- lib/database/index-field.js    2012-12-05 16:02:43 +0900 (ddc50ec)
+++ lib/database/index-field.js    2012-12-05 16:36:00 +0900 (caf3a64)
@@ -320,19 +320,32 @@ IndexField.prototype = {
     }
     return optionValues;
   },
+  clearAllOptions: function() {
+    var options = this.getAllOptions();
+    Object.keys(options).forEach(function(option) {
+      this.setOption(option, undefined);
+    });
+  },
   saveOptions: function() {
     if (!this._pendingOptions) return;
     var keys = Object.keys(this._pendingOptions);
+    var storedKeys = [];
     keys.forEach(function(key) {
-      this.domain.setConfiguration(key, this._pendingOptions[key]);
+      var value = this._pendingOptions[key];
+      if (typeof value == 'undefined') {
+        this.domain.deleteConfiguration(key);
+      } else {
+        this.domain.setConfiguration(key, value);
+        storedKeys.push(key);
+      }
     }, this);
     var existingKeys = this.domain.getConfiguration(this.getOptionKey('optionKeys'));
     if (existingKeys) {
       existingKeys.split(',').forEach(function(key) {
-        if (keys.indexOf(key) < 0) keys.push(key);
+        if (keys.indexOf(key) < 0) storedKeys.push(key);
       });
     }
-    this.domain.setConfiguration(this.getOptionKey('optionKeys'), keys.join(','));
+    this.domain.setConfiguration(this.getOptionKey('optionKeys'), storedKeys.sort().join(','));
     delete this._pendingOptions;
     return this;
   },

  Modified: test/database-index-field.test.js (+13 -0)
===================================================================
--- test/database-index-field.test.js    2012-12-05 16:02:43 +0900 (1f2110d)
+++ test/database-index-field.test.js    2012-12-05 16:36:00 +0900 (f8b9b47)
@@ -515,6 +515,19 @@ suite('database', function() {
         actual = field.getAllOptions();
         assert.deepEqual(actual, expected);
       });
+
+      test('clearAllOptions', function() {
+        field.setOption('Option1', 'false');
+        field.setOption('Option2', '1');
+        field.clearAllOptions();
+        assert.deepEqual(field.getAllOptions(), {});
+
+        field.setOption('Option1', 'false');
+        field.setOption('Option2', '1');
+        field.clearAllOptions();
+        field.saveOptions();
+        assert.deepEqual(field.getAllOptions(), {});
+      });
     });
 
     suite('database modifications', function() {
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index