Class: Rouge::FileReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ FileReader

Returns a new instance of FileReader.



7
8
9
# File 'lib/rouge/cli.rb', line 7

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/rouge/cli.rb', line 6

def input
  @input
end

Instance Method Details

#fileObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/rouge/cli.rb', line 11

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

#readObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/rouge/cli.rb', line 22

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