1module Psych 2 ### 3 # If an object defines +encode_with+, then an instance of Psych::Coder will 4 # be passed to the method when the object is being serialized. The Coder 5 # automatically assumes a Psych::Nodes::Mapping is being emitted. Other 6 # objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are 7 # called, respectively. 8 class Coder 9 attr_accessor :tag, :style, :implicit, :object 10 attr_reader :type, :seq 11 12 def initialize tag 13 @map = {} 14 @seq = [] 15 @implicit = false 16 @type = :map 17 @tag = tag 18 @style = Psych::Nodes::Mapping::BLOCK 19 @scalar = nil 20 @object = nil 21 end 22 23 def scalar *args 24 if args.length > 0 25 warn "#{caller[0]}: Coder#scalar(a,b,c) is deprecated" if $VERBOSE 26 @tag, @scalar, _ = args 27 @type = :scalar 28 end 29 @scalar 30 end 31 32 # Emit a map. The coder will be yielded to the block. 33 def map tag = @tag, style = @style 34 @tag = tag 35 @style = style 36 yield self if block_given? 37 @map 38 end 39 40 # Emit a scalar with +value+ and +tag+ 41 def represent_scalar tag, value 42 self.tag = tag 43 self.scalar = value 44 end 45 46 # Emit a sequence with +list+ and +tag+ 47 def represent_seq tag, list 48 @tag = tag 49 self.seq = list 50 end 51 52 # Emit a sequence with +map+ and +tag+ 53 def represent_map tag, map 54 @tag = tag 55 self.map = map 56 end 57 58 # Emit an arbitrary object +obj+ and +tag+ 59 def represent_object tag, obj 60 @tag = tag 61 @type = :object 62 @object = obj 63 end 64 65 # Emit a scalar with +value+ 66 def scalar= value 67 @type = :scalar 68 @scalar = value 69 end 70 71 # Emit a map with +value+ 72 def map= map 73 @type = :map 74 @map = map 75 end 76 77 def []= k, v 78 @type = :map 79 @map[k] = v 80 end 81 alias :add :[]= 82 83 def [] k 84 @type = :map 85 @map[k] 86 end 87 88 # Emit a sequence of +list+ 89 def seq= list 90 @type = :seq 91 @seq = list 92 end 93 end 94end 95