Class: Xlsxtream::IO::Hash
- Inherits:
-
Object
- Object
- Xlsxtream::IO::Hash
- Defined in:
- lib/xlsxtream/io/hash.rb
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #[](path) ⇒ Object
- #add_file(path) ⇒ Object
- #close ⇒ Object
- #fetch(path) ⇒ Object
-
#initialize(stream) ⇒ Hash
constructor
A new instance of Hash.
- #to_h ⇒ Object
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 |
#close ⇒ Object
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_h ⇒ Object
40 41 42 |
# File 'lib/xlsxtream/io/hash.rb', line 40 def to_h ::Hash[@hash.keys.map {|path| [path, fetch(path)] }] end |