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, :before_create, :around_create, :after_create, :before_update, :around_update, :after_update, :before_destroy, :around_destroy, :after_destroy, :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.
33 34 35 |
# File 'lib/arca/model.rb', line 33 def callbacks @callbacks end |
#file_path ⇒ Object (readonly)
Public: String file path.
30 31 32 |
# File 'lib/arca/model.rb', line 30 def file_path @file_path end |
#klass ⇒ Object (readonly)
Public: ActiveRecord model class.
24 25 26 |
# File 'lib/arca/model.rb', line 24 def klass @klass end |
#name ⇒ Object (readonly)
Public: String model name.
27 28 29 |
# File 'lib/arca/model.rb', line 27 def name @name end |
Instance Method Details
#analyzed_callbacks ⇒ Object
Public: Hash of CallbackAnalysis objects for each callback type.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 |
#analyzed_callbacks_array ⇒ Object
Public: Array of all CallbackAnalysis objects for this model.
78 79 80 |
# File 'lib/arca/model.rb', line 78 def analyzed_callbacks_array @analyzed_callbacks_array ||= analyzed_callbacks.values.flatten end |
#analyzed_callbacks_count ⇒ Object
Public: Integer representing the number of callbacks analyzed.
83 84 85 |
# File 'lib/arca/model.rb', line 83 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.
102 103 104 |
# File 'lib/arca/model.rb', line 102 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.
114 115 116 |
# File 'lib/arca/model.rb', line 114 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.
108 109 110 |
# File 'lib/arca/model.rb', line 108 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.
90 91 92 93 94 95 96 97 98 |
# 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 |
#report ⇒ Object
Public: Arca::Report for this model.
36 37 38 |
# File 'lib/arca/model.rb', line 36 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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 |