Class: ChupaText::VirtualContent

Inherits:
Object
  • Object
show all
Defined in:
lib/chupa-text/virtual-content.rb

Constant Summary collapse

KILO_BYTE =
1024
BUFFER_SIZE =
64 * KILO_BYTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, original_path = nil) ⇒ VirtualContent

Returns a new instance of VirtualContent.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chupa-text/virtual-content.rb', line 26

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.bytesize
  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

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



25
26
27
# File 'lib/chupa-text/virtual-content.rb', line 25

def size
  @size
end

Instance Method Details

#bodyObject



55
56
57
# File 'lib/chupa-text/virtual-content.rb', line 55

def body
  @body ||= open {|file| file.read}
end

#open(&block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/chupa-text/virtual-content.rb', line 47

def open(&block)
  if @body
    yield(StringIO.new(@body))
  else
    File.open(path, "rb", &block)
  end
end

#pathObject



59
60
61
62
63
64
# File 'lib/chupa-text/virtual-content.rb', line 59

def path
  ensure_setup_file do |file|
    file.write(@body)
  end
  @path
end