summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-01-26 18:03:53 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-01-26 18:03:53 -0800
commit2fa4eb511d63d2f0c0924664c0eb646f76f4d9dd (patch)
tree154d40239b13fcade5d0deadee18dda66058f31c /lib
parent7cb15116f5702c729abdc9888120704e5a828e27 (diff)
downloadpsych-2fa4eb511d63d2f0c0924664c0eb646f76f4d9dd.zip
coder can muck with the mapping output
Diffstat (limited to 'lib')
-rw-r--r--lib/psych.rb1
-rw-r--r--lib/psych/coder.rb33
-rw-r--r--lib/psych/visitors/to_ruby.rb12
-rw-r--r--lib/psych/visitors/yaml_tree.rb2
4 files changed, 41 insertions, 7 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index 0424e72..cebc427 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -6,6 +6,7 @@ require 'psych/tree_builder'
require 'psych/parser'
require 'psych/omap'
require 'psych/set'
+require 'psych/coder'
###
# = Overview
diff --git a/lib/psych/coder.rb b/lib/psych/coder.rb
new file mode 100644
index 0000000..88c7322
--- /dev/null
+++ b/lib/psych/coder.rb
@@ -0,0 +1,33 @@
+module Psych
+ class Coder < ::Hash
+ def initialize map, h = nil
+ super()
+ merge!(h) if h
+ @map = map
+ end
+
+ def tag= tag
+ @map.tag = tag
+ end
+
+ def tag
+ @map.tag
+ end
+
+ def style= style
+ @map.style = style
+ end
+
+ def style
+ @map.style
+ end
+
+ def implicit= implicity
+ @map.implicit = implicity
+ end
+
+ def implicit
+ @map.implicit
+ end
+ end
+end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index f156d87..2ec0449 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -89,7 +89,7 @@ module Psych
when '!str', 'tag:yaml.org,2002:str'
members = Hash[*o.children.map { |c| accept c }]
string = members.delete 'str'
- init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] })
+ init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
when /^!ruby\/struct:?(.*)?$/
klass = resolve_class($1)
@@ -108,7 +108,7 @@ module Psych
members[member.to_s.sub(/^@/, '')] = value
end
end
- init_with(s, members)
+ init_with(s, members, o)
else
members = o.children.map { |c| accept c }
h = Hash[*members]
@@ -124,7 +124,7 @@ module Psych
e = build_exception((resolve_class($1) || Exception),
h.delete('message'))
- init_with(e, h)
+ init_with(e, h, o)
when '!set', 'tag:yaml.org,2002:set'
set = Psych::Set.new
@@ -146,7 +146,7 @@ module Psych
name = $1 || 'Object'
h = Hash[*o.children.map { |c| accept c }]
s = (resolve_class(name) || Object).allocate
- init_with(s, h)
+ init_with(s, h, o)
else
hash = {}
@st[o.anchor] = hash if o.anchor
@@ -170,9 +170,9 @@ module Psych
end
private
- def init_with o, h
+ def init_with o, h, node
if o.respond_to?(:init_with)
- o.init_with h
+ o.init_with Psych::Coder.new(node, h)
else
h.each { |k,v| o.instance_variable_set(:"@#{k}", v) }
end
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 8732200..d27b1ec 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -235,7 +235,7 @@ module Psych
def dump_ivars target, map
if target.respond_to?(:encode_with)
- coder = {}
+ coder = Psych::Coder.new(map)
target.encode_with(coder)
coder.each do |k,v|
map.children << Nodes::Scalar.new(k)