Class: RubyDetective::SourceRepresentation::Entities::Klass

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_detective/source_representation/entities/klass.rb

Constant Summary

Constants inherited from Base

Base::ROOT_SIGN_SYMBOL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#absolute_path?, #namespace_as_text, #namespace_without_root_sign, #path, #path_as_text, #path_without_root_sign

Constructor Details

#initialize(name, namespace, inheritance_class_name: nil, file_path:, first_line:, last_line:) ⇒ Klass

Returns a new instance of Klass.



7
8
9
10
11
12
13
14
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 7

def initialize(name, namespace, inheritance_class_name: nil, file_path:, first_line:, last_line:)
  @name = name
  @namespace = namespace
  @file_path = file_path
  @inheritance_class_name = inheritance_class_name
  @lines_of_code = last_line - first_line + 1
  @data_store = SourceRepresentation::DataStore.instance
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



5
6
7
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 5

def file_path
  @file_path
end

#inheritance_class_nameObject (readonly)

Returns the value of attribute inheritance_class_name.



5
6
7
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 5

def inheritance_class_name
  @inheritance_class_name
end

#lines_of_codeObject (readonly)

Returns the value of attribute lines_of_code.



5
6
7
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 5

def lines_of_code
  @lines_of_code
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 5

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 5

def namespace
  @namespace
end

Instance Method Details

#constantsObject



16
17
18
19
20
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 16

def constants
  data_store
  .query
  .constants(where: { caller: self })
end

#dependenciesObject



22
23
24
25
26
27
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 22

def dependencies
  constants
  .map(&:refers_to)
  .compact
  .reject{ |c| c.name == name } # Removes circular dependencies
end

#dependentsObject



29
30
31
32
33
34
35
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 29

def dependents
  data_store.query
  .constants(where: { refers_to: self })
  .map(&:caller)
  .compact
  .reject{ |c| c.name == name } # Removes circular dependencies
end

#merge(duplicate) ⇒ Object



37
38
39
40
# File 'lib/ruby_detective/source_representation/entities/klass.rb', line 37

def merge(duplicate)
  @inheritance_class_name ||= duplicate.inheritance_class_name
  @lines_of_code += duplicate.lines_of_code
end