Module: Rulable

Included in:
GetPairsService, LinkageService, PhoneticsConverterService, Voxbi
Defined in:
lib/modules/rulable.rb

Defined Under Namespace

Modules: Macros

Constant Summary collapse

DATA_PATH =
File.expand_path('../../data', __dir__)
CSV_FILENAMES =
%i(conversion linkage exceptions available_pairs).freeze
JSON_FILENAMES =
%i(dictionary).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
# File 'lib/modules/rulable.rb', line 9

def self.included(klass)
  klass.extend(Macros)
end

Instance Method Details

#absolute_path_for(filename, file_format) ⇒ Object



33
34
35
# File 'lib/modules/rulable.rb', line 33

def absolute_path_for(filename, file_format)
  "#{DATA_PATH}/#{filename}" + ".#{file_format}"
end

#memoize_file_content(filename, file_format) ⇒ Object



13
14
15
16
# File 'lib/modules/rulable.rb', line 13

def memoize_file_content(filename, file_format)
  instance_variable_get("@#{filename}") ||
  instance_variable_set("@#{filename}", parse_rule_file(filename, file_format))
end

#parse_csv(file_path) ⇒ Object



27
28
29
30
31
# File 'lib/modules/rulable.rb', line 27

def parse_csv(file_path)
  parsed_csv = CSV.read(file_path, col_sep: '#')

  parsed_csv.first.length > 1 ? Hash[parsed_csv] : parsed_csv.flatten
end

#parse_rule_file(filename, file_format) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/modules/rulable.rb', line 18

def parse_rule_file(filename, file_format)
  absolute_file_path = absolute_path_for(filename, file_format)

  case file_format
  when :csv then parse_csv(absolute_file_path)
  when :json then JSON.parse(File.read(absolute_file_path))
  end
end