Class: Omnizip::IO::Sink

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/io/source.rb

Overview

Polymorphic adapter for "things we can write bytes to".

Adapters:

  • +IO+/+StringIO+/+Tempfile+ → wrapped as-is
  • String → treated as a file path; writes via File.binwrite
  • Object responding to :write → delegated to

Defined Under Namespace

Classes: PathSink

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Sink

Returns a new instance of Sink.



113
114
115
# File 'lib/omnizip/io/source.rb', line 113

def initialize(io)
  @io = io
end

Class Method Details

.for(output) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/omnizip/io/source.rb', line 98

def self.for(output)
  case output
  when ::IO, ::StringIO, ::Tempfile then new(output)
  when String then PathSink.new(output)
  else
    # allowed: boundary validation; duck-typing ends at this adapter
    unless output.respond_to?(:write)
      raise ArgumentError,
            "Cannot adapt #{output.inspect} to Omnizip::IO::Sink"
    end

    new(output)
  end
end

Instance Method Details

#closeObject



121
122
123
124
# File 'lib/omnizip/io/source.rb', line 121

def close
  # allowed: wrapped object may be a write-only duck with no close
  @io.close if @io.respond_to?(:close)
end

#write(data) ⇒ Object



117
118
119
# File 'lib/omnizip/io/source.rb', line 117

def write(data)
  @io.write(data)
end