[Groonga-commit] ranguba/chupa-text at 271e2f0 [master] Add VirtualContent to use in Data

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Jan 4 19:13:00 JST 2014


Kouhei Sutou	2014-01-04 19:13:00 +0900 (Sat, 04 Jan 2014)

  New Revision: 271e2f047643aea7cd04edd0eeec62bba2f424c9
  https://github.com/ranguba/chupa-text/commit/271e2f047643aea7cd04edd0eeec62bba2f424c9

  Message:
    Add VirtualContent to use in Data

  Added files:
    lib/chupa-text/virtual-content.rb
    test/test-virtual-content.rb
  Modified files:
    lib/chupa-text.rb

  Modified: lib/chupa-text.rb (+1 -0)
===================================================================
--- lib/chupa-text.rb    2014-01-04 19:04:06 +0900 (2e37f56)
+++ lib/chupa-text.rb    2014-01-04 19:13:00 +0900 (95599d1)
@@ -28,4 +28,5 @@ require "chupa-text/mime-type"
 require "chupa-text/mime-type-registry"
 
 require "chupa-text/file-content"
+require "chupa-text/virtual-content"
 require "chupa-text/command"

  Added: lib/chupa-text/virtual-content.rb (+91 -0) 100644
===================================================================
--- /dev/null
+++ lib/chupa-text/virtual-content.rb    2014-01-04 19:13:00 +0900 (0284c98)
@@ -0,0 +1,91 @@
+# 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 "stringio"
+require "tempfile"
+
+module ChupaText
+  class VirtualContent
+    KILO_BYTE = 1024
+    BUFFER_SIZE = 64 * KILO_BYTE
+
+    attr_reader :size
+    def initialize(input, original_path=nil)
+      @file = nil
+      @base_name = compute_base_name(original_path)
+      chunk = input.read(BUFFER_SIZE) || ""
+      if chunk.bytesize != BUFFER_SIZE
+        @path = nil
+        @body = chunk
+        @size =****@body*****
+      else
+        @body = nil
+        @size = chunk.bytesize
+        setup_file do |file|
+          file.write(chunk)
+          while (chunk = input.read(BUFFER_SIZE))
+            @size += chunk.bytesize
+            file.write(chunk)
+          end
+        end
+      end
+    end
+
+    def open(&block)
+      if @body
+        yield(StringIO.new(@body))
+      else
+        File.open(path, "rb", &block)
+      end
+    end
+
+    def body
+      @body ||= open {|file| file.read}
+    end
+
+    def path
+      ensure_setup_file do |file|
+        file.write(@body)
+      end
+      @path
+    end
+
+    private
+    def compute_base_name(original_path)
+      if original_path
+        prefix, suffix = File.basename(original_path).split(/(\.[^.]+\z)/)
+        if suffix
+          [prefix, suffix]
+        else
+          prefix
+        end
+      else
+        "chupa-text-virtual-content"
+      end
+    end
+
+    def ensure_setup_file(&block)
+      setup_file(&block) unless @file
+    end
+
+    def setup_file
+      @file = Tempfile.new(@base_name)
+      @path =****@file*****
+      yield(@file)
+      @file.close
+    end
+  end
+end

  Added: test/test-virtual-content.rb (+103 -0) 100644
===================================================================
--- /dev/null
+++ test/test-virtual-content.rb    2014-01-04 19:13:00 +0900 (de9ba76)
@@ -0,0 +1,103 @@
+# 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
+
+class TestVirtualContent < Test::Unit::TestCase
+  private
+  def input(string)
+    StringIO.new(string)
+  end
+
+  def content(string, original_path=nil)
+    ChupaText::VirtualContent.new(input(string), original_path)
+  end
+
+  sub_test_case("small data") do
+    def setup
+      @body = "Hello"
+    end
+
+    def test_size
+      assert_equal(@body.bytesize, content.size)
+    end
+
+    def test_path
+      assert_equal(@body, File.read(content.path))
+    end
+
+    def test_body
+      assert_equal(@body, content.body)
+    end
+
+    def test_open
+      assert_equal(@body, content.open {|file| file.read})
+    end
+
+    private
+    def content
+      super(@body)
+    end
+  end
+
+  sub_test_case("large data") do
+    def setup
+      @body = "X" * (ChupaText::VirtualContent::BUFFER_SIZE + 1)
+    end
+
+    def test_size
+      assert_equal(@body.bytesize, content.size)
+    end
+
+    def test_path
+      assert_equal(@body, File.read(content.path))
+    end
+
+    def test_body
+      assert_equal(@body, content.body)
+    end
+
+    def test_open
+      assert_equal(@body, content.open {|file| file.read})
+    end
+
+    private
+    def content
+      super(@body)
+    end
+  end
+
+  sub_test_case("original path") do
+    def test_extension
+      assert_equal(".txt", File.extname(path("hello.txt")))
+    end
+
+    def test_extension_only
+      assert_equal(".txt", File.extname(path(".txt")))
+    end
+
+    def test_no_extension
+      assert_equal("", File.extname(path("hello")))
+    end
+
+    def test_nil
+      assert_equal("", File.extname(path(nil)))
+    end
+
+    private
+    def path(original_path)
+      content("", original_path).path
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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