Kouhei Sutou
null+****@clear*****
Fri Jan 3 15:26:45 JST 2014
Kouhei Sutou 2014-01-03 15:26:45 +0900 (Fri, 03 Jan 2014) New Revision: c0dc35a4793897b402629434d2e23859b954f51c https://github.com/ranguba/chupa-text/commit/c0dc35a4793897b402629434d2e23859b954f51c Message: Add configuration Added files: lib/chupa-text/decomposers.rb Copied files: lib/chupa-text/configuration.rb (from lib/chupa-text/decomposer-registry.rb) test/test-decomposers.rb (from test/test-decomposer-registry.rb) Modified files: lib/chupa-text.rb lib/chupa-text/command/chupa-text.rb lib/chupa-text/decomposer-registry.rb test/run-test.rb test/test-decomposer-registry.rb Modified: lib/chupa-text.rb (+2 -0) =================================================================== --- lib/chupa-text.rb 2014-01-03 15:21:11 +0900 (df54d81) +++ lib/chupa-text.rb 2014-01-03 15:26:45 +0900 (45ce079) @@ -18,6 +18,8 @@ require "chupa-text/version" require "chupa-text/extractor" require "chupa-text/decomposer" +require "chupa-text/decomposers" require "chupa-text/formatters" +require "chupa-text/configuration" require "chupa-text/command" Modified: lib/chupa-text/command/chupa-text.rb (+5 -2) =================================================================== --- lib/chupa-text/command/chupa-text.rb 2014-01-03 15:21:11 +0900 (f1ee781) +++ lib/chupa-text/command/chupa-text.rb 2014-01-03 15:26:45 +0900 (d0c40dc) @@ -28,6 +28,7 @@ module ChupaText def initialize @path = nil + @configuration = Configuration.new end def run(*arguments) @@ -75,9 +76,11 @@ module ChupaText end def create_extractor - Decomposer.load + Decomposers.load extractor = Extractor.new - Decomposer.registry.decomposers.each do |decomposer| + decomposers = Decomposers.create(Decomposer.registry, + @configuration.decomposer) + decomposers.each do |decomposer| extractor.add_decomposer(decomposer) end extractor Copied: lib/chupa-text/configuration.rb (+9 -15) 71% =================================================================== --- lib/chupa-text/decomposer-registry.rb 2014-01-03 15:21:11 +0900 (d6b1fed) +++ lib/chupa-text/configuration.rb 2014-01-03 15:26:45 +0900 (7fd1d5e) @@ -15,24 +15,18 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA module ChupaText - class DecomposerRegistry - include Enumerable - + class Configuration + attr_reader :decomposer def initialize - @decomposer_classes = {} - end - - def register(name, decomposer_class) - @decomposer_classes[name] = decomposer_class - end - - def each(&block) - @decomposer_classes.each(&block) + @decomposer = DecomposerConfiguration.new end - def decomposers - collect do |name, decomposer_class| - decomposer_class.new({}) + class DecomposerConfiguration + attr_accessor :names + attr_accessor :options + def initialize + @names = ["*"] + @options = {} end end end Modified: lib/chupa-text/decomposer-registry.rb (+4 -6) =================================================================== --- lib/chupa-text/decomposer-registry.rb 2014-01-03 15:21:11 +0900 (d6b1fed) +++ lib/chupa-text/decomposer-registry.rb 2014-01-03 15:26:45 +0900 (9ef0741) @@ -26,14 +26,12 @@ module ChupaText @decomposer_classes[name] = decomposer_class end - def each(&block) - @decomposer_classes.each(&block) + def find(name) + @decomposer_classes[name] end - def decomposers - collect do |name, decomposer_class| - decomposer_class.new({}) - end + def each(&block) + @decomposer_classes.each(&block) end end end Added: lib/chupa-text/decomposers.rb (+55 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/decomposers.rb 2014-01-03 15:26:45 +0900 (d8c8a65) @@ -0,0 +1,55 @@ +# 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/decomposer-registry" + +module ChupaText + module Decomposers + class << self + def load + $LOAD_PATH.each do |load_path| + next unless File.directory?(load_path) + Dir.chdir(load_path) do + Dir.glob("chupa-text/decomposers/*.rb") do |decomposer_path| + require decomposer_path.gsub(/\.rb\z/, "") + end + end + end + end + + def create(registry, configuration) + enabled_names = resolve_names(registry, configuration.names) + enabled_names.collect do |enabled_name| + decomposer_class = registry.find(enabled_name) + options = configuration.options[name] + decomposer_class.new(options) + end + end + + private + def resolve_names(registry, enabled_names) + resolved_names = [] + enabled_names.each do |enabled_name| + registry.each do |name,| + next unless File.fnmatch(enabled_name, name, File::FNM_EXTGLOB) + resolved_names << name + end + end + resolved_names + end + end + end +end Modified: test/run-test.rb (+1 -1) =================================================================== --- test/run-test.rb 2014-01-03 15:21:11 +0900 (f12bc4f) +++ test/run-test.rb 2014-01-03 15:26:45 +0900 (15c7286) @@ -28,7 +28,7 @@ $LOAD_PATH.unshift(lib_dir.to_s) require "chupa-text" -ChupaText::Decomposer.load +ChupaText::Decomposers.load require_relative "helper" Modified: test/test-decomposer-registry.rb (+0 -5) =================================================================== --- test/test-decomposer-registry.rb 2014-01-03 15:21:11 +0900 (1aefa28) +++ test/test-decomposer-registry.rb 2014-01-03 15:26:45 +0900 (12e8725) @@ -27,9 +27,4 @@ class TestDecomposerRegistry < Test::Unit::TestCase @registry.register("csv", CSVDecomposer) assert_equal([["csv", CSVDecomposer]], @registry.to_a) end - - def test_decomposers - @registry.register("csv", CSVDecomposer) - assert_equal([CSVDecomposer], @registry.decomposers.collect(&:class)) - end end Copied: test/test-decomposers.rb (+18 -9) 65% =================================================================== --- test/test-decomposer-registry.rb 2014-01-03 15:21:11 +0900 (1aefa28) +++ test/test-decomposers.rb 2014-01-03 15:26:45 +0900 (071c88a) @@ -14,22 +14,31 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -class TestDecomposerRegistry < Test::Unit::TestCase +class TestDecomposers < Test::Unit::TestCase class CSVDecomposer < ChupaText::Decomposer end def setup @registry = ChupaText::DecomposerRegistry.new - end - - def test_register - assert_equal([], @registry.to_a) @registry.register("csv", CSVDecomposer) - assert_equal([["csv", CSVDecomposer]], @registry.to_a) + @configuration = ChupaText::Configuration.new end - def test_decomposers - @registry.register("csv", CSVDecomposer) - assert_equal([CSVDecomposer], @registry.decomposers.collect(&:class)) + sub_test_case("create") do + def test_default + decomposers = create + assert_equal([CSVDecomposer], decomposers.collect(&:class)) + end + + def test_no_match + @configuration.decomposer.names = [] + decomposers = create + assert_equal([], decomposers.collect(&:class)) + end + + private + def create + ChupaText::Decomposers.create(@registry, @configuration.decomposer) + end end end -------------- next part -------------- HTML����������������������������... 下載