Class: RubyLsp::RbsRails::FileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/rbs_rails/file_writer.rb

Overview

To avoid unnecessary type reloading by type checkers and other utilities, FileWriter modifies the target file only if its content has been changed.

See github.com/pocke/rbs_rails/pull/346

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileWriter

Returns a new instance of FileWriter.



13
14
15
# File 'lib/ruby_lsp/rbs_rails/file_writer.rb', line 13

def initialize(path) #: void
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

: Pathname



10
11
12
# File 'lib/ruby_lsp/rbs_rails/file_writer.rb', line 10

def path
  @path
end

Instance Method Details

#write(content) ⇒ Object

: void



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_lsp/rbs_rails/file_writer.rb', line 17

def write(content) #: void
  original_content = begin
    path.read
  rescue StandardError
    nil
  end

  return unless original_content != content

  path.write(content)
end