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:



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

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

Instance Attribute Details

#streamObject (readonly)

:nodoc:



165
166
167
# File 'lib/ri_cal/component/calendar.rb', line 165

def stream
  @stream
end

Instance Method Details

#fold(string) ⇒ Object

:nodoc:



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

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:



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

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

#stringObject

:nodoc:



170
171
172
# File 'lib/ri_cal/component/calendar.rb', line 170

def string #:nodoc:
  stream.string
end

#utf8_safe_split(string, n) ⇒ Object



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

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)


189
190
191
# File 'lib/ri_cal/component/calendar.rb', line 189

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