class Framecurve::Serializer
Writes out a Curve object to the passed IO
Public Instance Methods
serialize(io, curve)
click to toggle source
Serialize the passed curve into io. Will use the materialized curve version. Will write the file with CRLF linebreaks instead of LF. Also, if the passed Curve object does not contain a preamble (URL and column headers) they will be added automatically
# File lib/framecurve/serializer.rb, line 8 def serialize(io, curve) write_preamble(io) unless curve_has_preamble?(curve) curve.each do | record | io.write("%s\r\n" % record) end end
validate_and_serialize(io, curve)
click to toggle source
Serialize the passed curve into io and raise an exception
# File lib/framecurve/serializer.rb, line 16 def validate_and_serialize(io, curve) v = Framecurve::Validator.new v.validate(curve) raise Framecurve::Malformed, "Will not serialize a malformed curve: #{v.errors.join(', ')}" if v.any_errors? serialize(io, curve) end
Private Instance Methods
curve_has_preamble?(curve)
click to toggle source
# File lib/framecurve/serializer.rb, line 30 def curve_has_preamble?(curve) first_comment, second_comment = curve[0], curve[-1] return false unless first_comment && second_comment return false unless (first_comment.comment? && second_comment.comment?) return false unless first_comment.text.include?("http://framecurve.org") return false unless second_comment.text.include?("at_frame\tuse_frame_of_source") true end
write_preamble(io)
click to toggle source
# File lib/framecurve/serializer.rb, line 25 def write_preamble(io) io.write("# http://framecurve.org/specification-v1\n") io.write("# at_frame\tuse_frame_of_source\n") end