Class: Xlsxtream::IO::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxtream/io/hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Hash

Returns a new instance of Hash.



4
5
6
7
8
# File 'lib/xlsxtream/io/hash.rb', line 4

def initialize(stream)
  @stream = stream
  @hash = {}
  @path = nil
end

Instance Method Details

#<<(data) ⇒ Object



10
11
12
# File 'lib/xlsxtream/io/hash.rb', line 10

def <<(data)
  @stream << data
end

#[](path) ⇒ Object



34
35
36
37
38
# File 'lib/xlsxtream/io/hash.rb', line 34

def [](path)
  fetch(path)
rescue KeyError
  nil
end

#add_file(path) ⇒ Object



14
15
16
17
18
# File 'lib/xlsxtream/io/hash.rb', line 14

def add_file(path)
  close
  @path = path
  @hash[@path] = [@stream.tell]
end

#closeObject



20
21
22
# File 'lib/xlsxtream/io/hash.rb', line 20

def close
  @hash[@path] << @stream.tell if @path
end

#fetch(path) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/xlsxtream/io/hash.rb', line 24

def fetch(path)
  old = @stream.tell
  from, to = @hash.fetch(path)
  size = to - from
  @stream.seek(from)
  data = @stream.read(size)
  @stream.seek(old)
  data
end

#to_hObject



40
41
42
# File 'lib/xlsxtream/io/hash.rb', line 40

def to_h
  ::Hash[@hash.keys.map {|path| [path, fetch(path)] }]
end