Class: Range

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.yaml_new(klass, tag, val) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/syck/rubytypes.rb', line 210

def Range.yaml_new( klass, tag, val )
    inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
    opts = {}
    if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
        r1, rdots, r2 = $1, $2, $3
        opts = {
            'begin' => YAML.load( "--- #{r1}" ),
            'end' => YAML.load( "--- #{r2}" ),
            'excl' => rdots.length == 3
        }
        val = {}
    elsif Hash === val
        opts['begin'] = val.delete('begin')
        opts['end'] = val.delete('end')
        opts['excl'] = val.delete('excl')
    end
    if Hash === opts
        r = YAML::object_maker( klass, {} )
        # Thank you, NaHi
        Range.instance_method(:initialize).
              bind(r).
              call( opts['begin'], opts['end'], opts['excl'] )
        val.each { |k,v| r.instance_variable_set( k, v ) }
        r
    else
        raise YAML::TypeError, "Invalid Range: " + val.inspect
    end
end

Instance Method Details

#to_yaml(opts = {}) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/syck/rubytypes.rb', line 238

def to_yaml( opts = {} )
       return super unless YAML::ENGINE.syck?
	YAML::quick_emit( self, opts ) do |out|
           # if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
           #   self.end.is_complex_yaml? or self.end.respond_to? :to_str or
           #   not to_yaml_properties.empty?
               out.map( taguri, to_yaml_style ) do |map|
                   map.add( 'begin', self.begin )
                   map.add( 'end', self.end )
                   map.add( 'excl', self.exclude_end? )
                   to_yaml_properties.each do |m|
                       map.add( m, instance_variable_get( m ) )
                   end
               end
           # else
           #     out.scalar( taguri ) do |sc|
           #         sc.embed( self.begin )
           #         sc.concat( self.exclude_end? ? "..." : ".." )
           #         sc.embed( self.end )
           #     end
           # end
       end
end