Class: Arca::Model

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#callbacksObject (readonly)

Public: Hash of collected callback data.



33
34
35
# File 'lib/arca/model.rb', line 33

def callbacks
  @callbacks
end

#file_pathObject (readonly)

Public: String file path.



30
31
32
# File 'lib/arca/model.rb', line 30

def file_path
  @file_path
end

#klassObject (readonly)

Public: ActiveRecord model class.



24
25
26
# File 'lib/arca/model.rb', line 24

def klass
  @klass
end

#nameObject (readonly)

Public: String model name.



27
28
29
# File 'lib/arca/model.rb', line 27

def name
  @name
end

Instance Method Details

#analyzed_callbacksObject

Public: Hash of CallbackAnalysis objects for each callback type.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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({
        :model         => self,
        :callback_data => callback_data
      })

      unless callback_analysis.target_file_path_active_record?
        result[callback_symbol] << callback_analysis
      end
    end
    result
  end
end

#analyzed_callbacks_arrayObject

Public: Array of all CallbackAnalysis objects for this model.



81
82
83
# File 'lib/arca/model.rb', line 81

def analyzed_callbacks_array
  @analyzed_callbacks_array ||= analyzed_callbacks.values.flatten
end

#analyzed_callbacks_countObject

Public: Integer representing the number of callbacks analyzed.



86
87
88
# File 'lib/arca/model.rb', line 86

def analyzed_callbacks_count
  analyzed_callbacks_array.size
end

#external_callbacks_countObject

Public: Integer representing the number of callbacks called for this class from files other than this model.



105
106
107
# File 'lib/arca/model.rb', line 105

def external_callbacks_count
  analyzed_callbacks_array.select {|analysis| analysis.external_callback? }.size
end

#external_conditionals_countObject

Public: Integer representing the number of conditional callback targets that are defined in files other than this model.



117
118
119
# File 'lib/arca/model.rb', line 117

def external_conditionals_count
  analyzed_callbacks_array.select {|analysis| analysis.external_conditional_target? }.size
end

#external_targets_countObject

Public: Integer representing the number of callback targets that are defined in files other than this model.



111
112
113
# File 'lib/arca/model.rb', line 111

def external_targets_count
  analyzed_callbacks_array.select {|analysis| analysis.external_target? }.size
end

#lines_between_countObject

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.



93
94
95
96
97
98
99
100
101
# File 'lib/arca/model.rb', line 93

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

#reportObject

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