Class: RiCal::Component::Calendar::FoldingStream
- Defined in:
- lib/ri_cal/component/calendar.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#stream ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#fold(string) ⇒ Object
:nodoc:.
-
#initialize(stream) ⇒ FoldingStream
constructor
:nodoc:.
-
#puts(*strings) ⇒ Object
:nodoc:.
-
#string ⇒ Object
:nodoc:.
- #utf8_safe_split(string, n) ⇒ Object
- #valid_utf8?(string) ⇒ Boolean
Constructor Details
#initialize(stream) ⇒ FoldingStream
:nodoc:
164 165 166 |
# File 'lib/ri_cal/component/calendar.rb', line 164 def initialize(stream) #:nodoc: @stream = stream || StringIO.new end |
Instance Attribute Details
#stream ⇒ Object (readonly)
:nodoc:
163 164 165 |
# File 'lib/ri_cal/component/calendar.rb', line 163 def stream @stream end |
Instance Method Details
#fold(string) ⇒ Object
:nodoc:
207 208 209 210 211 212 213 214 |
# File 'lib/ri_cal/component/calendar.rb', line 207 def fold(string) #:nodoc: line, remainder = *utf8_safe_split(string, 73) stream.puts(line) while remainder line, remainder = *utf8_safe_split(remainder, 72) stream.puts(" #{line}") end end |
#puts(*strings) ⇒ Object
:nodoc:
216 217 218 219 220 221 222 |
# File 'lib/ri_cal/component/calendar.rb', line 216 def puts(*strings) #:nodoc: strings.each do |string| string.split("\n").each do |line| fold(line) end end end |
#string ⇒ Object
:nodoc:
168 169 170 |
# File 'lib/ri_cal/component/calendar.rb', line 168 def string #:nodoc: stream.string end |
#utf8_safe_split(string, n) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ri_cal/component/calendar.rb', line 173 def utf8_safe_split(string, n) if string.bytesize <= n [string, nil] else bytes = string.bytes.to_a while (128..191).include?(bytes[n]) n = n - 1 end before = bytes[0,n] after = bytes[n..-1] [before.pack("C*").force_encoding("utf-8"), after.empty? ? nil : after.pack("C*").force_encoding("utf-8")] end end |
#valid_utf8?(string) ⇒ Boolean
187 188 189 |
# File 'lib/ri_cal/component/calendar.rb', line 187 def valid_utf8?(string) string.unpack("U") rescue nil end |