Class: Ned::Read

Inherits:
Command show all
Defined in:
lib/ned/commands/read.rb

Instance Method Summary collapse

Methods inherited from Command

config, description, #execute_internal, help, #load_lines, long_name, option_parser, #options, #parse, #peek, require_all, #require_all, require_all?, #require_all?, short_name

Constructor Details

#initialize(inputs) ⇒ Read

Returns a new instance of Read.



3
4
5
# File 'lib/ned/commands/read.rb', line 3

def initialize(inputs)
  @inputs = inputs
end

Instance Method Details

#executeObject



7
8
9
10
11
12
# File 'lib/ned/commands/read.rb', line 7

def execute
  line = @next || next_line
  @next = next_line
  line.ensure_trailing_newline if @next
  line.nil? ? nil : line
end

#execute_allObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ned/commands/read.rb', line 27

def execute_all
  lines = []
  current = @inputs.shift
  while current
    lines.concat(current.readlines)
    current.close
    current = @inputs.shift
    lines[-1].ensure_trailing_newline if !!current && lines[-1]
  end

  lines
end

#next_lineObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ned/commands/read.rb', line 14

def next_line
  @current ||= @inputs.shift
  return unless @current

  line = @current.gets("\n")
  return line if line

  @current.close
  @current = nil

  next_line
end