Class: StringInput

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb

Overview

– 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

#reject, #sort_by

Constructor Details

#initialize(str) ⇒ StringInput

Returns a new instance of StringInput.



56
57
58
59
60
61
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 56

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

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



63
64
65
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 63

def lineno
  @lineno
end

Class Method Details

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 39

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



73
74
75
76
77
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 73

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

#closed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 79

def closed?
  @closed
end

#each(&block) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 117

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

#eof?Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 112

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

#getcObject



142
143
144
145
146
147
148
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 142

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

#getsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 126

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



69
70
71
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 69

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

#posObject Also known as: tell



83
84
85
86
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 83

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

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



150
151
152
153
154
155
156
157
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 150

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



161
162
163
164
165
166
167
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 161

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

#rewindObject



107
108
109
110
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 107

def rewind
  stream_check!
  @pos = 0
end

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 90

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



169
170
171
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 169

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

#stringObject



65
66
67
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/stringio.rb', line 65

def string
  @src
end