Class: String

Inherits:
Object show all
Defined in:
lib/yaml/rubytypes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.yaml_new(klass, tag, val) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/yaml/rubytypes.rb', line 149

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

Instance Method Details

#is_binary_data?Boolean

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/yaml/rubytypes.rb', line 145

def is_binary_data?
    # It's binary if it's got a null.
    index(0)
end

#is_complex_yaml?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/yaml/rubytypes.rb', line 142

def is_complex_yaml?
    to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
end

#to_yaml(opts = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/yaml/rubytypes.rb', line 164

def to_yaml( opts = {} )
	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