actionmailer 2.3.18 → 3.0.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/CHANGELOG +13 -16
  2. data/README +39 -21
  3. data/lib/action_mailer/adv_attr_accessor.rb +14 -18
  4. data/lib/action_mailer/base.rb +437 -560
  5. data/lib/action_mailer/collector.rb +36 -0
  6. data/lib/action_mailer/delivery_methods.rb +86 -0
  7. data/lib/action_mailer/deprecated_api.rb +139 -0
  8. data/lib/action_mailer/mail_helper.rb +25 -13
  9. data/lib/action_mailer/old_api.rb +248 -0
  10. data/lib/action_mailer/quoting.rb +4 -2
  11. data/lib/action_mailer/railtie.rb +25 -0
  12. data/lib/action_mailer/railties/subscriber.rb +20 -0
  13. data/lib/action_mailer/test_case.rb +4 -6
  14. data/lib/action_mailer/test_helper.rb +0 -1
  15. data/lib/action_mailer/tmail_compat.rb +34 -0
  16. data/lib/action_mailer/version.rb +3 -3
  17. data/lib/action_mailer.rb +25 -30
  18. metadata +41 -161
  19. data/Rakefile +0 -97
  20. data/install.rb +0 -30
  21. data/lib/action_mailer/helpers.rb +0 -113
  22. data/lib/action_mailer/part.rb +0 -107
  23. data/lib/action_mailer/part_container.rb +0 -55
  24. data/lib/action_mailer/utils.rb +0 -7
  25. data/lib/action_mailer/vendor/text-format-0.6.3/text/format.rb +0 -1466
  26. data/lib/action_mailer/vendor/text_format.rb +0 -10
  27. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/Makefile +0 -18
  28. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/address.rb +0 -392
  29. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/attachments.rb +0 -65
  30. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/base64.rb +0 -46
  31. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/compat.rb +0 -41
  32. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/config.rb +0 -67
  33. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/core_extensions.rb +0 -63
  34. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/encode.rb +0 -590
  35. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/header.rb +0 -962
  36. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/index.rb +0 -9
  37. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/interface.rb +0 -1162
  38. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/loader.rb +0 -3
  39. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mail.rb +0 -578
  40. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mailbox.rb +0 -496
  41. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/main.rb +0 -6
  42. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/mbox.rb +0 -3
  43. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/net.rb +0 -250
  44. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/obsolete.rb +0 -132
  45. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/parser.rb +0 -1060
  46. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/parser.y +0 -416
  47. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/port.rb +0 -379
  48. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/quoting.rb +0 -164
  49. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/require_arch.rb +0 -58
  50. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/scanner.rb +0 -49
  51. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/scanner_r.rb +0 -262
  52. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/stringio.rb +0 -280
  53. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/utils.rb +0 -362
  54. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/COPYING +0 -504
  55. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/README +0 -12
  56. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/big5freq.rb +0 -927
  57. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/big5prober.rb +0 -42
  58. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/chardistribution.rb +0 -238
  59. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/charsetgroupprober.rb +0 -112
  60. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/charsetprober.rb +0 -75
  61. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/codingstatemachine.rb +0 -64
  62. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/constants.rb +0 -42
  63. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/escprober.rb +0 -89
  64. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/escsm.rb +0 -244
  65. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/eucjpprober.rb +0 -88
  66. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euckrfreq.rb +0 -596
  67. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euckrprober.rb +0 -42
  68. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euctwfreq.rb +0 -430
  69. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/euctwprober.rb +0 -42
  70. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/gb2312freq.rb +0 -474
  71. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/gb2312prober.rb +0 -42
  72. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/hebrewprober.rb +0 -289
  73. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/jisfreq.rb +0 -570
  74. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/jpcntx.rb +0 -229
  75. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langbulgarianmodel.rb +0 -229
  76. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langcyrillicmodel.rb +0 -330
  77. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langgreekmodel.rb +0 -227
  78. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langhebrewmodel.rb +0 -202
  79. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langhungarianmodel.rb +0 -226
  80. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/langthaimodel.rb +0 -201
  81. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/latin1prober.rb +0 -147
  82. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcharsetprober.rb +0 -89
  83. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcsgroupprober.rb +0 -45
  84. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/mbcssm.rb +0 -542
  85. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sbcharsetprober.rb +0 -124
  86. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sbcsgroupprober.rb +0 -56
  87. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/sjisprober.rb +0 -88
  88. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/universaldetector.rb +0 -168
  89. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet/utf8prober.rb +0 -87
  90. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/vendor/rchardet-1.3/lib/rchardet.rb +0 -67
  91. data/lib/action_mailer/vendor/tmail-1.2.7/tmail/version.rb +0 -39
  92. data/lib/action_mailer/vendor/tmail-1.2.7/tmail.rb +0 -6
  93. data/lib/action_mailer/vendor/tmail.rb +0 -17
  94. data/lib/actionmailer.rb +0 -2
  95. data/test/abstract_unit.rb +0 -62
  96. data/test/asset_host_test.rb +0 -54
  97. data/test/delivery_method_test.rb +0 -51
  98. data/test/fixtures/asset_host_mailer/email_with_asset.html.erb +0 -1
  99. data/test/fixtures/auto_layout_mailer/hello.html.erb +0 -1
  100. data/test/fixtures/auto_layout_mailer/multipart.text.html.erb +0 -1
  101. data/test/fixtures/auto_layout_mailer/multipart.text.plain.erb +0 -1
  102. data/test/fixtures/explicit_layout_mailer/logout.html.erb +0 -1
  103. data/test/fixtures/explicit_layout_mailer/signup.html.erb +0 -1
  104. data/test/fixtures/first_mailer/share.erb +0 -1
  105. data/test/fixtures/helper_mailer/use_example_helper.erb +0 -1
  106. data/test/fixtures/helper_mailer/use_helper.erb +0 -1
  107. data/test/fixtures/helper_mailer/use_helper_method.erb +0 -1
  108. data/test/fixtures/helper_mailer/use_mail_helper.erb +0 -5
  109. data/test/fixtures/helpers/example_helper.rb +0 -5
  110. data/test/fixtures/layouts/auto_layout_mailer.html.erb +0 -1
  111. data/test/fixtures/layouts/auto_layout_mailer.text.erb +0 -1
  112. data/test/fixtures/layouts/spam.html.erb +0 -1
  113. data/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb +0 -1
  114. data/test/fixtures/raw_email +0 -14
  115. data/test/fixtures/raw_email10 +0 -20
  116. data/test/fixtures/raw_email12 +0 -32
  117. data/test/fixtures/raw_email13 +0 -29
  118. data/test/fixtures/raw_email2 +0 -114
  119. data/test/fixtures/raw_email3 +0 -70
  120. data/test/fixtures/raw_email4 +0 -59
  121. data/test/fixtures/raw_email5 +0 -19
  122. data/test/fixtures/raw_email6 +0 -20
  123. data/test/fixtures/raw_email7 +0 -66
  124. data/test/fixtures/raw_email8 +0 -47
  125. data/test/fixtures/raw_email9 +0 -28
  126. data/test/fixtures/raw_email_quoted_with_0d0a +0 -14
  127. data/test/fixtures/raw_email_with_invalid_characters_in_content_type +0 -104
  128. data/test/fixtures/raw_email_with_nested_attachment +0 -100
  129. data/test/fixtures/raw_email_with_partially_quoted_subject +0 -14
  130. data/test/fixtures/second_mailer/share.erb +0 -1
  131. data/test/fixtures/templates/signed_up.erb +0 -3
  132. data/test/fixtures/test_mailer/_subtemplate.text.plain.erb +0 -1
  133. data/test/fixtures/test_mailer/body_ivar.erb +0 -2
  134. data/test/fixtures/test_mailer/custom_templating_extension.text.html.haml +0 -6
  135. data/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml +0 -6
  136. data/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb +0 -1
  137. data/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak +0 -1
  138. data/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb +0 -10
  139. data/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb~ +0 -10
  140. data/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb +0 -2
  141. data/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb +0 -1
  142. data/test/fixtures/test_mailer/included_subtemplate.text.plain.erb +0 -1
  143. data/test/fixtures/test_mailer/rxml_template.builder +0 -2
  144. data/test/fixtures/test_mailer/rxml_template.rxml +0 -2
  145. data/test/fixtures/test_mailer/signed_up.html.erb +0 -3
  146. data/test/fixtures/test_mailer/signed_up_with_url.erb +0 -5
  147. data/test/mail_helper_test.rb +0 -95
  148. data/test/mail_layout_test.rb +0 -123
  149. data/test/mail_render_test.rb +0 -116
  150. data/test/mail_service_test.rb +0 -1145
  151. data/test/quoting_test.rb +0 -105
  152. data/test/test_helper_test.rb +0 -129
  153. data/test/tmail_test.rb +0 -22
  154. data/test/url_test.rb +0 -76
@@ -1,262 +0,0 @@
1
- # encoding: us-ascii
2
- # scanner_r.rb
3
- #
4
- #--
5
- # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
6
- #
7
- # Permission is hereby granted, free of charge, to any person obtaining
8
- # a copy of this software and associated documentation files (the
9
- # "Software"), to deal in the Software without restriction, including
10
- # without limitation the rights to use, copy, modify, merge, publish,
11
- # distribute, sublicense, and/or sell copies of the Software, and to
12
- # permit persons to whom the Software is furnished to do so, subject to
13
- # the following conditions:
14
- #
15
- # The above copyright notice and this permission notice shall be
16
- # included in all copies or substantial portions of the Software.
17
- #
18
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- #
26
- # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
27
- # with permission of Minero Aoki.
28
- #++
29
- #:stopdoc:
30
- require 'tmail/config'
31
-
32
- module TMail
33
-
34
- class TMailScanner
35
-
36
- Version = '1.2.3'
37
- Version.freeze
38
-
39
- MIME_HEADERS = {
40
- :CTYPE => true,
41
- :CENCODING => true,
42
- :CDISPOSITION => true
43
- }
44
-
45
- alnum = 'a-zA-Z0-9'
46
- atomsyms = %q[ _#!$%&`'*+-{|}~^/=? ].strip
47
- tokensyms = %q[ _#!$%&`'*+-{|}~^@. ].strip
48
- atomchars = alnum + Regexp.quote(atomsyms)
49
- tokenchars = alnum + Regexp.quote(tokensyms)
50
- iso2022str = '\e(?!\(B)..(?:[^\e]+|\e(?!\(B)..)*\e\(B'
51
-
52
- eucstr = "(?:[\xa1-\xfe][\xa1-\xfe])+"
53
- sjisstr = "(?:[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc])+"
54
- utf8str = "(?:[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf])+"
55
-
56
- quoted_with_iso2022 = /\A(?:[^\\\e"]+|#{iso2022str})+/n
57
- domlit_with_iso2022 = /\A(?:[^\\\e\]]+|#{iso2022str})+/n
58
- comment_with_iso2022 = /\A(?:[^\\\e()]+|#{iso2022str})+/n
59
-
60
- quoted_without_iso2022 = /\A[^\\"]+/n
61
- domlit_without_iso2022 = /\A[^\\\]]+/n
62
- comment_without_iso2022 = /\A[^\\()]+/n
63
-
64
- PATTERN_TABLE = {}
65
- PATTERN_TABLE['EUC'] =
66
- [
67
- /\A(?:[#{atomchars}]+|#{iso2022str}|#{eucstr})+/n,
68
- /\A(?:[#{tokenchars}]+|#{iso2022str}|#{eucstr})+/n,
69
- quoted_with_iso2022,
70
- domlit_with_iso2022,
71
- comment_with_iso2022
72
- ]
73
- PATTERN_TABLE['SJIS'] =
74
- [
75
- /\A(?:[#{atomchars}]+|#{iso2022str}|#{sjisstr})+/n,
76
- /\A(?:[#{tokenchars}]+|#{iso2022str}|#{sjisstr})+/n,
77
- quoted_with_iso2022,
78
- domlit_with_iso2022,
79
- comment_with_iso2022
80
- ]
81
- PATTERN_TABLE['UTF8'] =
82
- [
83
- /\A(?:[#{atomchars}]+|#{utf8str})+/n,
84
- /\A(?:[#{tokenchars}]+|#{utf8str})+/n,
85
- quoted_without_iso2022,
86
- domlit_without_iso2022,
87
- comment_without_iso2022
88
- ]
89
- PATTERN_TABLE['NONE'] =
90
- [
91
- /\A[#{atomchars}]+/n,
92
- /\A[#{tokenchars}]+/n,
93
- quoted_without_iso2022,
94
- domlit_without_iso2022,
95
- comment_without_iso2022
96
- ]
97
-
98
-
99
- def initialize( str, scantype, comments )
100
- init_scanner str
101
- @comments = comments || []
102
- @debug = false
103
-
104
- # fix scanner mode
105
- @received = (scantype == :RECEIVED)
106
- @is_mime_header = MIME_HEADERS[scantype]
107
-
108
- atom, token, @quoted_re, @domlit_re, @comment_re = PATTERN_TABLE[TMail.KCODE]
109
- @word_re = (MIME_HEADERS[scantype] ? token : atom)
110
- end
111
-
112
- attr_accessor :debug
113
-
114
- def scan( &block )
115
- if @debug
116
- scan_main do |arr|
117
- s, v = arr
118
- printf "%7d %-10s %s\n",
119
- rest_size(),
120
- s.respond_to?(:id2name) ? s.id2name : s.inspect,
121
- v.inspect
122
- yield arr
123
- end
124
- else
125
- scan_main(&block)
126
- end
127
- end
128
-
129
- private
130
-
131
- RECV_TOKEN = {
132
- 'from' => :FROM,
133
- 'by' => :BY,
134
- 'via' => :VIA,
135
- 'with' => :WITH,
136
- 'id' => :ID,
137
- 'for' => :FOR
138
- }
139
-
140
- def scan_main
141
- until eof?
142
- if skip(/\A[\n\r\t ]+/n) # LWSP
143
- break if eof?
144
- end
145
-
146
- if s = readstr(@word_re)
147
- if @is_mime_header
148
- yield [:TOKEN, s]
149
- else
150
- # atom
151
- if /\A\d+\z/ === s
152
- yield [:DIGIT, s]
153
- elsif @received
154
- yield [RECV_TOKEN[s.downcase] || :ATOM, s]
155
- else
156
- yield [:ATOM, s]
157
- end
158
- end
159
-
160
- elsif skip(/\A"/)
161
- yield [:QUOTED, scan_quoted_word()]
162
-
163
- elsif skip(/\A\[/)
164
- yield [:DOMLIT, scan_domain_literal()]
165
-
166
- elsif skip(/\A\(/)
167
- @comments.push scan_comment()
168
-
169
- else
170
- c = readchar()
171
- yield [c, c]
172
- end
173
- end
174
-
175
- yield [false, '$']
176
- end
177
-
178
- def scan_quoted_word
179
- scan_qstr(@quoted_re, /\A"/, 'quoted-word')
180
- end
181
-
182
- def scan_domain_literal
183
- '[' + scan_qstr(@domlit_re, /\A\]/, 'domain-literal') + ']'
184
- end
185
-
186
- def scan_qstr( pattern, terminal, type )
187
- result = ''
188
- until eof?
189
- if s = readstr(pattern) then result << s
190
- elsif skip(terminal) then return result
191
- elsif skip(/\A\\/) then result << readchar()
192
- else
193
- raise "TMail FATAL: not match in #{type}"
194
- end
195
- end
196
- scan_error! "found unterminated #{type}"
197
- end
198
-
199
- def scan_comment
200
- result = ''
201
- nest = 1
202
- content = @comment_re
203
-
204
- until eof?
205
- if s = readstr(content) then result << s
206
- elsif skip(/\A\)/) then nest -= 1
207
- return result if nest == 0
208
- result << ')'
209
- elsif skip(/\A\(/) then nest += 1
210
- result << '('
211
- elsif skip(/\A\\/) then result << readchar()
212
- else
213
- raise 'TMail FATAL: not match in comment'
214
- end
215
- end
216
- scan_error! 'found unterminated comment'
217
- end
218
-
219
- # string scanner
220
-
221
- def init_scanner( str )
222
- @src = str
223
- end
224
-
225
- def eof?
226
- @src.empty?
227
- end
228
-
229
- def rest_size
230
- @src.size
231
- end
232
-
233
- def readstr( re )
234
- if m = re.match(@src)
235
- @src = m.post_match
236
- m[0]
237
- else
238
- nil
239
- end
240
- end
241
-
242
- def readchar
243
- readstr(/\A./)
244
- end
245
-
246
- def skip( re )
247
- if m = re.match(@src)
248
- @src = m.post_match
249
- true
250
- else
251
- false
252
- end
253
- end
254
-
255
- def scan_error!( msg )
256
- raise SyntaxError, msg
257
- end
258
-
259
- end
260
-
261
- end # module TMail
262
- #:startdoc:
@@ -1,280 +0,0 @@
1
- # encoding: utf-8
2
- =begin rdoc
3
-
4
- = String handling class
5
-
6
- =end
7
- #--
8
- # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
9
- #
10
- # Permission is hereby granted, free of charge, to any person obtaining
11
- # a copy of this software and associated documentation files (the
12
- # "Software"), to deal in the Software without restriction, including
13
- # without limitation the rights to use, copy, modify, merge, publish,
14
- # distribute, sublicense, and/or sell copies of the Software, and to
15
- # permit persons to whom the Software is furnished to do so, subject to
16
- # the following conditions:
17
- #
18
- # The above copyright notice and this permission notice shall be
19
- # included in all copies or substantial portions of the Software.
20
- #
21
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
- #
29
- # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
30
- # with permission of Minero Aoki.
31
- #++
32
-
33
- class StringInput#:nodoc:
34
-
35
- include Enumerable
36
-
37
- class << self
38
-
39
- def new( str )
40
- if block_given?
41
- begin
42
- f = super
43
- yield f
44
- ensure
45
- f.close if f
46
- end
47
- else
48
- super
49
- end
50
- end
51
-
52
- alias open new
53
-
54
- end
55
-
56
- def initialize( str )
57
- @src = str
58
- @pos = 0
59
- @closed = false
60
- @lineno = 0
61
- end
62
-
63
- attr_reader :lineno
64
-
65
- def string
66
- @src
67
- end
68
-
69
- def inspect
70
- "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
71
- end
72
-
73
- def close
74
- stream_check!
75
- @pos = nil
76
- @closed = true
77
- end
78
-
79
- def closed?
80
- @closed
81
- end
82
-
83
- def pos
84
- stream_check!
85
- [@pos, @src.size].min
86
- end
87
-
88
- alias tell pos
89
-
90
- def seek( offset, whence = IO::SEEK_SET )
91
- stream_check!
92
- case whence
93
- when IO::SEEK_SET
94
- @pos = offset
95
- when IO::SEEK_CUR
96
- @pos += offset
97
- when IO::SEEK_END
98
- @pos = @src.size - offset
99
- else
100
- raise ArgumentError, "unknown seek flag: #{whence}"
101
- end
102
- @pos = 0 if @pos < 0
103
- @pos = [@pos, @src.size + 1].min
104
- offset
105
- end
106
-
107
- def rewind
108
- stream_check!
109
- @pos = 0
110
- end
111
-
112
- def eof?
113
- stream_check!
114
- @pos > @src.size
115
- end
116
-
117
- def each( &block )
118
- stream_check!
119
- begin
120
- @src.each(&block)
121
- ensure
122
- @pos = 0
123
- end
124
- end
125
-
126
- def gets
127
- stream_check!
128
- if idx = @src.index(?\n, @pos)
129
- idx += 1 # "\n".size
130
- line = @src[ @pos ... idx ]
131
- @pos = idx
132
- @pos += 1 if @pos == @src.size
133
- else
134
- line = @src[ @pos .. -1 ]
135
- @pos = @src.size + 1
136
- end
137
- @lineno += 1
138
-
139
- line
140
- end
141
-
142
- def getc
143
- stream_check!
144
- ch = @src[@pos]
145
- @pos += 1
146
- @pos += 1 if @pos == @src.size
147
- ch
148
- end
149
-
150
- def read( len = nil )
151
- stream_check!
152
- return read_all unless len
153
- str = @src[@pos, len]
154
- @pos += len
155
- @pos += 1 if @pos == @src.size
156
- str
157
- end
158
-
159
- alias sysread read
160
-
161
- def read_all
162
- stream_check!
163
- return nil if eof?
164
- rest = @src[@pos ... @src.size]
165
- @pos = @src.size + 1
166
- rest
167
- end
168
-
169
- def stream_check!
170
- @closed and raise IOError, 'closed stream'
171
- end
172
-
173
- end
174
-
175
-
176
- class StringOutput#:nodoc:
177
-
178
- class << self
179
-
180
- def new( str = '' )
181
- if block_given?
182
- begin
183
- f = super
184
- yield f
185
- ensure
186
- f.close if f
187
- end
188
- else
189
- super
190
- end
191
- end
192
-
193
- alias open new
194
-
195
- end
196
-
197
- def initialize( str = '' )
198
- @dest = str
199
- @closed = false
200
- end
201
-
202
- def close
203
- @closed = true
204
- end
205
-
206
- def closed?
207
- @closed
208
- end
209
-
210
- def string
211
- @dest
212
- end
213
-
214
- alias value string
215
- alias to_str string
216
-
217
- def size
218
- @dest.size
219
- end
220
-
221
- alias pos size
222
-
223
- def inspect
224
- "#<#{self.class}:#{@dest ? 'open' : 'closed'},#{object_id}>"
225
- end
226
-
227
- def print( *args )
228
- stream_check!
229
- raise ArgumentError, 'wrong # of argument (0 for >1)' if args.empty?
230
- args.each do |s|
231
- raise ArgumentError, 'nil not allowed' if s.nil?
232
- @dest << s.to_s
233
- end
234
- nil
235
- end
236
-
237
- def puts( *args )
238
- stream_check!
239
- args.each do |str|
240
- @dest << (s = str.to_s)
241
- @dest << "\n" unless s[-1] == ?\n
242
- end
243
- @dest << "\n" if args.empty?
244
- nil
245
- end
246
-
247
- def putc( ch )
248
- stream_check!
249
- @dest << ch.chr
250
- nil
251
- end
252
-
253
- def printf( *args )
254
- stream_check!
255
- @dest << sprintf(*args)
256
- nil
257
- end
258
-
259
- def write( str )
260
- stream_check!
261
- s = str.to_s
262
- @dest << s
263
- s.size
264
- end
265
-
266
- alias syswrite write
267
-
268
- def <<( str )
269
- stream_check!
270
- @dest << str.to_s
271
- self
272
- end
273
-
274
- private
275
-
276
- def stream_check!
277
- @closed and raise IOError, 'closed stream'
278
- end
279
-
280
- end