Module: Metz::SlimRubyExtractor

Extended by:
TemplateRubyExtractor
Defined in:
lib/metz/slim_ruby_extractor.rb

Overview

Ruby extractor for Slim view templates. Mirrors Metz::ErbRubyExtractor: registered with RuboCop::Runner.ruby_extractors so view-aware Metz cops (currently Metz/ViewsDeepNavigation) can run against Slim content.

Recognises four Slim constructs that carry Ruby:

* `= ruby_expr`   - output script (escaped)
* `== ruby_expr`  - output script (raw / unescaped)
* `- ruby_stmt`   - silent script
* `ruby:` filter  - indented block of plain Ruby

Each tag's body is handed to RuboCop as its own ProcessedSource whose offset points at the body's column in the original template. Tags whose body is not valid Ruby on its own are silently skipped (we never want to surface Lint/Syntax from a partial Slim fragment).

Constant Summary collapse

EXPR_LINE =
/\A(?<lead>[ \t]*(?:[a-zA-Z][\w-]*|[.#][^\s=]*)?)==?\s(?<body>.*)$/
STMT_LINE =
/\A(?<lead>[ \t]*)-\s(?<body>.*)$/
FILTER_LINE =
/\A(?<indent>[ \t]*)ruby:[ \t]*$/

Class Method Summary collapse

Methods included from TemplateRubyExtractor

build_processed_source, build_snippet, call, empty_snippet, install!

Class Method Details

.handles?(path) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/metz/slim_ruby_extractor.rb', line 30

def handles?(path)
  File.extname(path.to_s) == ".slim" && ::Metz::FileClassifier.view?(path)
end