class ChupaText::VirtualContent
Constants
- INLINE_MAX_SIZE
Attributes
Public Class Methods
Source
# File lib/chupa-text/virtual-content.rb, line 26 def initialize(input, original_path=nil) if original_path.is_a?(String) if original_path.empty? original_path = nil else original_path = Pathname.new(original_path) end end @original_path = original_path body = input.read(INLINE_MAX_SIZE + 1) || "" if body.bytesize <= INLINE_MAX_SIZE @body = body @size = @body.bytesize @file = nil @path = nil else @body = nil setup_file do |file| file.write(body) @size = body.bytesize @size += IO.copy_stream(input, file) end end end
Public Instance Methods
Source
# File lib/chupa-text/virtual-content.rb, line 67 def body if @body @body else open do |file| file.read end end end
Source
# File lib/chupa-text/virtual-content.rb, line 51 def open(&block) if @body yield(StringIO.new(@body)) else File.open(path, "rb", &block) end end
Source
# File lib/chupa-text/virtual-content.rb, line 87 def path if @path.nil? setup_file do |file| file.write(@body) end end @path end
Source
# File lib/chupa-text/virtual-content.rb, line 77 def peek_body(size) if @body @body[0, size] else open do |file| file.read(size) end end end
Source
# File lib/chupa-text/virtual-content.rb, line 59 def release @body = nil if @file @file.delete @file = nil end end
Private Instance Methods
Source
# File lib/chupa-text/virtual-content.rb, line 97 def compute_tempfile_basename if @original_path prefix, suffix = @original_path.basename.to_s.split(/(\.[^.]+\z)/) prefix = prefix[0, 20] if suffix [prefix, suffix] else prefix end else "chupa-text-virtual-content" end end
Source
# File lib/chupa-text/virtual-content.rb, line 111 def setup_file basename = compute_tempfile_basename @file = Tempfile.new(basename) @file.binmode @path = @file.path yield(@file) @file.close end