Class: Plumbo::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/plumbo/collector.rb

Overview

Subscribes to ActionView/ActionController notifications for the duration of a single request and gathers every project file that took part in rendering. Render events are recorded with the timestamp at which each render began, so #files can return them in true call order (a template before the partials it renders) rather than the inner-to-outer order in which the events fire on completion. Subscriptions are scoped to the calling thread so concurrent requests on a threaded dev server don’t bleed into one another.

Constant Summary collapse

RENDER_EVENTS =
%w[
  render_template.action_view
  render_partial.action_view
  render_layout.action_view
].freeze
CONTROLLER_EVENT =
"process_action.action_controller"

Instance Method Summary collapse

Constructor Details

#initialize(config = Plumbo.config) ⇒ Collector

Returns a new instance of Collector.



24
25
26
27
28
29
# File 'lib/plumbo/collector.rb', line 24

def initialize(config = Plumbo.config)
  @config = config
  @leading = []   # controller + helper, forced to the top of the list
  @renders = []   # [start_time, prefixed_path] for each template/partial
  @root_prefix = File.join(@config.root, "")
end

Instance Method Details

#collect(&block) ⇒ Object

Runs the block with subscriptions active and returns whatever it returns (the downstream Rack response triple). Render and controller events go to separate handlers so neither has to branch on the event name.



34
35
36
37
38
39
# File 'lib/plumbo/collector.rb', line 34

def collect(&block)
  @thread = Thread.current
  with_subscriptions(RENDER_EVENTS, method(:on_render)) do
    with_subscriptions([CONTROLLER_EVENT], method(:on_controller), &block)
  end
end

#filesObject

Project files as [path, depth] pairs in call order: controller and helper first (depth 0), then every template/partial sorted by when its render began, so a template precedes the partials it renders. Each render’s depth is how many other renders enclose it, giving a parent/child nesting. Deduped by path (first occurrence wins) and capped at max_files.



46
47
48
49
# File 'lib/plumbo/collector.rb', line 46

def files
  entries = leading_entries + render_entries
  entries.uniq { |path, _depth| path }.first(@config.max_files)
end