Class: Khaleesi::CLI::FileReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ FileReader

Returns a new instance of FileReader.



590
591
592
# File 'lib/khaleesi/cli.rb', line 590

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



589
590
591
# File 'lib/khaleesi/cli.rb', line 589

def input
  @input
end

Instance Method Details

#fileObject



594
595
596
597
598
599
600
601
602
603
# File 'lib/khaleesi/cli.rb', line 594

def file
  case input
    when '-'
      $stdin
    when String
      File.new(input)
    when ->(i){ i.respond_to? :read }
      input
  end
end

#readObject



605
606
607
608
609
610
611
612
613
614
# File 'lib/khaleesi/cli.rb', line 605

def read
  @read ||= begin
    file.read
  rescue => e
    $stderr.puts "unable to open #{input}: #{e.message}"
    exit 1
  ensure
    file.close
  end
end