class Temple::Filters::StaticAnalyzer

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

Constants

DYNAMIC_TOKENS
STATIC_KEYWORDS
STATIC_OPERATORS
STATIC_TOKENS

Public Class Methods

static?(code) click to toggle source
# File lib/temple/filters/static_analyzer.rb, line 34
def self.static?(code)
  return false if code.nil? || code.strip.empty?
  return false if SyntaxChecker.syntax_error?(code)

  Ripper.lex(code).each do |(_, col), token, str|
    case token
    when *STATIC_TOKENS
      # noop
    when :on_kw
      return false unless STATIC_KEYWORDS.include?(str)
    when :on_op
      return false unless STATIC_OPERATORS.include?(str)
    when *DYNAMIC_TOKENS
      return false
    else
      return false
    end
  end
  true
end

Public Instance Methods

call(ast) click to toggle source

Do nothing if ripper is unavailable

# File lib/temple/filters/static_analyzer.rb, line 81
def call(ast)
  ast
end
on_dynamic(code) click to toggle source
# File lib/temple/filters/static_analyzer.rb, line 55
def on_dynamic(code)
  if StaticAnalyzer.static?(code)
    [:static, eval(code).to_s]
  else
    [:dynamic, code]
  end
end