Class: TkInspect::ClassBrowser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tk_inspect/class_browser/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tk_inspect/class_browser/base.rb', line 13

def initialize
  @tk_root = nil
  @main_component = nil
  @selected_class_path = []
  @loaded_code = {}
  @class_filter = nil
  @browsing_method = 'hierarchy'
  @class_data_sources = {
    hierarchy: ClassTreeDataSource.new,
    namespaces: ClassNamespaceDataSource.new
  }.with_indifferent_access
  @module_method_data_source = ModuleMethodDataSource.new
end

Instance Attribute Details

#browsing_methodObject

Returns the value of attribute browsing_method.



9
10
11
# File 'lib/tk_inspect/class_browser/base.rb', line 9

def browsing_method
  @browsing_method
end

#class_data_sourcesObject

Returns the value of attribute class_data_sources.



10
11
12
# File 'lib/tk_inspect/class_browser/base.rb', line 10

def class_data_sources
  @class_data_sources
end

#main_componentObject

Returns the value of attribute main_component.



5
6
7
# File 'lib/tk_inspect/class_browser/base.rb', line 5

def main_component
  @main_component
end

#module_method_data_sourceObject

Returns the value of attribute module_method_data_source.



11
12
13
# File 'lib/tk_inspect/class_browser/base.rb', line 11

def module_method_data_source
  @module_method_data_source
end

#selected_class_pathObject

Returns the value of attribute selected_class_path.



6
7
8
# File 'lib/tk_inspect/class_browser/base.rb', line 6

def selected_class_path
  @selected_class_path
end

#selected_methodObject

Returns the value of attribute selected_method.



8
9
10
# File 'lib/tk_inspect/class_browser/base.rb', line 8

def selected_method
  @selected_method
end

#selected_moduleObject

Returns the value of attribute selected_module.



7
8
9
# File 'lib/tk_inspect/class_browser/base.rb', line 7

def selected_module
  @selected_module
end

#tk_rootObject

Returns the value of attribute tk_root.



4
5
6
# File 'lib/tk_inspect/class_browser/base.rb', line 4

def tk_root
  @tk_root
end

Instance Method Details

#class_data_sourceObject



38
39
40
# File 'lib/tk_inspect/class_browser/base.rb', line 38

def class_data_source
  @class_data_sources[browsing_method]
end

#code_for_file(file) ⇒ Object



72
73
74
75
76
77
# File 'lib/tk_inspect/class_browser/base.rb', line 72

def code_for_file(file)
  @loaded_code[file] || begin
                          @loaded_code[file] = IO.read(file)
                          @loaded_code[file]
                        end
end

#code_for_method(meth) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/tk_inspect/class_browser/base.rb', line 61

def code_for_method(meth)
  return nil unless meth.present?
  class_name = selected_class_name
  return nil unless class_name.present?
  klass = Object.const_get(class_name)
  method = klass.instance_method(meth)
  file, line = method.source_location
  return nil unless file && line
  return code_for_file(file), line, file
end

#create_rootObject



31
32
33
34
35
36
# File 'lib/tk_inspect/class_browser/base.rb', line 31

def create_root
  @tk_root = TkComponent::Window.new(title: "Class Browser")
  @main_component = RootComponent.new
  @main_component.class_browser = self
  @tk_root.place_root_component(@main_component)
end

#refreshObject



27
28
29
# File 'lib/tk_inspect/class_browser/base.rb', line 27

def refresh
  @main_component.nil? ? create_root : @main_component.regenerate
end

#select_class_name(class_name) ⇒ Object



42
43
44
45
46
# File 'lib/tk_inspect/class_browser/base.rb', line 42

def select_class_name(class_name)
  self.browsing_method = 'hierarchy'
  class_path = class_data_source.path_for_class(class_name)
  select_class_path(class_path)
end

#select_class_path(class_path) ⇒ Object



52
53
54
55
# File 'lib/tk_inspect/class_browser/base.rb', line 52

def select_class_path(class_path)
  self.selected_class_path = class_path
  module_method_data_source.selected_class = selected_class_name
end

#selected_class_nameObject



57
58
59
# File 'lib/tk_inspect/class_browser/base.rb', line 57

def selected_class_name
  browsing_method.to_s == 'hierarchy' ? self.selected_class_path.last : self.selected_class_path.join('::')
end

#show_current_pathObject



48
49
50
# File 'lib/tk_inspect/class_browser/base.rb', line 48

def show_current_path
  @main_component.show_current_selection
end