Class: RiCal::Component::Calendar::FoldingStream

Inherits:
Object
  • Object
show all
Defined in:
lib/ri_cal/component/calendar.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ FoldingStream

:nodoc:



168
169
170
# File 'lib/ri_cal/component/calendar.rb', line 168

def initialize(stream) #:nodoc:
  @stream = stream || StringIO.new
end

Instance Attribute Details

#streamObject (readonly)

:nodoc:



167
168
169
# File 'lib/ri_cal/component/calendar.rb', line 167

def stream
  @stream
end

Instance Method Details

#fold(string) ⇒ Object

:nodoc:



211
212
213
214
215
216
217
218
# File 'lib/ri_cal/component/calendar.rb', line 211

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:



220
221
222
223
224
225
226
# File 'lib/ri_cal/component/calendar.rb', line 220

def puts(*strings) #:nodoc:
  strings.each do |string|
    string.split("\n").each do |line|
      fold(line)
    end
  end
end

#stringObject

:nodoc:



172
173
174
# File 'lib/ri_cal/component/calendar.rb', line 172

def string #:nodoc:
  stream.string
end

#utf8_safe_split(string, n) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ri_cal/component/calendar.rb', line 177

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

Returns:

  • (Boolean)


191
192
193
# File 'lib/ri_cal/component/calendar.rb', line 191

def valid_utf8?(string)
  string.unpack("U") rescue nil
end