Module: Sentiero::Analytics::Bounded

Included in:
Analyzer, ClickCollector, CustomTagCollector, ErrorCollector, FormCollector, FrustrationCollector, ScrollCollector
Defined in:
lib/sentiero/analytics/bounded.rb

Overview

Cap primitives shared by the compute-on-read collectors/analyzers, which bound memory during a scan. Both leave the collection unchanged once full, so the caller flips its own @capped flag on a false/nil return.

Instance Method Summary collapse

Instance Method Details

#bounded_fetch(store, key, cap) ⇒ Object

Slot cap: the existing entry, a freshly built one (yielded, while under cap), or nil when the store already holds cap distinct keys.



21
22
23
24
25
26
# File 'lib/sentiero/analytics/bounded.rb', line 21

def bounded_fetch(store, key, cap)
  return store[key] if store.key?(key)
  return nil if cap && store.size >= cap

  store[key] = yield
end

#bounded_increment(counts, key, cap, by: 1) ⇒ Object

Counter cap: bump counts (a Hash defaulting to 0), adding a NEW key only while under cap (nil = unbounded). Returns true if counted, false if the cap dropped it.



12
13
14
15
16
17
# File 'lib/sentiero/analytics/bounded.rb', line 12

def bounded_increment(counts, key, cap, by: 1)
  return false unless counts.key?(key) || cap.nil? || counts.size < cap

  counts[key] += by
  true
end