Sha256: 7cfba3de7663a1166f471df9f7a2b75667ace1d0af696bf81bf53a687c6b3afb

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require "actionview_precompiler/template_scanner"
require "actionview_precompiler/controller_scanner"
require "actionview_precompiler/helper_scanner"
require "actionview_precompiler/template_loader"

module ActionviewPrecompiler
  class Precompiler
    def initialize(verbose: false)
      @scanners = []
      @loader = TemplateLoader.new
      @verbose = verbose
      @static_templates = []
      @template_renders = nil
    end

    def scan_view_dir(view_dir)
      @scanners << TemplateScanner.new(view_dir)
    end

    def scan_controller_dir(controller_dir)
      @scanners << ControllerScanner.new(controller_dir)
    end

    def scan_helper_dir(controller_dir)
      @scanners << HelperScanner.new(controller_dir)
    end

    def add_template(virtual_path, locals = [])
      locals = locals.map(&:to_s).sort
      @static_templates << [virtual_path, locals]
    end

    def run
      count = 0
      template_renders.each do |virtual_path, locals|
        debug "precompiling: #{virtual_path}"

        templates = @loader.load_template(virtual_path, locals)
        count += templates.count

        debug "  No templates found at #{virtual_path}" if templates.empty?
      end

      debug "Precompiled #{count} Templates"
    end

    def template_renders
      return @template_renders if @template_renders

      template_renders = []

      @scanners.each do |scanner|
        template_renders.concat scanner.template_renders
      end

      template_renders.concat @static_templates

      template_renders.uniq!

      @template_renders = template_renders
    end

    private

    def debug(msg)
      puts msg if @verbose
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
actionview_precompiler-0.4.0 lib/actionview_precompiler/precompiler.rb
actionview_precompiler-0.3.0 lib/actionview_precompiler/precompiler.rb