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.



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

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

Instance Method Details

#<<(data) ⇒ Object



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

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

#[](path) ⇒ Object



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

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

#add_file(path) ⇒ Object



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

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

#closeObject



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

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

#fetch(path) ⇒ Object



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

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



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

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