In Files

  • syck/lib/syck/rubytypes.rb

String

Public Class Methods

yaml_new( klass, tag, val ) click to toggle source
 
               # File syck/lib/syck/rubytypes.rb, line 153
def String.yaml_new( klass, tag, val )
    val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
    val = { 'str' => val } if String === val
    if Hash === val
        s = klass.allocate
        # Thank you, NaHi
        String.instance_method(:initialize).
              bind(s).
              call( val.delete( 'str' ) )
        val.each { |k,v| s.instance_variable_set( k, v ) }
        s
    else
        raise YAML::TypeError, "Invalid String: " + val.inspect
    end
end
            

Public Instance Methods

is_binary_data?() click to toggle source
 
               # File syck/lib/syck/rubytypes.rb, line 150
def is_binary_data?
    self.count("\x00-\x7F", "^ -~\t\r\n").fdiv(self.size) > 0.3 || self.index("\x00") unless self.empty?
end
            
is_complex_yaml?() click to toggle source
 
               # File syck/lib/syck/rubytypes.rb, line 147
def is_complex_yaml?
    to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
end
            
to_yaml( opts = {} ) click to toggle source
 
               # File syck/lib/syck/rubytypes.rb, line 168
def to_yaml( opts = {} )
return super unless YAML::ENGINE.syck?
        YAML::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
    if is_binary_data?
        out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
    elsif to_yaml_properties.empty?
        out.scalar( taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style )
    else
        out.map( taguri, to_yaml_style ) do |map|
            map.add( 'str', "#{self}" )
            to_yaml_properties.each do |m|
                map.add( m, instance_variable_get( m ) )
            end
        end
    end
end
end