Class: Isort::FileSorter

Inherits:
Object
  • Object
show all
Defined in:
lib/isort.rb

Overview

FileSorter provides the public API for sorting imports in a Ruby file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, options = {}) ⇒ FileSorter

Returns a new instance of FileSorter.



41
42
43
44
45
46
47
48
49
50
# File 'lib/isort.rb', line 41

def initialize(file_path, options = {})
  @file_path = file_path
  @options = {
    check: false,
    diff: false,
    atomic: false,
    quiet: false
  }.merge(options)
  @processor = FileProcessor.new(file_path, @options)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



39
40
41
# File 'lib/isort.rb', line 39

def file_path
  @file_path
end

Instance Method Details

#checkObject

Check if file would change (dry-run)



66
67
68
# File 'lib/isort.rb', line 66

def check
  @processor.check
end

#diffObject

Get diff of changes without applying



71
72
73
# File 'lib/isort.rb', line 71

def diff
  @processor.diff
end

#sort_and_format_importsObject

Sort and format imports in the file Returns true if changes were made (or would be made in check mode)



54
55
56
57
58
59
60
61
62
63
# File 'lib/isort.rb', line 54

def sort_and_format_imports
  @processor.process
rescue Errno::ENOENT
  raise
rescue ExistingSyntaxErrors, IntroducedSyntaxErrors, FileSkipped
  raise
rescue StandardError => e
  puts "An error occurred: #{e.message}" unless @options[:quiet]
  raise
end

#sort_importsObject

Deprecated.

Use sort_and_format_imports instead

Legacy method for backward compatibility



77
78
79
# File 'lib/isort.rb', line 77

def sort_imports
  sort_and_format_imports
end