Kouhei Sutou
null+****@clear*****
Wed Jan 13 17:28:52 JST 2016
Kouhei Sutou 2013-10-14 20:48:28 +0900 (Mon, 14 Oct 2013) New Revision: 1ed7fe989c26ef705c1793d5cccb392c3b86e2e7 https://github.com/groonga/groonga-command/commit/1ed7fe989c26ef705c1793d5cccb392c3b86e2e7 Message: load: add values accessor Modified files: groonga-command.gemspec lib/groonga/command/load.rb test/command/test-load.rb Modified: groonga-command.gemspec (+2 -0) =================================================================== --- groonga-command.gemspec 2013-09-30 16:46:57 +0900 (83fbe82) +++ groonga-command.gemspec 2013-10-14 20:48:28 +0900 (167baf1) @@ -52,6 +52,8 @@ Gem::Specification.new do |spec| spec.licenses = ["LGPLv2.1+"] spec.require_paths = ["lib"] + spec.add_runtime_dependency("json") + spec.add_development_dependency("test-unit") spec.add_development_dependency("test-unit-notify") spec.add_development_dependency("rake") Modified: lib/groonga/command/load.rb (+14 -1) =================================================================== --- lib/groonga/command/load.rb 2013-09-30 16:46:57 +0900 (347e3a4) +++ lib/groonga/command/load.rb 2013-10-14 20:48:28 +0900 (fc17401) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -16,6 +16,8 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +require "json" + require "groonga/command/base" module Groonga @@ -36,17 +38,28 @@ module Groonga end end + attr_writer :values attr_writer :columns def initialize(*argumetns) super + @values = nil @columns = nil end + def values + @values ||= parse_values(self[:values]) + end + def columns @columns ||= parse_columns(self[:columns]) end private + def parse_values(values) + return values if values.nil? + JSON.parse(values) + end + def parse_columns(columns) return columns if columns.nil? columns.split(/\s*,\s*/) Modified: test/command/test-load.rb (+35 -0) =================================================================== --- test/command/test-load.rb 2013-09-30 16:46:57 +0900 (8094ca8) +++ test/command/test-load.rb 2013-10-14 20:48:28 +0900 (8971dc9) @@ -53,4 +53,39 @@ class LoadCommandTest < Test::Unit::TestCase command.arguments) end end + + class ValuesTest < self + def test_nil + command = load_command + assert_nil(command.values) + end + + def test_empty + command = load_command(:values => "[]") + assert_equal([], command.values) + end + + def test_array + command = load_command(:values => "[[\"Alice\"]]") + assert_equal([["Alice"]], command.values) + end + + def test_object_literal + command = load_command(:values => "[{\"key\": \"Alice\"}]") + assert_equal([{"key" => "Alice"}], command.values) + end + + def test_writer + command = load_command(:values => "[{\"key\": \"Alice\"}]") + command.values = [["Alice"]] + assert_equal({ + :reader => [["Alice"]], + :array_reference => "[{\"key\": \"Alice\"}]", + }, + { + :reader => command.values, + :array_reference => command[:values], + }) + end + end end -------------- next part -------------- HTML����������������������������...Télécharger