File: abstract_output_stream.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (53 lines) | stat: -rw-r--r-- 1,247 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

module Zip
  module IOExtras # :nodoc:
    # Implements many of the output convenience methods of IO.
    # relies on <<
    module AbstractOutputStream # :nodoc:
      include FakeIO

      def write(data)
        self << data
        data.to_s.bytesize
      end

      def print(*params)
        self << params.join

        # Deflate doesn't like `nil`s or empty strings!
        unless $OUTPUT_RECORD_SEPARATOR.nil? || $OUTPUT_RECORD_SEPARATOR.empty?
          self << $OUTPUT_RECORD_SEPARATOR
        end

        nil
      end

      def printf(a_format_string, *params)
        self << format(a_format_string, *params)
        nil
      end

      def putc(an_object)
        self << case an_object
                when Integer
                  an_object.chr
                when String
                  an_object
                else
                  raise TypeError, 'putc: Only Integer and String supported'
                end
        an_object
      end

      def puts(*params)
        params << "\n" if params.empty?
        params.flatten.each do |element|
          val = element.to_s
          self << val
          self << "\n" unless val[-1, 1] == "\n"
        end
      end
    end
  end
end