class AdLint::Source

DESCRIPTION

Target source file.

Attributes

fpath[R]

VALUE

String – The path name of this source file.

included_at[R]

Public Class Methods

new(fpath, fenc, included_at = Location.new) click to toggle source

DESCRIPTION

Constructs a target source file.

PARAMETER

fpath

Pathname – Path name of the target source file.

content

StringSource content.

# File lib/adlint/source.rb, line 47
def initialize(fpath, fenc, included_at = Location.new)
  @fpath       = fpath
  @fenc        = fenc
  @included_at = included_at
  @content     = nil
end

Public Instance Methods

analysis_target?(traits) click to toggle source
# File lib/adlint/source.rb, line 74
def analysis_target?(traits)
  Location.new(@fpath).in_analysis_target?(traits)
end
open() { |io| ... } click to toggle source

DESCRIPTION

Opens the target source file.

PARAMETER

block

Proc – Yieldee block.

# File lib/adlint/source.rb, line 83
def open(&block)
  @content ||= read_content(@fpath)
  io = StringIO.new(@content)
  yield(io)
ensure
  io && io.close
end
system_header?() click to toggle source
# File lib/adlint/source.rb, line 70
def system_header?
  false
end
to_s() click to toggle source

DESCRIPTION

Converts this source content into string.

RETURN VALUE

StringContent of this source file.

# File lib/adlint/source.rb, line 96
def to_s
  @content ||= read_content(@fpath)
end
user_header?() click to toggle source
# File lib/adlint/source.rb, line 66
def user_header?
  false
end

Private Instance Methods

notify_cr_at_eol_found(loc) click to toggle source
# File lib/adlint/source.rb, line 122
def notify_cr_at_eol_found(loc)
  on_cr_at_eol_found.invoke(loc)
end
notify_eof_mark_at_eof_found(loc) click to toggle source
# File lib/adlint/source.rb, line 126
def notify_eof_mark_at_eof_found(loc)
  on_eof_mark_at_eof_found.invoke(loc)
end
notify_eof_newline_not_found(loc) click to toggle source
# File lib/adlint/source.rb, line 130
def notify_eof_newline_not_found(loc)
  on_eof_newline_not_found.invoke(loc)
end
read_content(fpath) click to toggle source
# File lib/adlint/source.rb, line 101
def read_content(fpath)
  cont = IO.read(fpath, mode: "rb", encoding: @fenc || "binary")

  if cont =~ /\r/
    notify_cr_at_eol_found(Location.new(fpath))
    cont = cont.gsub(/\r\n|\r/, "\n")
  end

  if cont =~ /\x1a/
    notify_eof_mark_at_eof_found(Location.new(fpath))
    cont = cont.gsub(/\x1a/, "")
  end

  unless cont.end_with?("\n")
    notify_eof_newline_not_found(Location.new(fpath))
    cont << "\n"
  end

  cont
end