Class: Stash::Sword::SequenceIO

Inherits:
Object
  • Object
show all
Defined in:
lib/stash/sword/sequence_io.rb

Overview

A read-only IO-like that concatenates a sequence of strings or IOs.

Instance Method Summary collapse

Constructor Details

#initialize(inputs) ⇒ SequenceIO

Creates a new Stash::Sword::SequenceIO concatenating the specified input sources. Strings are wrapped internally as StringIO.

Parameters:

  • inputs (Enumerable<String, IO>)

    an array of strings and/or IOs to concatenate



13
14
15
16
17
18
19
# File 'lib/stash/sword/sequence_io.rb', line 13

def initialize(inputs)
  inputs  = [inputs] unless inputs.respond_to?(:[]) && inputs.respond_to?(:map)
  @inputs = to_ios(inputs)
  binmode if any_binmode(@inputs)
  @index = 0
  @input = @inputs[index] unless inputs.empty?
end

Instance Method Details

#binmode



36
37
38
39
40
41
42
43
# File 'lib/stash/sword/sequence_io.rb', line 36

def binmode
  return self if binmode?
  inputs.each do |input|
    input.binmode if input.respond_to?(:binmode)
  end
  @binmode = true
  self
end

#binmode?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/stash/sword/sequence_io.rb', line 45

def binmode?
  @binmode
end

#close



49
50
51
# File 'lib/stash/sword/sequence_io.rb', line 49

def close
  next_input! until input.nil?
end

#closed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/stash/sword/sequence_io.rb', line 53

def closed?
  input.nil? && index >= inputs.length
end

#read(length = nil, outbuf = nil)



28
29
30
31
32
33
34
# File 'lib/stash/sword/sequence_io.rb', line 28

def read(length = nil, outbuf = nil)
  # use <= instead of == to get around https://github.com/bbatsov/rubocop/issues/3131
  return nil if size <= 0
  outbuf = outbuf ? outbuf.clear : ''
  length ? read_segment(length, outbuf) : read_fully(outbuf)
  outbuf
end

#size



21
22
23
24
25
26
# File 'lib/stash/sword/sequence_io.rb', line 21

def size
  @size ||= inputs.inject(0) do |sum, input|
    raise "input #{input} does not respond to :size" unless input.respond_to?(:size)
    sum + input.size
  end
end