Skip to content

Instantly share code, notes, and snippets.

@cmer
Created November 15, 2011 00:13
Show Gist options
  • Save cmer/1365659 to your computer and use it in GitHub Desktop.
Save cmer/1365659 to your computer and use it in GitHub Desktop.
##
## TAKEN FROM https://raw.github.com/benhoskings/delayed_job/c3a28f9714568c887271e492dd8c6710965278e8/lib/delayed/yaml_ext.rb
## Solves problems serializing strategies to yaml
##
# 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 if respond_to?(:to_yaml) && method(:to_yaml).owner == Class # 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment