Class: StringInput

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_mailer/vendor/tmail/stringio.rb

Overview

stringio.rb

Copyright © 1999-2003 Minero Aoki <[email protected]>

This program is free software. You can distribute/modify this program under the terms of the GNU Lesser General Public License version 2 or later.

Id: stringio.rb,v 1.10 2003/04/27 22:02:14 aamine Exp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#sort_by

Constructor Details

#initialize(str) ⇒ StringInput

Returns a new instance of StringInput.



36
37
38
39
40
41
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 36

def initialize( str )
  @src = str
  @pos = 0
  @closed = false
  @lineno = 0
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



43
44
45
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 43

def lineno
  @lineno
end

Class Method Details

.new(str) ⇒ Object Also known as: open



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 19

def new( str )
  if block_given?
    begin
      f = super
      yield f
    ensure
      f.close if f
    end
  else
    super
  end
end

Instance Method Details

#closeObject



53
54
55
56
57
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 53

def close
  stream_check!
  @pos = nil
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 59

def closed?
  @closed
end

#each(&block) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 97

def each( &block )
  stream_check!
  begin
    @src.each(&block)
  ensure
    @pos = 0
  end
end

#eof?Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 92

def eof?
  stream_check!
  @pos > @src.size
end

#getcObject



122
123
124
125
126
127
128
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 122

def getc
  stream_check!
  ch = @src[@pos]
  @pos += 1
  @pos += 1 if @pos == @src.size
  ch
end

#getsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 106

def gets
  stream_check!
  if idx = @src.index(?\n, @pos)
    idx += 1  # "\n".size
    line = @src[ @pos ... idx ]
    @pos = idx
    @pos += 1 if @pos == @src.size
  else
    line = @src[ @pos .. -1 ]
    @pos = @src.size + 1
  end
  @lineno += 1

  line
end

#inspectObject



49
50
51
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 49

def inspect
  "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
end

#posObject Also known as: tell



63
64
65
66
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 63

def pos
  stream_check!
  [@pos, @src.size].min
end

#read(len = nil) ⇒ Object Also known as: sysread



130
131
132
133
134
135
136
137
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 130

def read( len = nil )
  stream_check!
  return read_all unless len
  str = @src[@pos, len]
  @pos += len
  @pos += 1 if @pos == @src.size
  str
end

#read_allObject



141
142
143
144
145
146
147
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 141

def read_all
  stream_check!
  return nil if eof?
  rest = @src[@pos ... @src.size]
  @pos = @src.size + 1
  rest
end

#rewindObject



87
88
89
90
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 87

def rewind
  stream_check!
  @pos = 0
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 70

def seek( offset, whence = IO::SEEK_SET )
  stream_check!
  case whence
  when IO::SEEK_SET
    @pos = offset
  when IO::SEEK_CUR
    @pos += offset
  when IO::SEEK_END
    @pos = @src.size - offset
  else
    raise ArgumentError, "unknown seek flag: #{whence}"
  end
  @pos = 0 if @pos < 0
  @pos = [@pos, @src.size + 1].min
  offset
end

#stream_check!Object



149
150
151
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 149

def stream_check!
  @closed and raise IOError, 'closed stream'
end

#stringObject



45
46
47
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 45

def string
  @src
end