Class: Spektr::Targets::View

Inherits:
Base
  • Object
show all
Defined in:
lib/spektr/targets/view.rb

Constant Summary collapse

TEMPLATE_EXTENSIONS =
/.*\.(erb|rhtml|haml|slim)$/

Instance Attribute Summary collapse

Attributes inherited from Base

#ast, #name, #options, #parent, #path, #processor

Instance Method Summary collapse

Methods inherited from Base

#ast_to_exp, #find, #find_calls, #find_calls_with_block, #find_method, #find_methods, #find_xstr, #node_matches?

Constructor Details

#initialize(path, content) ⇒ View

Returns a new instance of View.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spektr/targets/view.rb', line 7

def initialize(path, content)
  Spektr.logger.debug "loading #{path}"
  @view_path = nil
  @path = path
  if match_data = path.match(%r{views/(.+?)\.})
    @view_path = match_data[1]
  end
  begin
    @ast = Spektr::App.parser.parse(source(content))
  rescue Parser::SyntaxError => e
    @ast = Spektr::App.parser.parse('')
    ::Spektr.logger.error "Parser::SyntaxError when parsing #{@view_path}: #{e.message}"
  end
  @name = @view_path # @ast.children.first.children.last.to_s
end

Instance Attribute Details

#view_pathObject

Returns the value of attribute view_path.



5
6
7
# File 'lib/spektr/targets/view.rb', line 5

def view_path
  @view_path
end

Instance Method Details

#source(content) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spektr/targets/view.rb', line 23

def source(content)
  type = @path.match(TEMPLATE_EXTENSIONS)[1].to_sym
  case type
  when :erb, :rhtml
    Erubi.new(content, trim_mode: '-').src
  when :haml
    Haml::Engine.new(content).precompiled
  when :slim
    erb = Slim::ERBConverter.new.call(content)
    Erubi.new(erb, trim_mode: '-').src
  end
end