class Romajic::Config

Configurations for {Cop}

Constants

ALLOWED_CONVERTERS
HEPBURN_CONVERTERS

Attributes

distance[R]
exclude_words[R]
target_words[R]

Public Class Methods

new(options) click to toggle source

Initialize a new Config object

@param options [Hash] Initialize options @option options [String] :word Target romaji @option options [String] :exclude_word Word to exclude @option options [String] :config Path of the configuration file @option options [String] :dir Path of target directory @option options [String] :extensions Comma-separated target extensions @option options [Integer] :distance Levenshtein distance @option options [String] :converter Romaji converter

# File lib/romajic/config.rb, line 23
def initialize(options)
  @options = Marshal.load(Marshal.dump(options))
  set_configs_from_file
  set_converter
  set_target_words
  set_exclude_words
  set_root_dir
  set_extensions
  set_distance
end

Public Instance Methods

exclude_word?(word) click to toggle source

Check if a word is the excluded word

@param word [String] A word, e.g. class, const @return [Boolean] True if a word is the excluded word

# File lib/romajic/config.rb, line 47
def exclude_word?(word)
  exclude_words.include?(word)
end
target_file_pattern() click to toggle source

Get the glob pattern of the search target files

@return [String] Glob pattern

# File lib/romajic/config.rb, line 37
def target_file_pattern
  pattern = "#{@root_dir}/**/*"
  pattern += ".{#{@extensions.join(',')}}" unless @extensions.nil?
  pattern
end

Private Instance Methods

allowed_converter?() click to toggle source
# File lib/romajic/config.rb, line 74
def allowed_converter?
  ALLOWED_CONVERTERS.include?(@converter)
end
hepburn_converter?() click to toggle source
# File lib/romajic/config.rb, line 70
def hepburn_converter?
  HEPBURN_CONVERTERS.include?(@converter)
end
set_configs_from_file() click to toggle source
# File lib/romajic/config.rb, line 53
def set_configs_from_file
  configs = YAML.load_file(@options[:config])
  @configs = Hash[configs.map { |k, v| [k.to_sym, v] }]
end
set_converter() click to toggle source
# File lib/romajic/config.rb, line 58
def set_converter
  @converter = @options[:converter] || @configs[:converter] || 'hepburn'
  set_converter_options
  @converter = 'hepburn' if hepburn_converter?

  raise "No such converter - #{@converter}" unless allowed_converter?
end
set_converter_options() click to toggle source
# File lib/romajic/config.rb, line 66
def set_converter_options
  @converter_options = @converter == 'traditional_hepburn' ? { traditional: true } : {}
end
set_distance() click to toggle source
# File lib/romajic/config.rb, line 116
def set_distance
  @distance = @options[:distance] || @configs[:distance] || 3
end
set_exclude_words() click to toggle source
# File lib/romajic/config.rb, line 90
def set_exclude_words
  if @options[:exclude_word]
    @exclude_words = [@options[:exclude_word]]
  else
    @exclude_words = @configs[:exclude_words] || []
  end
end
set_extensions() click to toggle source
# File lib/romajic/config.rb, line 108
def set_extensions
  if @options[:extensions]
    @extensions = @options[:extensions].split(',')
  else
    @extensions = @configs[:extensions]
  end
end
set_root_dir() click to toggle source
# File lib/romajic/config.rb, line 98
def set_root_dir
  if @options[:dir]
    @root_dir = File.expand_path(@options[:dir])
  elsif @configs[:root_dir]
    @root_dir = File.expand_path(@configs[:root_dir], File.dirname(@options[:config]))
  else
    @root_dir = File.expand_path('.')
  end
end
set_target_words() click to toggle source
# File lib/romajic/config.rb, line 78
def set_target_words
  if @options[:word]
    @target_words = [@options[:word]]
  else
    @target_words = @configs[:target_words] || []
  end

  @target_words.map! do |word|
    word.ascii_only? ? word : Romajify::Converter.public_send(@converter, word, @converter_options)
  end
end