Class: OverridesTracker::MethodsCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/overrides_tracker/methods_collector.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



9
10
11
# File 'lib/overrides_tracker/methods_collector.rb', line 9

def self.instance
  @instance
end

Instance Method Details

#add_instance_method_for_class(class_name, method_name, method_hash) ⇒ Object



13
14
15
# File 'lib/overrides_tracker/methods_collector.rb', line 13

def add_instance_method_for_class(class_name, method_name, method_hash)
  add_method_for_class(:instance_methods, class_name, method_name, method_hash)
end

#add_method_for_class(method_type, class_name, method_name, method_hash) ⇒ Object



21
22
23
24
# File 'lib/overrides_tracker/methods_collector.rb', line 21

def add_method_for_class(method_type, class_name, method_name, method_hash)
  methods_collection(class_name)
  @methods_collection[class_name][method_type][method_name] = method_hash
end

#add_singleton_method_for_class(class_name, method_name, method_hash) ⇒ Object



17
18
19
# File 'lib/overrides_tracker/methods_collector.rb', line 17

def add_singleton_method_for_class(class_name, method_name, method_hash)
  add_method_for_class(:singleton_methods, class_name, method_name, method_hash)
end

#build_overrides_hashObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/overrides_tracker/methods_collector.rb', line 107

def build_overrides_hash
  total_classes = @methods_collection.size
  count = 0
  working_directory = Dir.pwd
  @methods_collection.each do |class_name, class_methods|
    if class_name != nil
      clazz = class_name.constantize
      build_overrides_hash_for_method_type(clazz, class_methods, :instance_methods, working_directory)
      build_overrides_hash_for_method_type(clazz, class_methods, :singleton_methods, working_directory)
    end
    count = count+1
    puts "Processed #{class_name} #{count} / #{total_classes}"
  end
end

#build_overrides_hash_for_method_type(clazz, class_methods, methods_type, working_directory) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/overrides_tracker/methods_collector.rb', line 55

def build_overrides_hash_for_method_type(clazz, class_methods, methods_type, working_directory)
  methods = []
  if methods_type == :instance_methods
    methods = clazz.instance_methods(false)
    clazz.ancestors.each do |ancestor|
      if ancestor.class == Class
        break
      end
      methods = methods + ancestor.instance_methods(false)
    end
  else
    methods = clazz.singleton_methods(false)
    clazz.ancestors.each do |ancestor|
      if ancestor.class == Class
        break
      end
      methods = methods + ancestor.singleton_methods(false)
    end
  end

  methods.each do |method_name|
    if method_name != nil && method_name != :overrides_tracker_finished_file
      method_hash = class_methods[methods_type][method_name]
      begin
        if methods_type == :instance_methods
          method_to_check = clazz.instance_method(method_name)
        else
          method_to_check = clazz.singleton_method(method_name)
        end

        method_to_check_hash = OverridesTracker::Util.method_hash(method_to_check)
      
        if method_to_check_hash[:location] != nil 
          if method_hash != nil
            if method_to_check_hash[:location] != method_hash[:location]
              mark_method_as_override(methods_type, clazz.name, method_name, method_to_check, method_to_check_hash)
              puts "#{method_name} of class #{clazz.name} was overridden".green
            end
          else
            if (method_to_check_hash[:location][0].include? working_directory)
              mark_method_as_added("added_#{methods_type}".to_sym, clazz.name, method_name, method_to_check, method_to_check_hash)
              puts "#{method_name} of class #{clazz.name} was added".green
            end
          end
        end
      rescue Exception => e
        #puts "Error processing #{method_name} of class #{clazz.name}".red
      end
    end
  end
end

#load_from_file(file_name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/overrides_tracker/methods_collector.rb', line 126

def load_from_file(file_name)
  file_path = File.join(Dir.pwd, "/overrides_tracker/#{file_name}")
  data = nil
  begin
    File.open(file_path) do |f|
      file_data = JSON.parse(f.read)
      data = file_data['overridden_methods'] != nil ?  file_data['overridden_methods'] : file_data
    end
  rescue
    puts "Error processing #{file_path}"
  end
  data
end

#mark_method_as_added(method_type, class_name, method_name, overriding_method, method_hash) ⇒ Object



49
50
51
52
53
# File 'lib/overrides_tracker/methods_collector.rb', line 49

def mark_method_as_added(method_type, class_name, method_name, overriding_method, method_hash)
  overridden_methods_collection(class_name)
  @overridden_methods_collection[class_name][method_type][method_name] =  method_hash
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_location] = overriding_method.source_location
end

#mark_method_as_added_instance(class_name, method_name, overriding_method, method_hash) ⇒ Object



41
42
43
# File 'lib/overrides_tracker/methods_collector.rb', line 41

def mark_method_as_added_instance(class_name, method_name, overriding_method, method_hash)
  mark_method_as_added(:added_instance_methods, class_name, method_name, overriding_method, method_hash)
end

#mark_method_as_added_singleton(class_name, method_name, overriding_method, method_hash) ⇒ Object



45
46
47
# File 'lib/overrides_tracker/methods_collector.rb', line 45

def mark_method_as_added_singleton(class_name, method_name, overriding_method, method_hash)
  mark_method_as_added(:added_singleton_methods, class_name, method_name, overriding_method, method_hash)
end

#mark_method_as_instance_override(class_name, method_name, overriding_method, method_hash) ⇒ Object



26
27
28
# File 'lib/overrides_tracker/methods_collector.rb', line 26

def mark_method_as_instance_override(class_name, method_name, overriding_method, method_hash)
  mark_method_as_override(:instance_methods, class_name, method_name, overriding_method, method_hash)
end

#mark_method_as_override(method_type, class_name, method_name, overriding_method, method_hash) ⇒ Object



34
35
36
37
38
39
# File 'lib/overrides_tracker/methods_collector.rb', line 34

def mark_method_as_override(method_type, class_name, method_name, overriding_method, method_hash)
  overridden_methods_collection(class_name)
  @overridden_methods_collection[class_name][method_type][method_name] = @methods_collection[class_name][method_type][method_name]
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_location] = overriding_method.source_location
  @overridden_methods_collection[class_name][method_type][method_name][:overriding_body] = method_hash[:body]
end

#mark_method_as_singleton_override(class_name, method_name, overriding_method, method_hash) ⇒ Object



30
31
32
# File 'lib/overrides_tracker/methods_collector.rb', line 30

def mark_method_as_singleton_override(class_name, method_name, overriding_method, method_hash)
  mark_method_as_override(:singleton_methods, class_name, method_name, overriding_method, method_hash)
end

#overridden_methodsObject



122
123
124
# File 'lib/overrides_tracker/methods_collector.rb', line 122

def overridden_methods
  @overridden_methods_collection
end

#report(api_token) ⇒ Object



159
160
161
# File 'lib/overrides_tracker/methods_collector.rb', line 159

def report(api_token)
  OverridesTracker::Api.report_build(api_token, branch_name_to_report, last_commit_id, last_commit_name_to_report, path_to_report_file)
end

#save_to_fileObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/overrides_tracker/methods_collector.rb', line 140

def save_to_file
  
  file_data = {}
  file_data[:version] = OverridesTracker::VERSION
  file_data[:branch_name] = branch_name
  file_data[:branch_name_to_report] = branch_name_to_report
  file_data[:last_commit_id] = last_commit_id
  file_data[:last_commit_name] = last_commit_name
  file_data[:last_commit_name_to_report] = last_commit_name_to_report
  file_data[:overridden_methods] = @overridden_methods_collection

  File.open(path_to_report_file, "w") do |f|
    f << file_data.to_json
  end
  puts '  '
  puts '==========='
  puts "Report saved to #{path_to_report_file}."
end