Class: GrepInteractors::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/grep_interactors/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interactors_path, file_path) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
# File 'lib/grep_interactors/parser.rb', line 7

def initialize(interactors_path, file_path)
  @interactors_path = interactors_path
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



5
6
7
# File 'lib/grep_interactors/parser.rb', line 5

def file_path
  @file_path
end

#interactors_pathObject (readonly)

Returns the value of attribute interactors_path.



5
6
7
# File 'lib/grep_interactors/parser.rb', line 5

def interactors_path
  @interactors_path
end

Instance Method Details

#file_pathsObject



12
13
14
# File 'lib/grep_interactors/parser.rb', line 12

def file_paths
  recursive_parse_file_paths([file_path])
end

#get_class_name_by_path(path) ⇒ Object



45
46
47
# File 'lib/grep_interactors/parser.rb', line 45

def get_class_name_by_path(path)
  ActiveSupport::Inflector.camelize(path.gsub(interactors_path, "").gsub(".rb", ""))
end

#get_file_path_by_class(class_name) ⇒ Object



49
50
51
# File 'lib/grep_interactors/parser.rb', line 49

def get_file_path_by_class(class_name)
  "#{interactors_path}#{ActiveSupport::Inflector.underscore(class_name)}.rb"
end

#get_full_class_name(constant, file_path) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/grep_interactors/parser.rb', line 36

def get_full_class_name(constant, file_path)
  return constant if Pathname.new(get_file_path_by_class(constant)).exist?

  current_module = get_class_name_by_path(file_path).split("::")[0..-2].join("::")
  full_class_name = "#{current_module}::#{constant}"

  full_class_name if Pathname.new(get_file_path_by_class(full_class_name)).exist?
end

#parse_files_paths(class_names) ⇒ Object



23
24
25
# File 'lib/grep_interactors/parser.rb', line 23

def parse_files_paths(class_names)
  class_names.map { |class_name| get_file_path_by_class(class_name) }
end

#parse_interactors(file_path) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/grep_interactors/parser.rb', line 27

def parse_interactors(file_path)
  interactors = File.read(file_path).scan(/[A-Z][a-z]*[::[A-Z][a-z]*]*/).map do |constant|
    get_full_class_name(constant, file_path)
  end.compact
  current_class = get_class_name_by_path(file_path)

  interactors - [current_class]
end

#recursive_parse_file_paths(file_paths) ⇒ Object



16
17
18
19
20
21
# File 'lib/grep_interactors/parser.rb', line 16

def recursive_parse_file_paths(file_paths)
  return [] if file_paths.empty?

  new_file_paths = parse_files_paths parse_interactors file_paths[0]
  new_file_paths + recursive_parse_file_paths(file_paths[1..-1]) + recursive_parse_file_paths(new_file_paths)
end