forked from collectiveidea/delayed_job
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out psych and syck yaml extensions to their own files.
- Loading branch information
Showing
3 changed files
with
71 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Psych | ||
module Visitors | ||
class YAMLTree | ||
def visit_Class(klass) | ||
tag = ['!ruby/class', klass.name].compact.join(':') | ||
register(klass, @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)) | ||
@emitter.end_mapping | ||
end | ||
end | ||
|
||
class ToRuby | ||
def visit_Psych_Nodes_Mapping_with_class(object) | ||
return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag] | ||
|
||
case object.tag | ||
when /^!ruby\/class:?(.*)?$/ | ||
resolve_class $1 | ||
else | ||
visit_Psych_Nodes_Mapping_without_class(object) | ||
end | ||
end | ||
alias_method_chain :visit_Psych_Nodes_Mapping, :class | ||
|
||
def resolve_class_with_constantize(klass_name) | ||
klass_name.constantize | ||
rescue | ||
resolve_class_without_constantize(klass_name) | ||
end | ||
alias_method_chain :resolve_class, :constantize | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Module | ||
yaml_as "tag:ruby.yaml.org,2002:module" | ||
|
||
def self.yaml_new(klass, tag, val) | ||
val.constantize | ||
end | ||
|
||
def to_yaml(options = {}) | ||
YAML.quick_emit(nil, options) do |out| | ||
out.scalar(taguri, name, :plain) | ||
end | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters