forked from tobi/delayed_job
/
yaml_ext.rb
46 lines (37 loc) · 968 Bytes
/
yaml_ext.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# These extensions allow properly serializing and autoloading of
# Classes, Modules and Structs
require 'yaml'
class Module
yaml_as "tag:ruby.yaml.org,2002:module"
def self.yaml_new(klass, tag, val)
val.constantize
end
def to_yaml( opts = {} )
YAML::quick_emit( nil, opts ) { |out|
out.scalar(taguri, self.name, :plain)
}
end
def yaml_tag_read_class(name)
# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
name.constantize
name
end
end
class Class
yaml_as "tag:ruby.yaml.org,2002:class"
remove_method :to_yaml # use Module's to_yaml
end
class Struct
def self.yaml_tag_read_class(name)
# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
name.constantize
"Struct::#{ name }"
end
end
class Object
def to_delayed_yaml
to_yaml
end
end