Class: MetricFu::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/metric_fu/data_structures/location.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, class_name, method_name) ⇒ Location

Returns a new instance of Location.



18
19
20
21
22
23
24
25
26
# File 'lib/metric_fu/data_structures/location.rb', line 18

def initialize(file_path, class_name, method_name)
  @file_path               = file_path
  @file_name, @line_number = file_path.to_s.split(/:/)
  @class_name              = class_name
  @method_name             = method_name
  @simple_method_name      = @method_name.to_s.sub(@class_name.to_s, "")
  @hash_key                = to_key
  @hash                    = @hash_key.hash
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def class_name
  @class_name
end

#file_nameObject

Returns the value of attribute file_name.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def file_name
  @file_name
end

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def file_path
  @file_path
end

#hashObject

Returns the value of attribute hash.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def hash
  @hash
end

#hash_keyObject

Returns the value of attribute hash_key.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def hash_key
  @hash_key
end

#line_numberObject

Returns the value of attribute line_number.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def line_number
  @line_number
end

#method_nameObject

Returns the value of attribute method_name.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def method_name
  @method_name
end

#simple_method_nameObject

Returns the value of attribute simple_method_name.



5
6
7
# File 'lib/metric_fu/data_structures/location.rb', line 5

def simple_method_name
  @simple_method_name
end

Class Method Details

.for(class_or_method_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/metric_fu/data_structures/location.rb', line 59

def self.for(class_or_method_name)
  class_or_method_name = strip_modules(class_or_method_name)
  if class_or_method_name
    begin
      match = class_or_method_name.match(/(.*)((\.|\#|\:\:[a-z])(.+))/)
    rescue => error
      # new error during port to metric_fu occasionally a unintialized
      # MatchData object shows up here. Not expected.
      mf_debug "ERROR on getting location for #{class_or_method_name} #{error.inspect}"
      match = nil
    end

    # reek reports the method with :: not # on modules like
    # module ApplicationHelper \n def signed_in?, convert it so it records correctly
    # but classes have to start with a capital letter... HACK for REEK bug, reported underlying issue to REEK
    if match
      class_name = strip_modules(match[1])
      method_name = class_or_method_name.gsub(/\:\:/, "#")
    else
      class_name = strip_modules(class_or_method_name)
      method_name = nil
    end
  else
    class_name = nil
    method_name = nil
  end
  get(nil, class_name, method_name)
end

.get(file_path, class_name, method_name) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/metric_fu/data_structures/location.rb', line 8

def self.get(file_path, class_name, method_name)
  location = new(file_path, class_name, method_name)
  @@locations ||= {}
  @@locations.fetch(location.hash_key) do
    @@locations[location.hash_key] = location
    location.finalize
    location
  end
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/metric_fu/data_structures/location.rb', line 50

def <=>(other)
  hash <=> other.hash
end

#eql?(other) ⇒ Boolean

defining :eql? and :hash to use Location as a hash key

Returns:

  • (Boolean)


46
47
48
# File 'lib/metric_fu/data_structures/location.rb', line 46

def eql?(other)
  @hash == other.hash
end

#finalizeObject



88
89
90
91
92
93
94
95
# File 'lib/metric_fu/data_structures/location.rb', line 88

def finalize
  @file_path   &&= @file_path.clone
  @file_name   &&= @file_name.clone
  @line_number &&= @line_number.clone
  @class_name  &&= @class_name.clone
  @method_name &&= @method_name.clone
  freeze  # we cache a lot of method call results, so we want location to be immutable
end

#to_hashObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/metric_fu/data_structures/location.rb', line 28

def to_hash
  hash = {
    "class_name"  => class_name,
    "method_name" => method_name,
    "file_path"   => file_path,
    "file_name"   => file_name,
    "line_number" => line_number,
    "hash_key"    => hash_key,
  }

  if method_name.to_s.size > 0
    hash = hash.merge("simple_method_name" => simple_method_name)
  else
    hash
  end
end

#to_keyObject

Generates the @hash key



55
56
57
# File 'lib/metric_fu/data_structures/location.rb', line 55

def to_key
  [@file_path, @class_name, @method_name].inspect
end