Class: Erbcop::RubyExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/erbcop/ruby_extractor.rb

Overview

Extract Ruby codes from Erb source.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, source:) ⇒ RubyExtractor

Returns a new instance of RubyExtractor.

Parameters:

  • file_path (String, nil)
  • source (String)


26
27
28
29
# File 'lib/erbcop/ruby_extractor.rb', line 26

def initialize(file_path:, source:)
  @file_path = file_path
  @source = source
end

Class Method Details

.call(file_path:, source:) ⇒ Object

Parameters:

  • file_path (String, nil)
  • source (String)


13
14
15
16
17
18
19
20
21
# File 'lib/erbcop/ruby_extractor.rb', line 13

def call(
  file_path:,
  source:
)
  new(
    file_path: file_path,
    source: source
  ).call
end

Instance Method Details

#callArray<Hash>

Returns:

  • (Array<Hash>)


32
33
34
35
36
37
38
39
40
41
# File 'lib/erbcop/ruby_extractor.rb', line 32

def call
  nodes.map do |node|
    snippet = node.children.first
    clipped = ::Templatecop::RubyClipper.new(snippet).call
    {
      code: clipped[:code],
      offset: node.location.begin_pos + clipped[:offset]
    }
  end
end