Class: Romajic::Cop

Inherits:
Object
  • Object
show all
Defined in:
lib/romajic/cop.rb

Overview

Search logic class of Romajic

Constant Summary collapse

TARGET_KINDS =
CodeRay::TokenKinds.keys

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cop

Initialize a new Cop object

Parameters:

  • options (Hash)

    Initialize options

Options Hash (options):

  • :word (String)

    Target romaji

  • :exclude_word (String)

    Word to exclude

  • :config (String)

    Path of the configuration file

  • :dir (String)

    Path of target directory

  • :extensions (String)

    Comma-separated target extensions

  • :distance (Integer)

    Levenshtein distance

  • :converter (String)

    Romaji converter



21
22
23
# File 'lib/romajic/cop.rb', line 21

def initialize(options)
  @config = Config.new(options)
end

Instance Method Details

#searchObject

Search romaji in the source files



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/romajic/cop.rb', line 26

def search
  Dir.glob(@config.target_file_pattern, File::FNM_CASEFOLD).each do |file_path|
    next if FileTest.directory?(file_path)

    extension = File.extname(file_path).sub(/^./, '').downcase

    if extension.empty?
      tokens = CodeRay.scan(File.read(file_path), :txt).tokens
      search_in_plain_text(tokens, file_path)
    else
      tokens = CodeRay.scan(File.read(file_path), extension.to_sym).tokens
      search_in_tokens(tokens, file_path)
    end
  end

  nil
end