Class: RuboCop::CachedData Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/cached_data.rb |
Overview
Converts RuboCop objects to and from the serialization format JSON.
Class Method Summary
- .new(filename) ⇒ CachedData constructor Internal use only
Instance Method Summary
- #from_json(text) Internal use only
- #to_json(offenses) Internal use only
-
#deserialize_offenses(offenses)
private
Internal use only
Restore an offense object loaded from a JSON file.
- #location_from_source_buffer(offense, source_buffer) private Internal use only
- #message(offense) private Internal use only
- #serialize_offense(offense) private Internal use only
Instance Method Details
#deserialize_offenses(offenses) (private)
Restore an offense object loaded from a JSON file.
# File 'lib/rubocop/cached_data.rb', line 47
def deserialize_offenses(offenses) source_buffer = Parser::Source::Buffer.new(@filename) source_buffer.source = File.read(@filename, encoding: Encoding::UTF_8) offenses.map! do |o| location = location_from_source_buffer(o, source_buffer) Cop::Offense.new(o['severity'], location, o['message'], o['cop_name'], o['status'].to_sym) end end
#from_json(text)
[ GitHub ]# File 'lib/rubocop/cached_data.rb', line 13
def from_json(text) deserialize_offenses(JSON.parse(text)) end
#location_from_source_buffer(offense, source_buffer) (private)
[ GitHub ]# File 'lib/rubocop/cached_data.rb', line 56
def location_from_source_buffer(offense, source_buffer) begin_pos = offense['location']['begin_pos'] end_pos = offense['location']['end_pos'] if begin_pos.zero? && end_pos.zero? Cop::Offense::NO_LOCATION else Parser::Source::Range.new(source_buffer, begin_pos, end_pos) end end
#message(offense) (private)
[ GitHub ]# File 'lib/rubocop/cached_data.rb', line 40
def (offense) # JSON.dump will fail if the offense message contains text which is not # valid UTF-8 offense. .dup.force_encoding(::Encoding::UTF_8).scrub end
#serialize_offense(offense) (private)
[ GitHub ]# File 'lib/rubocop/cached_data.rb', line 23
def serialize_offense(offense) status = :uncorrected if %i[corrected corrected_with_todo].include?(offense.status) { # Calling #to_s here ensures that the serialization works when using # other json serializers such as Oj. Some of these gems do not call # #to_s implicitly. severity: offense.severity.to_s, location: { begin_pos: offense.location.begin_pos, end_pos: offense.location.end_pos }, message: (offense), cop_name: offense.cop_name, status: status || offense.status } end
#to_json(offenses)
[ GitHub ]# File 'lib/rubocop/cached_data.rb', line 17
def to_json(offenses) JSON.dump(offenses.map { |o| serialize_offense(o) }) end