Kouhei Sutou
null+****@clear*****
Fri Jan 3 15:48:21 JST 2014
Kouhei Sutou 2014-01-03 15:48:21 +0900 (Fri, 03 Jan 2014) New Revision: fcf450c0659f357d54ea5adc3114987d5d59d8e9 https://github.com/ranguba/chupa-text/commit/fcf450c0659f357d54ea5adc3114987d5d59d8e9 Message: chupa-text: add --configuration option Added files: chupa-text.conf lib/chupa-text/configuration-loader.rb test/fixture/command/chupa-text/hello.txt.gz test/fixture/command/chupa-text/no-decomposer.conf Modified files: lib/chupa-text.rb lib/chupa-text/command/chupa-text.rb test/command/test-chupa-text.rb Added: chupa-text.conf (+3 -0) 100644 =================================================================== --- /dev/null +++ chupa-text.conf 2014-01-03 15:48:21 +0900 (59ea55e) @@ -0,0 +1,3 @@ +# -*- ruby -*- + +decomposer.names = ["*"] Modified: lib/chupa-text.rb (+1 -0) =================================================================== --- lib/chupa-text.rb 2014-01-03 15:37:12 +0900 (45ce079) +++ lib/chupa-text.rb 2014-01-03 15:48:21 +0900 (71bf0f8) @@ -21,5 +21,6 @@ require "chupa-text/decomposer" require "chupa-text/decomposers" require "chupa-text/formatters" require "chupa-text/configuration" +require "chupa-text/configuration-loader" require "chupa-text/command" Modified: lib/chupa-text/command/chupa-text.rb (+5 -0) =================================================================== --- lib/chupa-text/command/chupa-text.rb 2014-01-03 15:37:12 +0900 (d0c40dc) +++ lib/chupa-text/command/chupa-text.rb 2014-01-03 15:48:21 +0900 (456e533) @@ -72,6 +72,11 @@ module ChupaText parser = OptionParser.new parser.banner += " [FILE]" parser.version = VERSION + parser.on("--configuration=FILE", + "Read configuration from FILE.") do |path| + loader = ConfigurationLoader.new(@configuration) + loader.load(path) + end parser end Added: lib/chupa-text/configuration-loader.rb (+71 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/configuration-loader.rb 2014-01-03 15:48:21 +0900 (e4e0a64) @@ -0,0 +1,71 @@ +# Copyright (C) 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 +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# 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 "chupa-text/configuration" + +module ChupaText + class ConfigurationLoader + def initialize(configuration=nil) + @configuration = configuration || Configuration.new + end + + def load(path) + File.open(path) do |file| + instance_eval(file.read, path, 1) + end + end + + def decomposer + DecomposerConfigurationLoader.new(@configuration.decomposer) + end + + class DecomposerConfigurationLoader + def initialize(configuration) + @configuration = configuration + end + + def names + @configuration.names + end + + def names=(names) + @configuration.names = names + end + + def [](name) + @configuration.options[name] + end + + def []=(name, options) + @configuration.options[name] = options + end + + def method_missing(name, *arguments) + return super if block_given? + + case arguments.size + when 0 + self[name.to_s] + when 1 + value = arguments.first + self[name.to_s] = value + else + super + end + end + end + end +end Modified: test/command/test-chupa-text.rb (+17 -0) =================================================================== --- test/command/test-chupa-text.rb 2014-01-03 15:37:12 +0900 (91e261d) +++ test/command/test-chupa-text.rb 2014-01-03 15:48:21 +0900 (91d86b9) @@ -97,4 +97,21 @@ class TestCommandChupaText < Test::Unit::TestCase end end end + + sub_test_case("configuration") do + def test_no_decomposer + conf = fixture_path("no-decomposer.conf") + gz = fixture_path("hello.txt.gz") + assert_equal([ + true, + { + "path" => gz.to_s, + "size" => gz.stat.size, + "texts" => [], + }, + ], + run_command("--configuration", conf.to_s, + gz.to_s)) + end + end end Added: test/fixture/command/chupa-text/hello.txt.gz (+0 -0) 100644 =================================================================== (Binary files differ) Added: test/fixture/command/chupa-text/no-decomposer.conf (+3 -0) 100644 =================================================================== --- /dev/null +++ test/fixture/command/chupa-text/no-decomposer.conf 2014-01-03 15:48:21 +0900 (a8fb84e) @@ -0,0 +1,3 @@ +# -*- ruby -*- + +decomposer.names = [] -------------- next part -------------- HTML����������������������������...Download