Class: Cabriolet::Streaming::LazyFile

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/streaming.rb

Overview

Wrapper for lazy file data loading

Instance Method Summary collapse

Constructor Details

#initialize(file, chunk_size) ⇒ LazyFile

Returns a new instance of LazyFile.



127
128
129
130
131
# File 'lib/cabriolet/streaming.rb', line 127

def initialize(file, chunk_size)
  @file = file
  @chunk_size = chunk_size
  @data_loaded = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



170
171
172
# File 'lib/cabriolet/streaming.rb', line 170

def method_missing(method, ...)
  @file.send(method, ...)
end

Instance Method Details

#attributesObject



141
142
143
# File 'lib/cabriolet/streaming.rb', line 141

def attributes
  @file.attributes if @file.respond_to?(:attributes)
end

#dataObject

Load data only when accessed



154
155
156
# File 'lib/cabriolet/streaming.rb', line 154

def data
  @data ||= @file.data
end

#dateObject



145
146
147
# File 'lib/cabriolet/streaming.rb', line 145

def date
  @file.date if @file.respond_to?(:date)
end

#nameObject



133
134
135
# File 'lib/cabriolet/streaming.rb', line 133

def name
  @file.name
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/cabriolet/streaming.rb', line 174

def respond_to_missing?(method, include_private = false)
  @file.respond_to?(method, include_private)
end

#sizeObject



137
138
139
# File 'lib/cabriolet/streaming.rb', line 137

def size
  @file.size
end

#stream_data(chunk_size: @chunk_size) ⇒ Object

Stream data in chunks



159
160
161
162
163
164
165
166
167
168
# File 'lib/cabriolet/streaming.rb', line 159

def stream_data(chunk_size: @chunk_size)
  full_data = data
  offset = 0

  while offset < full_data.bytesize
    chunk = full_data.byteslice(offset, chunk_size)
    yield chunk
    offset += chunk_size
  end
end

#timeObject



149
150
151
# File 'lib/cabriolet/streaming.rb', line 149

def time
  @file.time if @file.respond_to?(:time)
end