Class: Debtective::Comments::Comment::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/debtective/comments/comment/base.rb

Overview

Hold comment information

Direct Known Subclasses

Fixme, Magic, Note, Offense, Shebang, Todo, Yard

Constant Summary collapse

REGULAR_COMMENT =

Does not match YARD or shebang comments

/^\s*#\s(?!@)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pathname:, index:) ⇒ Base

Returns a new instance of Base.

Parameters:

  • pathname (Pathname)

    path of the file

  • index (Integer)

    position of the comment in the file



17
18
19
20
# File 'lib/debtective/comments/comment/base.rb', line 17

def initialize(pathname:, index:)
  @pathname = pathname
  @index = index
end

Instance Attribute Details

#pathnameObject

Returns the value of attribute pathname.



13
14
15
# File 'lib/debtective/comments/comment/base.rb', line 13

def pathname
  @pathname
end

Instance Method Details

#comment_endInteger

Returns:

  • (Integer)


33
34
35
# File 'lib/debtective/comments/comment/base.rb', line 33

def comment_end
  @comment_end ||= last_following_comment_index || comment_start
end

#comment_startInteger

Returns:

  • (Integer)


28
29
30
# File 'lib/debtective/comments/comment/base.rb', line 28

def comment_start
  @index
end

#commitDebtective::Comments::FindCommit::Commit

Return commit that introduced the todo



55
56
57
# File 'lib/debtective/comments/comment/base.rb', line 55

def commit
  @commit ||= FindCommit.new(pathname: @pathname, line: lines[@index]).call
end

#daysInteger

Returns:

  • (Integer)


60
61
62
63
64
# File 'lib/debtective/comments/comment/base.rb', line 60

def days
  return if commit.time.nil?

  ((Time.now - commit.time) / (24 * 60 * 60)).round
end

#locationString

Location in the codebase (for clickable link)

Returns:

  • (String)


49
50
51
# File 'lib/debtective/comments/comment/base.rb', line 49

def location
  "#{@pathname.to_s.gsub(%r{^./}, "")}:#{comment_start + 1}"
end

#statement_endInteger

Returns:

  • (Integer)


43
44
45
# File 'lib/debtective/comments/comment/base.rb', line 43

def statement_end
  statement_start
end

#statement_startInteger

Returns:

  • (Integer)


38
39
40
# File 'lib/debtective/comments/comment/base.rb', line 38

def statement_start
  @statement_start ||= inline? ? @index : comment_end.next
end

#to_hHash

Returns:

  • (Hash)


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/debtective/comments/comment/base.rb', line 67

def to_h
  {
    pathname: @pathname,
    location: location,
    type: type,
    comment_boundaries: [comment_start, comment_end],
    statement_boundaries: [statement_start, statement_end],
    commit: commit.sha,
    author: commit.author.to_h,
    time: commit.time
  }
end

#typeString

Returns:

  • (String)


23
24
25
# File 'lib/debtective/comments/comment/base.rb', line 23

def type
  self.class.name.split("::").last.downcase
end