Kouhei Sutou
null+****@clear*****
Wed Jan 1 22:46:59 JST 2014
Kouhei Sutou 2014-01-01 22:46:59 +0900 (Wed, 01 Jan 2014) New Revision: ca9d01894591f2f4f04125a13f2733a5fdf4c983 https://github.com/ranguba/chupa-text/commit/ca9d01894591f2f4f04125a13f2733a5fdf4c983 Message: Add ContentTypeRegistory Added files: lib/chupa-text/content-type.rb Copied files: lib/chupa-text/content-type-registory.rb (from test/test-data.rb) Modified files: lib/chupa-text/data.rb test/test-data.rb Copied: lib/chupa-text/content-type-registory.rb (+13 -33) 52% =================================================================== --- test/test-data.rb 2014-01-01 21:39:35 +0900 (9aa09c8) +++ lib/chupa-text/content-type-registory.rb 2014-01-01 22:46:59 +0900 (0109106) @@ -14,48 +14,28 @@ # 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 TestData < Test::Unit::TestCase - def setup - @data = ChupaText::Data.new - end - - sub_test_case("content-type") do - sub_test_case("guess") do - private - def guess(path) - @data.path = path - @data.content_type - end - - sub_test_case("extension") do - def test_txt - assert_equal("text/plain", guess("README.txt")) - end - end - end - end - - sub_test_case("extension") do - def test_no_path - assert_nil(extension(nil)) +module ChupaText + class ContentTypeRegistory + def initialize + @from_extension_map = {} end - def test_lower_case - assert_equal(".md", extension("README.md")) + def register(extension, content_type) + @from_extension_map[normalize_extension(extension)] = content_type end - def test_upper_case - assert_equal(".md", extension("README.MD")) + def find(extension) + @from_extension_map[normalize_extension(extension)] end - def test_mixed_case - assert_equal(".md", extension("README.mD")) + def clear + @from_extension_map.clear end private - def extension(path) - @data.path = path - @data.extension + def normalize_extension(extension) + return nil if extension + extension.to_s.downcase.gsub(/\A\./, "") end end end Added: lib/chupa-text/content-type.rb (+27 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/content-type.rb 2014-01-01 22:46:59 +0900 (23dbb57) @@ -0,0 +1,27 @@ +# 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/content-type-registory" + +module ChupaText + module ContentType + class << self + def registory + @@registory ||= ContentTypeRegistory.new + end + end + end +end Modified: lib/chupa-text/data.rb (+3 -4) =================================================================== --- lib/chupa-text/data.rb 2014-01-01 21:39:35 +0900 (80a0b88) +++ lib/chupa-text/data.rb 2014-01-01 22:46:59 +0900 (ccb13d0) @@ -16,6 +16,8 @@ require "pathname" +require "chupa-text/content-type" + module ChupaText class Data attr_writer :body @@ -80,11 +82,8 @@ module ChupaText guess_content_type_from_body end - EXTENSION_TO_CONTENT_TYPE_MAP = { - ".txt" => "text/plain", - } def guess_content_type_from_path - EXTENSION_TO_CONTENT_TYPE_MAP[extension] + ContentType.registory.find(extension) end def guess_content_type_from_body Modified: test/test-data.rb (+2 -0) =================================================================== --- test/test-data.rb 2014-01-01 21:39:35 +0900 (9aa09c8) +++ test/test-data.rb 2014-01-01 22:46:59 +0900 (9a5978d) @@ -17,6 +17,7 @@ class TestData < Test::Unit::TestCase def setup @data = ChupaText::Data.new + ChupaText::ContentType.registory.clear end sub_test_case("content-type") do @@ -29,6 +30,7 @@ class TestData < Test::Unit::TestCase sub_test_case("extension") do def test_txt + ChupaText::ContentType.registory.register("txt", "text/plain") assert_equal("text/plain", guess("README.txt")) end end -------------- next part -------------- HTML����������������������������...Download