class Arca::Model
Constants
- CALLBACKS
Array of ActiveRecord callback method symbols in a rough order of when they are used in the life cycle of an ActiveRecord model.
Attributes
Public: Hash of collected callback data.
Public: String file path.
Public: ActiveRecord model class.
Public: String model name.
Public Class Methods
Arca::Model
wraps an ActiveRecord model class and provides an interface to the collected and analyzed callback data for that class and the file path to the model class.
# File lib/arca/model.rb, line 7 def initialize(klass) @klass = klass @name = klass.name @callbacks = klass.arca_callback_data.dup @file_path = callbacks.delete(:model_file_path) end
Public Instance Methods
Public: Hash of CallbackAnalysis
objects for each callback type.
# File lib/arca/model.rb, line 63 def analyzed_callbacks @analyzed_callbacks ||= CALLBACKS.inject({}) do |result, callback_symbol| Array(callbacks[callback_symbol]).each do |callback_data| result[callback_symbol] ||= [] callback_analysis = CallbackAnalysis.new(self, callback_data) unless callback_analysis.target_file_path_active_record? result[callback_symbol] << callback_analysis end end result end end
Public: Array of all CallbackAnalysis
objects for this model.
# File lib/arca/model.rb, line 78 def analyzed_callbacks_array @analyzed_callbacks_array ||= analyzed_callbacks.values.flatten end
Public: Integer representing the number of callbacks analyzed.
# File lib/arca/model.rb, line 83 def analyzed_callbacks_count analyzed_callbacks_array.size end
Public: Integer representing the number of callbacks called for this class from files other than this model.
# File lib/arca/model.rb, line 102 def external_callbacks_count analyzed_callbacks_array.select {|analysis| analysis.external_callback? }.size end
Public: Integer representing the number of conditional callback targets that are defined in files other than this model.
# File lib/arca/model.rb, line 114 def external_conditionals_count analyzed_callbacks_array.select {|analysis| analysis.external_conditional_target? }.size end
Public: Integer representing the number of callback targets that are defined in files other than this model.
# File lib/arca/model.rb, line 108 def external_targets_count analyzed_callbacks_array.select {|analysis| analysis.external_target? }.size end
Public: Integer representing the total number of lines between callbacks called for this class from files other than the one where the class is defined.
# File lib/arca/model.rb, line 90 def lines_between_count lines_between = 0 line_numbers = analyzed_callbacks_array.map &:callback_line_number sorted_line_numbers = line_numbers.sort {|a,b| b <=> a } sorted_line_numbers.each_with_index do |line_number, index| lines_between += line_number - (sorted_line_numbers[index + 1] || 0) end lines_between end
Public: Arca::Report
for this model.
# File lib/arca/model.rb, line 36 def report @report ||= Report.new(self) end
Public: Helper method for finding the file path and line number where a method is located for the ActiveRecord model.
method_symbol - Symbol representation of the method name.
# File lib/arca/model.rb, line 44 def source_location(method_symbol) source_location = klass.instance_method(method_symbol).source_location { :file_path => source_location[0], :line_number => source_location[1] } rescue NameError { :file_path => nil, :line_number => nil } rescue TypeError { :file_path => nil, :line_number => nil } end