Class: Kapusta::Formatter
- Inherits:
-
Object
- Object
- Kapusta::Formatter
- Defined in:
- lib/kapusta/formatter.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- MAX_WIDTH =
80- INDENT =
2- STDIN_PATH =
'-'- PIPELINE_FORMS =
%w[-> ->> -?> -?>> doto].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ Formatter
constructor
A new instance of Formatter.
- #run ⇒ Object
Constructor Details
#initialize(argv) ⇒ Formatter
Returns a new instance of Formatter.
17 18 19 20 21 22 |
# File 'lib/kapusta/formatter.rb', line 17 def initialize(argv) @mode = :stdout @files = [] @version = false parse_args(argv) end |
Class Method Details
.format(source, path: nil) ⇒ Object
13 14 15 |
# File 'lib/kapusta/formatter.rb', line 13 def self.format(source, path: nil) new([]).send(:format_source, source, path) end |
Instance Method Details
#run ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kapusta/formatter.rb', line 24 def run if @version puts "kapfmt #{Kapusta::VERSION}" return 0 end validate_args! formatted = @files.map do |path| original = read_source(path) validate_kapusta_source(original, path) [path, original, format_source(original, path)] end case @mode when :stdout $stdout.write(formatted.first[2]) when :fix formatted.each do |path, _original, rewritten| raise Error, 'Cannot use --fix with stdin (-).' if stdin_path?(path) File.write(path, rewritten) end when :check dirty = formatted.reject { |_path, original, rewritten| original == rewritten } dirty.each do |path, _original, _rewritten| warn "Not formatted: #{path}" end return 1 unless dirty.empty? end 0 rescue Kapusta::Error => e warn e.formatted 1 end |