Class: ActionviewPrecompiler::Precompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/actionview_precompiler/precompiler.rb

Defined Under Namespace

Classes: Template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_dirs) ⇒ Precompiler

Returns a new instance of Precompiler.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/actionview_precompiler/precompiler.rb', line 26

def initialize(view_dirs)
  @templates =
    view_dirs.flat_map do |view_dir|
      Dir["**/*", base: view_dir].map do |file|
        fullpath = File.expand_path(file, view_dir)
        next if File.directory?(fullpath)

        Template.new(fullpath, file)
      end.compact
    end

  determine_locals
end

Instance Attribute Details

#templatesObject (readonly)

Returns the value of attribute templates.



24
25
26
# File 'lib/actionview_precompiler/precompiler.rb', line 24

def templates
  @templates
end

Instance Method Details

#determine_localsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/actionview_precompiler/precompiler.rb', line 75

def determine_locals
  @locals_sets = {}

  @templates.each do |template|
    parser = TemplateParser.new(template.fullpath)
    parser.render_calls.each do |render_call|
      virtual_path = render_call.virtual_path
      unless virtual_path.include?("/")
        # Not necessarily true, since the perfix is based on the current
        # controller, but is a safe bet most of the time.
        virtual_path = "#{template.prefix}/#{virtual_path}"
      end
      @locals_sets[virtual_path] ||= []
      @locals_sets[virtual_path] << render_call.locals_keys.map(&:to_s).sort
    end
  end

  @locals_sets.each_value(&:uniq!)
end

#each_lookup_argsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/actionview_precompiler/precompiler.rb', line 40

def each_lookup_args
  return enum_for(__method__) unless block_given?

  each_template_render do |template, locals|
    details = {
      locale: Array(template.details[:locale]),
      variants: Array(template.details[:variant]),
      formats: Array(template.details[:format]),
      handlers: Array(template.details[:handler])
    }

    yield [template.action, template.prefix, template.partial?, locals, details]
  end

  nil
end

#each_template_renderObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/actionview_precompiler/precompiler.rb', line 57

def each_template_render
  return enum_for(__method__) unless block_given?

  @templates.each do |template|
    locals_set = @locals_sets[template.virtual_path]
    if locals_set
      locals_set.each do |locals|
        yield template, locals
      end
    elsif !template.partial?
      # For now, guess that non-partials we haven't seen take no locals
      yield template, []
    else
      # Locals unknown
    end
  end
end