Class: Psych::Coder

Inherits:
Object show all
Defined in:
lib/psych/coder.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ Coder

Returns a new instance of Coder.



13
14
15
16
17
18
19
20
21
22
# File 'lib/psych/coder.rb', line 13

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

Instance Attribute Details

#implicitObject

Returns the value of attribute implicit



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

def implicit
  @implicit
end

#objectObject

Returns the value of attribute object



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

def object
  @object
end

#seqObject

Returns the value of attribute seq



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

def seq
  @seq
end

#styleObject

Returns the value of attribute style



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

def style
  @style
end

#tagObject

Returns the value of attribute tag



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

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

#[](k) ⇒ Object



84
85
86
87
# File 'lib/psych/coder.rb', line 84

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

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



78
79
80
81
# File 'lib/psych/coder.rb', line 78

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

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

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Psych::Coder)

    the object that the method was called on



34
35
36
37
38
39
# File 'lib/psych/coder.rb', line 34

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

#map=(map) ⇒ Object

Emit a map with value



73
74
75
76
# File 'lib/psych/coder.rb', line 73

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

#represent_map(tag, map) ⇒ Object

Emit a sequence with map and tag



54
55
56
57
# File 'lib/psych/coder.rb', line 54

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

#represent_object(tag, obj) ⇒ Object

Emit an arbitrary object obj and tag



60
61
62
63
64
# File 'lib/psych/coder.rb', line 60

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

#represent_scalar(tag, value) ⇒ Object

Emit a scalar with value and tag



42
43
44
45
# File 'lib/psych/coder.rb', line 42

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

#represent_seq(tag, list) ⇒ Object

Emit a sequence with list and tag



48
49
50
51
# File 'lib/psych/coder.rb', line 48

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

#scalar(*args) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/psych/coder.rb', line 24

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) ⇒ Object

Emit a scalar with value



67
68
69
70
# File 'lib/psych/coder.rb', line 67

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