--SUOF0GtieIMvvwua
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Aug 23, 2012 at 03:49:03PM +0900, naruse (Yui NARUSE) wrote:
>=20
> Issue #6910 has been updated by naruse (Yui NARUSE).
>=20
>=20
> I want to migrate gradually.
> The way needs explicit big bang conversion.

Then I guess you need a way to differentiate converted yaml from
non-converted yaml.  Most people I've helped with this use something out
of band like a database column to mark the conversion.  However, it is
possible to use a YAML version.  Syck does not support YAML 1.1, so we
can tag the YAML as version 1.1.  Unfortunately, syck will not raise an
exception on the version identifier, so we have to test for it
ourselves.  Here is an example:

    require 'syck'
    require 'yaml'
    require 'psych'
    require 'minitest/autorun'
   =20
    class Loader < MiniTest::Unit::TestCase
      def converted? text
        text =3D~ /\A%YAML 1\.1/
      end
   =20
      def load_yaml text
        if converted? text
          Psych.load text
        else
          YAML.load text
        end
      end
   =20
      def dump_yaml object
        Psych.dump object, {:version =3D> [1,1]}
      end
   =20
      def test_convert
        obj         =3D ["\u3042",Time.at(0).to_s]
        legacy_yaml =3D YAML.dump obj
        obj2        =3D load_yaml legacy_yaml
   =20
        # we can load legacy yaml
        assert_equal obj, obj2
   =20
        converted_yaml =3D dump_yaml obj
   =20
        # make sure the yaml is tagged when dumping
        assert converted? converted_yaml
   =20
        # make sure object loaded from converted yaml is same
        assert_equal obj, load_yaml(converted_yaml)
      end
    end

--=20
Aaron Patterson
http://tenderlovemaking.com/

--SUOF0GtieIMvvwua
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (Darwin)

iQEcBAEBAgAGBQJQNlbvAAoJEJUxcLy0/6/G5+sIAJjmZ7uqeDfN2YRXcLdI4xwy
Iolt0X6yPRvlrpKCQv9FdRm5pE4isBp5uK4/r/WUdkaOSuPL/ZprxzHhSXlHzU4v
SYtKZhbiGJuLzLE/fvOucKqf9NbSVvJbKZNNDAgKqXdQNqxkoIraP9i/XAYtSdFf
6/1u7JlchHot+K7VAnxPvuZebUEGPZ2h/bVFrEsy5JqSH4kQ846ePYr/xrAdZbHw
/QEiB23x1ev4DxCXF7drUUCpS/rTQWP7mxUfh2ShYjufVqyZpejYyXT4R3iHD2EJ
brq3gDjIDPZBie1/MLECDonobdWEUUHKbjXBtXrSi7UxVUOwKqFzmGcHuKo255Y=
=RkRW
-----END PGP SIGNATURE-----

--SUOF0GtieIMvvwua--

On Thu, Aug 23, 2012 at 03:49:03PM +0900, naruse (Yui NARUSE) wrote:
>=20
> Issue #6910 has been updated by naruse (Yui NARUSE).
>=20
>=20
> I want to migrate gradually.
> The way needs explicit big bang conversion.

Then I guess you need a way to differentiate converted yaml from
non-converted yaml.  Most people I've helped with this use something out
of band like a database column to mark the conversion.  However, it is
possible to use a YAML version.  Syck does not support YAML 1.1, so we
can tag the YAML as version 1.1.  Unfortunately, syck will not raise an
exception on the version identifier, so we have to test for it
ourselves.  Here is an example:

    require 'syck'
    require 'yaml'
    require 'psych'
    require 'minitest/autorun'
   =20
    class Loader < MiniTest::Unit::TestCase
      def converted? text
        text =3D~ /\A%YAML 1\.1/
      end
   =20
      def load_yaml text
        if converted? text
          Psych.load text
        else
          YAML.load text
        end
      end
   =20
      def dump_yaml object
        Psych.dump object, {:version =3D> [1,1]}
      end
   =20
      def test_convert
        obj         =3D ["\u3042",Time.at(0).to_s]
        legacy_yaml =3D YAML.dump obj
        obj2        =3D load_yaml legacy_yaml
   =20
        # we can load legacy yaml
        assert_equal obj, obj2
   =20
        converted_yaml =3D dump_yaml obj
   =20
        # make sure the yaml is tagged when dumping
        assert converted? converted_yaml
   =20
        # make sure object loaded from converted yaml is same
        assert_equal obj, load_yaml(converted_yaml)
      end
    end

--=20
Aaron Patterson
http://tenderlovemaking.com/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (Darwin)

iQEcBAEBAgAGBQJQNlbvAAoJEJUxcLy0/6/G5+sIAJjmZ7uqeDfN2YRXcLdI4xwy
Iolt0X6yPRvlrpKCQv9FdRm5pE4isBp5uK4/r/WUdkaOSuPL/ZprxzHhSXlHzU4v
SYtKZhbiGJuLzLE/fvOucKqf9NbSVvJbKZNNDAgKqXdQNqxkoIraP9i/XAYtSdFf
6/1u7JlchHot+K7VAnxPvuZebUEGPZ2h/bVFrEsy5JqSH4kQ846ePYr/xrAdZbHw
/QEiB23x1ev4DxCXF7drUUCpS/rTQWP7mxUfh2ShYjufVqyZpejYyXT4R3iHD2EJ
brq3gDjIDPZBie1/MLECDonobdWEUUHKbjXBtXrSi7UxVUOwKqFzmGcHuKo255Y=
=RkRW
-----END PGP SIGNATURE-----