Class: Arca::Model
- Inherits:
-
Object
- Object
- Arca::Model
- Defined in:
- lib/arca/model.rb
Constant Summary collapse
- CALLBACKS =
Array of ActiveRecord callback method symbols in a rough order of when they are used in the life cycle of an ActiveRecord model.
[ :after_initialize, :after_find, :after_touch, :before_validation, :after_validation, :before_save, :around_save, :after_save, :after_save_commit, :before_create, :around_create, :after_create, :after_create_commit, :before_update, :around_update, :after_update, :after_update_commit, :before_destroy, :around_destroy, :after_destroy, :after_destroy_commit, :after_commit, :after_rollback ]
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Public: Hash of collected callback data.
-
#file_path ⇒ Object
readonly
Public: String file path.
-
#klass ⇒ Object
readonly
Public: ActiveRecord model class.
-
#name ⇒ Object
readonly
Public: String model name.
Instance Method Summary collapse
-
#analyzed_callbacks ⇒ Object
Public: Hash of CallbackAnalysis objects for each callback type.
-
#analyzed_callbacks_array ⇒ Object
Public: Array of all CallbackAnalysis objects for this model.
-
#analyzed_callbacks_count ⇒ Object
Public: Integer representing the number of callbacks analyzed.
-
#external_callbacks_count ⇒ Object
Public: Integer representing the number of callbacks called for this class from files other than this model.
-
#external_conditionals_count ⇒ Object
Public: Integer representing the number of conditional callback targets that are defined in files other than this model.
-
#external_targets_count ⇒ Object
Public: Integer representing the number of callback targets that are defined in files other than this model.
-
#initialize(klass) ⇒ Model
constructor
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.
-
#lines_between_count ⇒ Object
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.
-
#report ⇒ Object
Public: Arca::Report for this model.
-
#source_location(method_symbol) ⇒ Object
Public: Helper method for finding the file path and line number where a method is located for the ActiveRecord model.
Constructor Details
#initialize(klass) ⇒ Model
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.
7 8 9 10 11 12 |
# 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 |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Public: Hash of collected callback data.
35 36 37 |
# File 'lib/arca/model.rb', line 35 def callbacks @callbacks end |
#file_path ⇒ Object (readonly)
Public: String file path.
32 33 34 |
# File 'lib/arca/model.rb', line 32 def file_path @file_path end |
#klass ⇒ Object (readonly)
Public: ActiveRecord model class.
26 27 28 |
# File 'lib/arca/model.rb', line 26 def klass @klass end |
#name ⇒ Object (readonly)
Public: String model name.
29 30 31 |
# File 'lib/arca/model.rb', line 29 def name @name end |
Instance Method Details
#analyzed_callbacks ⇒ Object
Public: Hash of CallbackAnalysis objects for each callback type.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/arca/model.rb', line 65 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 |
#analyzed_callbacks_array ⇒ Object
Public: Array of all CallbackAnalysis objects for this model.
80 81 82 |
# File 'lib/arca/model.rb', line 80 def analyzed_callbacks_array @analyzed_callbacks_array ||= analyzed_callbacks.values.flatten end |
#analyzed_callbacks_count ⇒ Object
Public: Integer representing the number of callbacks analyzed.
85 86 87 |
# File 'lib/arca/model.rb', line 85 def analyzed_callbacks_count analyzed_callbacks_array.size end |
#external_callbacks_count ⇒ Object
Public: Integer representing the number of callbacks called for this class from files other than this model.
104 105 106 |
# File 'lib/arca/model.rb', line 104 def external_callbacks_count analyzed_callbacks_array.select {|analysis| analysis.external_callback? }.size end |
#external_conditionals_count ⇒ Object
Public: Integer representing the number of conditional callback targets that are defined in files other than this model.
116 117 118 |
# File 'lib/arca/model.rb', line 116 def external_conditionals_count analyzed_callbacks_array.select {|analysis| analysis.external_conditional_target? }.size end |
#external_targets_count ⇒ Object
Public: Integer representing the number of callback targets that are defined in files other than this model.
110 111 112 |
# File 'lib/arca/model.rb', line 110 def external_targets_count analyzed_callbacks_array.select {|analysis| analysis.external_target? }.size end |
#lines_between_count ⇒ Object
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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/arca/model.rb', line 92 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 |
#report ⇒ Object
Public: Arca::Report for this model.
38 39 40 |
# File 'lib/arca/model.rb', line 38 def report @report ||= Report.new(self) end |
#source_location(method_symbol) ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/arca/model.rb', line 46 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 |