123456789_123456789_123456789_123456789_123456789_

Class: Psych::Coder

Relationships & Source Files
Inherits: Object
Defined in: ext/psych/lib/psych/coder.rb

Overview

If an object defines encode_with, then an instance of Coder will be passed to the method when the object is being serialized. The Coder automatically assumes a Nodes::Mapping is being emitted. Other objects like Sequence and Scalar may be emitted if #seq= or #scalar= are called, respectively.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(tag) ⇒ Coder

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 12

def initialize tag
  @map      = {}
  @seq      = []
  @implicit = false
  @type     = :map
  @tag      = tag
  @style    = Psych::Nodes::Mapping::BLOCK
  @scalar   = nil
  @object   = nil
end

Instance Attribute Details

#implicit (rw)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 9

attr_accessor :tag, :style, :implicit, :object

#object (rw)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 9

attr_accessor :tag, :style, :implicit, :object

#seq (rw)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 10

attr_reader   :type, :seq

#seq=(list) (rw)

Emit a sequence of list

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 89

def seq= list
  @type = :seq
  @seq  = list
end

#style (rw)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 9

attr_accessor :tag, :style, :implicit, :object

#tag (rw)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 9

attr_accessor :tag, :style, :implicit, :object

#type (readonly)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 10

attr_reader   :type, :seq

Instance Method Details

#[](k)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 83

def [] k
  @type = :map
  @map[k]
end

#[]=(k, v) Also known as: #add

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 77

def []= k, v
  @type = :map
  @map[k] = v
end

#add(k, v)

Alias for #[]=.

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 81

alias :add :[]=

#map(tag = @tag, style = @style) {|_self| ... }

Emit a map. The coder will be yielded to the block.

Yields:

  • (_self)

Yield Parameters:

  • _self (Coder)

    the object that the method was called on

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 33

def map tag = @tag, style = @style
  @tag   = tag
  @style = style
  yield self if block_given?
  @map
end

#map=(map)

Emit a map with value

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 72

def map= map
  @type = :map
  @map  = map
end

#represent_map(tag, map)

Emit a sequence with #map and #tag

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 53

def represent_map tag, map
  @tag = tag
  self.map = map
end

#represent_object(tag, obj)

Emit an arbitrary object obj and #tag

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 59

def represent_object tag, obj
  @tag    = tag
  @type   = :object
  @object = obj
end

#represent_scalar(tag, value)

Emit a scalar with value and #tag

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 41

def represent_scalar tag, value
  self.tag    = tag
  self.scalar = value
end

#represent_seq(tag, list)

Emit a sequence with list and #tag

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 47

def represent_seq tag, list
  @tag = tag
  self.seq = list
end

#scalar(*args)

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 23

def scalar *args
  if args.length > 0
    warn "#{caller[0]}: Coder#scalar(a,b,c) is deprecated" if $VERBOSE
    @tag, @scalar, _ = args
    @type = :scalar
  end
  @scalar
end

#scalar=(value)

Emit a scalar with value

[ GitHub ]

  
# File 'ext/psych/lib/psych/coder.rb', line 66

def scalar= value
  @type   = :scalar
  @scalar = value
end