Class: RubyLint::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-lint/report.rb,
lib/ruby-lint/report/entry.rb

Overview

Report is a data container for error messages, warnings, informational messages and other custom defined types of messages. It should be used by the various analysis classes to store information such as errors for undefined variables and warnings for potentially confusing code.

Levels

Each instance can add entries for a set of predefined reporting levels. By default the following levels are defined:

  • error
  • warning
  • info

Unless other levels are specified when creating an instance these levels are used for each new instance. Extra levels can be specified in the constructor of this class.

Adding Entries

Adding entries can be done by either calling #add:

report = RubyLint::Report.new

report.add(
  :level   => :info,
  :message => 'informational message',
  :line    => 1,
  :column  => 2,
  :file    => 'file.rb'
)

When using #add any invalid/disabled reporting levels will be silently ignored. This makes it easier for code to add entries of a particular level without having to manually check if said level is enabled.

Defined Under Namespace

Classes: Entry

Constant Summary

DEFAULT_LEVELS =

Reporting levels that are always available.

Returns:

  • (Array)
[:error, :warning, :info].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(levels = DEFAULT_LEVELS) ⇒ Report

Returns a new instance of Report

Parameters:

  • levels (Array) (defaults to: DEFAULT_LEVELS)

    The reporting levels to enable for this instance.



58
59
60
61
# File 'lib/ruby-lint/report.rb', line 58

def initialize(levels = DEFAULT_LEVELS)
  @levels  = levels.map(&:to_sym)
  @entries = []
end

Instance Attribute Details

#entriesArray (readonly)

Returns The entries of the report.

Returns:

  • (Array)

    The entries of the report.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
87
88
# File 'lib/ruby-lint/report.rb', line 45

class Report
  attr_reader :entries, :levels

  ##
  # Reporting levels that are always available.
  #
  # @return [Array]
  #
  DEFAULT_LEVELS = [:error, :warning, :info].freeze

  ##
  # @param [Array] levels The reporting levels to enable for this instance.
  #
  def initialize(levels = DEFAULT_LEVELS)
    @levels  = levels.map(&:to_sym)
    @entries = []
  end

  ##
  # Adds a new entry to the report.
  #
  # @param [Hash] attributes
  # @option attributes [Symbol] :level The level of the message.
  #
  # @see RubyLint::Report::Entry#initialize
  #
  def add(attributes)
    level = attributes[:level].to_sym

    if valid_level?(level)
      entries << Entry.new(attributes)
    end
  end

  private

  ##
  # @param [Symbol] level
  # @return [TrueClass|FalseClass]
  #
  def valid_level?(level)
    return levels.include?(level)
  end
end

#levelsArray (readonly)

Returns The enabled levels of the report.

Returns:

  • (Array)

    The enabled levels of the report.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
87
88
# File 'lib/ruby-lint/report.rb', line 45

class Report
  attr_reader :entries, :levels

  ##
  # Reporting levels that are always available.
  #
  # @return [Array]
  #
  DEFAULT_LEVELS = [:error, :warning, :info].freeze

  ##
  # @param [Array] levels The reporting levels to enable for this instance.
  #
  def initialize(levels = DEFAULT_LEVELS)
    @levels  = levels.map(&:to_sym)
    @entries = []
  end

  ##
  # Adds a new entry to the report.
  #
  # @param [Hash] attributes
  # @option attributes [Symbol] :level The level of the message.
  #
  # @see RubyLint::Report::Entry#initialize
  #
  def add(attributes)
    level = attributes[:level].to_sym

    if valid_level?(level)
      entries << Entry.new(attributes)
    end
  end

  private

  ##
  # @param [Symbol] level
  # @return [TrueClass|FalseClass]
  #
  def valid_level?(level)
    return levels.include?(level)
  end
end

Instance Method Details

#add(attributes) ⇒ Object

Adds a new entry to the report.

Parameters:

  • attributes (Hash)

Options Hash (attributes):

  • :level (Symbol)

    The level of the message.

See Also:



71
72
73
74
75
76
77
# File 'lib/ruby-lint/report.rb', line 71

def add(attributes)
  level = attributes[:level].to_sym

  if valid_level?(level)
    entries << Entry.new(attributes)
  end
end

#valid_level?(level) ⇒ TrueClass|FalseClass (private)

Parameters:

  • level (Symbol)

Returns:

  • (TrueClass|FalseClass)


85
86
87
# File 'lib/ruby-lint/report.rb', line 85

def valid_level?(level)
  return levels.include?(level)
end