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.



13
14
15
# File 'lib/rouge/cli.rb', line 13

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



12
13
14
# File 'lib/rouge/cli.rb', line 12

def input
  @input
end

Instance Method Details

#fileObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/rouge/cli.rb', line 17

def file
  case input
  when '-'
    IO.new($stdin.fileno, 'rt:bom|utf-8')
  when String
    File.new(input, 'rt:bom|utf-8')
  when ->(i){ i.respond_to? :read }
    input
  end
end

#readObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/rouge/cli.rb', line 28

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