lib/ya2yaml.rb in ya2yaml-0.26 vs lib/ya2yaml.rb in ya2yaml-0.29.2

- old
+ new

@@ -1,16 +1,17 @@ -#!/usr/bin/env ruby +# encoding: UTF-8 -# $Id: ya2yaml.rb,v 0.26 2007-01-19 20:42:42+09 funai Exp funai $ +# $Id: ya2yaml.rb,v 0.29 2009/02/09 09:01:30 funai Exp funai $ # # Author:: Akira FUNAI # Copyright:: Copyright (c) 2006 Akira FUNAI # License:: MIT License class Ya2YAML - def initialize(options = {}) + def initialize(opts = {}) + options = opts.dup options[:indent_size] = 2 if options[:indent_size].to_i <= 0 options[:minimum_block_length] = 0 if options[:minimum_block_length].to_i <= 0 options.update( { :printable_with_syck => true, @@ -21,28 +22,28 @@ @options = options end def _ya2yaml(obj) - throw 'set $KCODE to "UTF8".' if $KCODE != 'UTF8' + throw 'set $KCODE to "UTF8".' if (RUBY_VERSION < '1.9.0') && ($KCODE != 'UTF8') '--- ' + emit(obj,1) + "\n" end private def emit(obj,level) - case obj.class.to_s - when 'Array' + case obj + when Array if (obj.length == 0) '[]' else indent = "\n" + s_indent(level - 1) obj.collect {|o| indent + '- ' + emit(o,level + 1) }.join('') end - when 'Hash' + when Hash if (obj.length == 0) '{}' else indent = "\n" + s_indent(level - 1) hash_order = @options[:hash_order] @@ -67,34 +68,34 @@ indent + '? ' + key + indent + ': ' + emit(obj[k],level + 1) end }.join('') end - when 'NilClass' + when NilClass '~' - when 'String' + when String emit_string(obj,level) - when 'TrueClass','FalseClass' + when TrueClass,FalseClass obj.to_s - when 'Fixnum','Bignum','Float' + when Fixnum,Bignum,Float obj.to_s - when 'Date' + when Date obj.to_s - when 'Time' + when Time offset = obj.gmtoff off_hm = sprintf( '%+.2d:%.2d', (offset / 3600.0).to_i, (offset % 3600.0) / 60 ) u_sec = (obj.usec != 0) ? sprintf(".%.6d",obj.usec) : '' obj.strftime("%Y-%m-%d %H:%M:%S#{u_sec} #{off_hm}") - when 'Symbol' - '!ruby/symbol ' + obj.to_s - when 'Range' + when Symbol + '!ruby/symbol ' + emit_string(obj.to_s,level) + when Range '!ruby/range ' + obj.to_s - when 'Regexp' + when Regexp '!ruby/regexp ' + obj.inspect else case when obj.is_a?(Struct) struct_members = {} @@ -103,11 +104,11 @@ emit(struct_members,level + 1) else # serialized as a generic object object_members = {} obj.instance_variables.each{|k,v| - object_members[k.sub(/^@/,'')] = obj.instance_variable_get(k) + object_members[k.to_s.sub(/^@/,'')] = obj.instance_variable_get(k) } '!ruby/object:' + obj.class.to_s + ' ' + emit(object_members,level + 1) end end @@ -181,23 +182,26 @@ base64 = [str].pack('m') '!binary |' + indent + base64.gsub(/\n(?!\z)/,indent) end def string_type(str) + if str.respond_to?(:valid_encoding?) && !str.valid_encoding? + return false,false,false,false + end (ucs_codes = str.unpack('U*')) rescue ( # ArgumentError -> binary data return false,false,false,false ) if ( @options[:printable_with_syck] && str =~ /\A#{REX_ANY_LB}* | #{REX_ANY_LB}*\z|#{REX_ANY_LB}{2}\z/ ) # detour Syck bug - return true,false,is_one_line?(str),false + return true,false,nil,false end ucs_codes.each {|ucs_code| - return true,false,is_one_line?(str),false unless is_printable?(ucs_code) + return true,false,nil,false unless is_printable?(ucs_code) } return true,true,is_one_line?(str),is_one_plain_line?(str) end def is_printable?(ucs_code) @@ -247,10 +251,12 @@ when ESCAPE_SEQ[c] ESCAPE_SEQ[c] when is_printable?(ucs_code) c when @options[:escape_as_utf8] - '\\x' + c.unpack('H2' * c.size).join('\\x') + c.respond_to?(:bytes) ? + c.bytes.collect {|b| '\\x%.2x' % b }.join : + '\\x' + c.unpack('H2' * c.size).join('\\x') when ucs_code == 0x2028 || ucs_code == 0x2029 ESCAPE_SEQ_LB[c] when ucs_code <= 0x7f sprintf('\\x%.2x',ucs_code) when ucs_code <= 0xffff