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|herb)$/

Instance Attribute Summary collapse

Attributes inherited from Base

#ast, #calls, #interpolated_xstrings, #lvars, #methods, #name, #options, #parent, #parent_modules, #path

Instance Method Summary collapse

Methods inherited from Base

#find_calls, #find_calls_with_block, #find_method, #find_methods, #method_definitions, #public_methods, #visit_call_node, #visit_class_node, #visit_def_node, #visit_instance_variable_write_node, #visit_interpolated_x_string_node, #visit_local_variable_write_node, #visit_module_node

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
# File 'lib/spektr/targets/view.rb', line 7

def initialize(path, content)
  super
  @calls = []
  Spektr.logger.debug "loading #{path}"
  @view_path = nil
  @path = path
  if match_data = path.match(%r{views/(.+?)\.})
    @view_path = match_data[1]
  end
  @ast = Prism.parse(source(content))
  @ast.value.accept(self)
  @name = @view_path
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



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

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