Class: Temple::Filters::StaticAnalyzer

Inherits:
Temple::Filter show all
Defined in:
lib/temple/filters/static_analyzer.rb

Overview

Convert [:dynamic, code] to [:static, text] if code is static Ruby expression.

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods included from Mixins::Options

included, #initialize

Methods included from Mixins::ControlFlowDispatcher

#on_block, #on_case, #on_cond, #on_if

Methods included from Mixins::EscapeDispatcher

#on_escape

Methods included from Mixins::CoreDispatcher

#on_capture, #on_multi

Methods included from Mixins::CompiledDispatcher

#compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Instance Method Details

#call(exp) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/temple/filters/static_analyzer.rb', line 6

def call(exp)
  # Optimize only when Ripper is available.
  if ::Temple::StaticAnalyzer.available?
    super
  else
    exp
  end
end

#on_dynamic(code) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/temple/filters/static_analyzer.rb', line 15

def on_dynamic(code)
  if ::Temple::StaticAnalyzer.static?(code)
    exp = [:static, eval(code).to_s]

    newlines = code.count("\n")
    if newlines == 0
      exp
    else
      [:multi, exp, *newlines.times.map { [:newline] }]
    end
  else
    [:dynamic, code]
  end
end