Class: RuboCop::Erb::WhenDecomposer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/erb/when_decomposer.rb

Constant Summary collapse

REGEXP =
/
  \A
  \s*
  when[ \t]
/x.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processed_source, ruby_clip) ⇒ WhenDecomposer

Returns a new instance of WhenDecomposer.

Parameters:



26
27
28
29
30
31
32
# File 'lib/rubocop/erb/when_decomposer.rb', line 26

def initialize(
  processed_source,
  ruby_clip
)
  @processed_source = processed_source
  @ruby_clip = ruby_clip
end

Class Method Details

.call(processed_source, ruby_clip) ⇒ Array<RuboCop::Erb::RubyClip>

Parameters:

Returns:



16
17
18
19
20
21
# File 'lib/rubocop/erb/when_decomposer.rb', line 16

def call(
  processed_source,
  ruby_clip
)
  new(processed_source, ruby_clip).call
end

Instance Method Details

#callArray<RuboCop::Erb::RubyClip>

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/erb/when_decomposer.rb', line 35

def call
  match_data = @ruby_clip.code.match(REGEXP)
  if match_data
    offset = match_data[0].length
    condition = @ruby_clip.code[offset..].sub(/[ \t]then(?:[ \t].*)?/, '')
    parse(
      <<~RUBY
        [
          #{condition}
        ]
      RUBY
    ).children.map do |child|
      RubyClip.new(
        code: child.location.expression.source,
        offset: @ruby_clip.offset + offset + child.location.expression.begin_pos - 4
      )
    end
  else
    [@ruby_clip]
  end
end