Module: CacheDebugging::ViewSampling

Extended by:
ActiveSupport::Concern
Defined in:
lib/cache_debugging/view_sampling.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.force_sampling?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/cache_debugging/view_sampling.rb', line 7

def self.force_sampling?
  !!self.force_sampling
end

.render_block(view, &block) ⇒ Object

code taken from fragment_for



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cache_debugging/view_sampling.rb', line 20

def self.render_block(view, &block)
  # VIEW TODO: Make #capture usable outside of ERB
  # This dance is needed because Builder can't use capture
  output_buffer = view.output_buffer
  pos = output_buffer.length
  yield
  output_safe = output_buffer.html_safe?
  fragment = output_buffer.slice!(pos..-1)
  if output_safe
    view.output_buffer = output_buffer.class.new(output_buffer)
  end
  fragment
end

.should_sample?(options) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/cache_debugging/view_sampling.rb', line 34

def self.should_sample?(options)
  return false unless view_sampling_enabled?
  return true if force_sampling?

  sample = (options || {}).fetch(:sample) { view_sampling_rate }.to_f
  rand <= sample
end

.view_sampling_enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/cache_debugging/view_sampling.rb', line 15

def self.view_sampling_enabled?
  !!view_sampling_rate
end

.view_sampling_rateObject



11
12
13
# File 'lib/cache_debugging/view_sampling.rb', line 11

def self.view_sampling_rate
  Rails.application.config.cache_debugging.view_sampling
end

Instance Method Details

#cache_with_view_sampling(name = {}, options = nil, &block) ⇒ Object

clear forcing of view sampling if it’s been initiated



47
48
49
50
51
# File 'lib/cache_debugging/view_sampling.rb', line 47

def cache_with_view_sampling(name = {}, options = nil, &block)
  cache_without_view_sampling(name, options, &block).tap do
    CacheDebugging::ViewSampling.force_sampling = false if cache_depth == 0
  end
end