Skip to content

Commit

Permalink
Break out psych and syck yaml extensions to their own files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryckbost committed Sep 27, 2011
1 parent 39aa92a commit ff51ecb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
33 changes: 33 additions & 0 deletions lib/delayed/psych_ext.rb
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

34 changes: 34 additions & 0 deletions lib/delayed/syck_ext.rb
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
39 changes: 4 additions & 35 deletions lib/delayed/yaml_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,9 @@
# Classes, Modules and Structs

require 'yaml'
YAML::ENGINE.yamler = "syck" if defined?(YAML::ENGINE)

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
if YAML::ENGINE.syck?
require File.expand_path('../syck_ext', __FILE__)
else
require File.expand_path('../psych_ext', __FILE__)
end

0 comments on commit ff51ecb

Please sign in to comment.