Class: Arca::CallbackAnalysis

Inherits:
Object
  • Object
show all
Defined in:
lib/arca/callback_analysis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, callback_data) ⇒ CallbackAnalysis

Arca::CallbackAnalysis takes an Arca::Model and data for a specific callback and then calculates and exposes a complete analysis for the callback including target methods, file paths, line numbers, booleans representing whether targets are in the same file they are called from, and finally the number of lines between callers and the target methods they call.

model - Arca::Model instance. callback_data - Hash with callback data collected by Arca::Collector.



13
14
15
16
17
18
19
20
21
# File 'lib/arca/callback_analysis.rb', line 13

def initialize(model, callback_data)
  @model                     = model
  @callback_symbol           = callback_data.fetch(:callback_symbol)
  @callback_file_path        = callback_data.fetch(:callback_file_path)
  @callback_line_number      = callback_data.fetch(:callback_line_number)
  @target_symbol             = callback_data.fetch(:target_symbol)
  @conditional_symbol        = callback_data[:conditional_symbol]
  @conditional_target_symbol = callback_data[:conditional_target_symbol]
end

Instance Attribute Details

#callback_file_pathObject (readonly)

Public: String path to the file where the callback is used.



56
57
58
# File 'lib/arca/callback_analysis.rb', line 56

def callback_file_path
  @callback_file_path
end

#callback_line_numberObject (readonly)

Public: Integer line number where the callback is called.



59
60
61
# File 'lib/arca/callback_analysis.rb', line 59

def callback_line_number
  @callback_line_number
end

#callback_symbolObject (readonly)

Public: Symbol representing the callback method name.



53
54
55
# File 'lib/arca/callback_analysis.rb', line 53

def callback_symbol
  @callback_symbol
end

#conditional_symbolObject (readonly)

Public: Symbol representing the conditional target method name.



97
98
99
# File 'lib/arca/callback_analysis.rb', line 97

def conditional_symbol
  @conditional_symbol
end

#conditional_target_symbolObject (readonly)

Returns the value of attribute conditional_target_symbol.



98
99
100
# File 'lib/arca/callback_analysis.rb', line 98

def conditional_target_symbol
  @conditional_target_symbol
end

#modelObject (readonly)

Public: Arca::Model this callback belongs to.



50
51
52
# File 'lib/arca/callback_analysis.rb', line 50

def model
  @model
end

#target_symbolObject (readonly)

Public: Symbol representing the callback target method name.



68
69
70
# File 'lib/arca/callback_analysis.rb', line 68

def target_symbol
  @target_symbol
end

Instance Method Details

#conditional_target_file_pathObject

Public: String path to the file where the conditional target is located.



101
102
103
104
105
# File 'lib/arca/callback_analysis.rb', line 101

def conditional_target_file_path
  return if conditional_target_symbol.nil?

  model.source_location(conditional_target_symbol)[:file_path]
end

#conditional_target_line_numberObject

Public: Integer line number where the conditional target is located.



108
109
110
111
112
# File 'lib/arca/callback_analysis.rb', line 108

def conditional_target_line_number
  return if conditional_target_symbol.nil?

  model.source_location(conditional_target_symbol)[:line_number]
end

#external_callback?Boolean

Public: Boolean representing whether the callback is used in the same file where the ActiveRecord model is defined.

Returns:

  • (Boolean)


63
64
65
# File 'lib/arca/callback_analysis.rb', line 63

def external_callback?
  callback_file_path != model.file_path
end

#external_conditional_target?Boolean

Public: Boolean representing whether the conditional target is located in the same file where the callback is defined.

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/arca/callback_analysis.rb', line 116

def external_conditional_target?
  return false if conditional_target_symbol.nil?
  return false if conditional_target_symbol.is_a?(Array)
  return false if [:create, :update, :destroy].include?(conditional_target_symbol)

  callback_file_path != conditional_target_file_path
end

#external_target?Boolean

Public: Boolean representing whether the callback target is located in the same file where the callback is defined.

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/arca/callback_analysis.rb', line 82

def external_target?
  return false if target_symbol == :inline
  target_file_path != callback_file_path
end

#inspectObject

Public: Hash representation of the object for interactive consoles.



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

def inspect
  to_hash.to_s
end

#lines_to_conditional_targetObject

Public: Integer representing the number of lines between where the callback is used and the conditional target is located.



126
127
128
129
130
# File 'lib/arca/callback_analysis.rb', line 126

def lines_to_conditional_target
  return if conditional_target_symbol.nil? || external_conditional_target?

  (conditional_target_line_number - callback_line_number).abs
end

#lines_to_targetObject

Public: Integer representing the number of lines between where the callback is used and the callback target is located.



89
90
91
92
93
94
# File 'lib/arca/callback_analysis.rb', line 89

def lines_to_target
  return if external_target?
  return if target_line_number.nil? || callback_line_number.nil?

  (target_line_number - callback_line_number).abs
end

#target_file_pathObject

Public: String path to the file where the callback target is located.



71
72
73
# File 'lib/arca/callback_analysis.rb', line 71

def target_file_path
  model.source_location(target_symbol)[:file_path]
end

#target_file_path_active_record?Boolean

Public: Boolean representing whether the callback target is located in the ActiveRecord gem.

Returns:

  • (Boolean)


134
135
136
# File 'lib/arca/callback_analysis.rb', line 134

def target_file_path_active_record?
  target_file_path =~ /gems\/activerecord/
end

#target_line_numberObject

Public: Integer line number where the callback target is located.



76
77
78
# File 'lib/arca/callback_analysis.rb', line 76

def target_line_number
  model.source_location(target_symbol)[:line_number]
end

#to_hashObject

Public: Hash of collected and analyzed callback data.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/arca/callback_analysis.rb', line 29

def to_hash
  {
    :callback                       => callback_symbol,
    :callback_file_path             => Arca.relative_path(callback_file_path),
    :callback_line_number           => callback_line_number,
    :external_callback              => external_callback?,
    :target                         => target_symbol,
    :target_file_path               => Arca.relative_path(target_file_path),
    :target_line_number             => target_line_number,
    :external_target                => external_target?,
    :lines_to_target                => lines_to_target,
    :conditional                    => conditional_symbol,
    :conditional_target             => conditional_target_symbol,
    :conditional_target_file_path   => Arca.relative_path(conditional_target_file_path),
    :conditional_target_line_number => conditional_target_line_number,
    :external_conditional_target    => external_conditional_target?,
    :lines_to_conditional_target    => nil
  }
end