Module: RailsExcelReporter::Streaming

Included in:
Base
Defined in:
lib/rails_excel_reporter/streaming.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/rails_excel_reporter/streaming.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#build_progress_info(current, total) ⇒ Object



61
62
63
64
# File 'lib/rails_excel_reporter/streaming.rb', line 61

def build_progress_info(current, total)
  percentage = (current.to_f / total * 100).round 2
  OpenStruct.new current: current, total: total, percentage: percentage
end

#collection_sizeObject



21
22
23
24
25
# File 'lib/rails_excel_reporter/streaming.rb', line 21

def collection_size
  return @collection_size if defined?(@collection_size)

  @collection_size = calculate_collection_size
end

#should_stream?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rails_excel_reporter/streaming.rb', line 17

def should_stream?
  collection_size >= self.class.streaming_threshold
end

#stream_data(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rails_excel_reporter/streaming.rb', line 39

def stream_data(&block)
  return enum_for :stream_data unless block_given?

  if should_stream?
    stream_large_dataset(&block)
  else
    stream_small_dataset(&block)
  end
end

#stream_large_dataset(&block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rails_excel_reporter/streaming.rb', line 66

def stream_large_dataset(&block)
  if @collection.respond_to? :find_each
    stream_with_find_each(&block)
  else
    stream_with_fallback(&block)
  end
end

#with_progress_trackingObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_excel_reporter/streaming.rb', line 49

def with_progress_tracking
  return enum_for :with_progress_tracking unless block_given?

  total, current = collection_size, 0
  stream_data do |item|
    current += 1
    progress = build_progress_info current, total
    @progress_callback&.call progress
    yield item, progress
  end
end