Skip to content
This repository
Browse code

represent scalar works

  • Loading branch information...
commit 182f450bd8b60a1b615db517e54d74c8a836e2a1 1 parent 35ca1ca
Aaron Patterson authored
6 lib/psych/coder.rb
@@ -19,6 +19,12 @@ def initialize tag
19 19 @scalar = nil
20 20 end
21 21
  22 + # Emit a scalar with +value+ and +tag+
  23 + def represent_scalar tag, value
  24 + self.tag = tag
  25 + self.scalar = value
  26 + end
  27 +
22 28 # Emit a scalar with +value+
23 29 def scalar= value
24 30 @type = :scalar
12 lib/psych/visitors/to_ruby.rb
@@ -28,6 +28,18 @@ def accept target
28 28 def visit_Psych_Nodes_Scalar o
29 29 @st[o.anchor] = o.value if o.anchor
30 30
  31 + if klass = Psych.load_tags[o.tag]
  32 + instance = klass.allocate
  33 +
  34 + if instance.respond_to?(:init_with)
  35 + coder = Psych::Coder.new(o.tag)
  36 + coder.scalar = o.value
  37 + instance.init_with coder
  38 + end
  39 +
  40 + return instance
  41 + end
  42 +
31 43 return o.value if o.quoted
32 44 return @ss.tokenize(o.value) unless o.tag
33 45
2  lib/psych/visitors/yaml_tree.rb
@@ -255,7 +255,7 @@ def dump_coder o
255 255 def emit_coder c
256 256 case c.type
257 257 when :scalar
258   - append Nodes::Scalar.new(c.scalar, nil, c.tag)
  258 + append Nodes::Scalar.new(c.scalar, nil, c.tag, c.tag.nil?)
259 259 when :map
260 260 map = append Nodes::Mapping.new(nil, c.tag, c.implicit, c.style)
261 261 @stack.push map
30 test/psych/test_coder.rb
@@ -44,6 +44,36 @@ def encode_with coder
44 44 end
45 45 end
46 46
  47 + class Represent
  48 + yaml_tag 'foo'
  49 + def encode_with coder
  50 + coder.represent_scalar 'foo', 'bar'
  51 + end
  52 + end
  53 +
  54 + class RepresentWithInit
  55 + yaml_tag name
  56 + attr_accessor :str
  57 +
  58 + def init_with coder
  59 + @str = coder.scalar
  60 + end
  61 +
  62 + def encode_with coder
  63 + coder.represent_scalar self.class.name, 'bar'
  64 + end
  65 + end
  66 +
  67 + def test_represent_with_init
  68 + thing = Psych.load(Psych.dump(RepresentWithInit.new))
  69 + assert_equal 'bar', thing.str
  70 + end
  71 +
  72 + def test_represent!
  73 + assert_match(/foo/, Psych.dump(Represent.new))
  74 + assert_instance_of(Represent, Psych.load(Psych.dump(Represent.new)))
  75 + end
  76 +
47 77 def test_scalar_coder
48 78 foo = Psych.load(Psych.dump(ScalarCoder.new))
49 79 assert_equal 'foo', foo

0 comments on commit 182f450

Please sign in to comment.
Something went wrong with that request. Please try again.