Closed softmoth closed 3 years ago
I should probably add just a few notes about this for posterity, since there's zero documentation for Psych's add_tag
.
Learning resources:
I tried to def initialize(tag_name)
and then call add_tag('code', TaggedMap.new('code')
, and def yaml_tag: '!' + @tag
, but couldn't find a combo that works, and it would have made the code a lot harder to read without providing any real benefits. So I left the !code
tag name hard-coded.
I'd rather not have inherited from Hash
, but I could not get as_json
to be called properly on the object. Calling doc.as_json()
fails since Hash
doesn't implement it, and it makes no sense to try to do so for this use case. There may well be a better solution for this, but I couldn't find anything. Anyways, I doubt that any proper solution would be as concise and legible, so perhaps it's best as is.
I've force-pushed an update of the specs/*.json
to match the latest revision of #119.
Thanks!
Note: this PR is built on top of #119. Please just look at the 2nd commit, the first is from #119.
This was broken when Ruby switched YAML engines from Syck to Psych around Ruby 1.9.3. Syck was removed completely around version 2.0. The code didn't fail, because Psych also implements the add_builtin_type routine, but it does not recognize
!code
as a builtin type (because it isn't; builtins look like!!str
or!!map
).Instead, Psych requires a custom class to handle decoding the tagged YAML item to Ruby.
And then, to get JSON to actually display the object, it is easiest to just inherit from
Hash
.I am not a Ruby developer. I welcome any code changes that would improve this.