summaryrefslogtreecommitdiff
path: root/ext/psych/lib/psych/coder.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-30 10:46:06 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-30 10:46:06 +0000
commitf1330fd12403ef4d664df7619d6ce312d525721a (patch)
treecfafd35d3d416b8529003d43db2ace98af20fdb1 /ext/psych/lib/psych/coder.rb
parent38af94c6abaaa20aeaa1ffafbe3f6ff962f63eaa (diff)
* ext/psych/lib: moved external library dependent files from lib.
[ruby-core:29129] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27112 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych/lib/psych/coder.rb')
-rw-r--r--ext/psych/lib/psych/coder.rb68
1 files changed, 68 insertions, 0 deletions
diff --git a/ext/psych/lib/psych/coder.rb b/ext/psych/lib/psych/coder.rb
new file mode 100644
index 0000000000..79e46dab1b
--- /dev/null
+++ b/ext/psych/lib/psych/coder.rb
@@ -0,0 +1,68 @@
+module Psych
+ ###
+ # If an object defines +encode_with+, then an instance of Psych::Coder will
+ # be passed to the method when the object is being serialized. The Coder
+ # automatically assumes a Psych::Nodes::Mapping is being emitted. Other
+ # objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are
+ # called, respectively.
+ class Coder
+ attr_accessor :tag, :style, :implicit
+ attr_reader :type, :map, :scalar, :seq
+
+ def initialize tag
+ @map = {}
+ @seq = []
+ @implicit = false
+ @type = :map
+ @tag = tag
+ @style = Psych::Nodes::Mapping::BLOCK
+ @scalar = nil
+ end
+
+ # Emit a scalar with +value+ and +tag+
+ def represent_scalar tag, value
+ self.tag = tag
+ self.scalar = value
+ end
+
+ # Emit a sequence with +list+ and +tag+
+ def represent_seq tag, list
+ @tag = tag
+ self.seq = list
+ end
+
+ # Emit a sequence with +map+ and +tag+
+ def represent_map tag, map
+ @tag = tag
+ self.map = map
+ end
+
+ # Emit a scalar with +value+
+ def scalar= value
+ @type = :scalar
+ @scalar = value
+ end
+
+ # Emit a map with +value+
+ def map= map
+ @type = :map
+ @map = map
+ end
+
+ def []= k, v
+ @type = :map
+ @map[k] = v
+ end
+
+ def [] k
+ @type = :map
+ @map[k]
+ end
+
+ # Emit a sequence of +list+
+ def seq= list
+ @type = :seq
+ @seq = list
+ end
+ end
+end