Class: StringInput

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

Overview

stringio.rb

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

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note: Originally licensed under LGPL v2+. Using MIT license for Rails with permission of Minero Aoki. ++

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.



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

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

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



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

def lineno
  @lineno
end

Class Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 36

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



70
71
72
73
74
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 70

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

#closed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 76

def closed?
  @closed
end

#each(&block) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 114

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

#eof?Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 109

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

#getcObject



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

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

#getsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 123

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



66
67
68
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 66

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

#posObject Also known as: tell



80
81
82
83
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 80

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

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



147
148
149
150
151
152
153
154
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 147

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



158
159
160
161
162
163
164
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 158

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

#rewindObject



104
105
106
107
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 104

def rewind
  stream_check!
  @pos = 0
end

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 87

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



166
167
168
# File 'lib/action_mailer/vendor/tmail/stringio.rb', line 166

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

#stringObject



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

def string
  @src
end