1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/lib/paper_trail/serializers/yaml.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

require "yaml"
module PaperTrail
module Serializers
2016-04-09 01:08:34 -04:00
# The default serializer for, e.g. `versions.object`.
module YAML
extend self # makes all instance methods become module methods as well
def load(string)
::YAML.load string
end
def dump(object)
::YAML.dump object
end
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized object.
def where_object_condition(arel_field, field, value)
arel_field.matches("%\n#{field}: #{value}\n%")
end
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized `object_changes`.
def where_object_changes_condition(arel_field, field, value)
# Need to check first (before) and secondary (after) fields
m1 = nil
m2 = nil
2016-03-05 17:48:20 -05:00
case yaml_engine_id
when :psych
m1 = "%\n#{field}:\n- #{value}\n%"
m2 = "%\n#{field}:\n-%\n- #{value}\n%"
2016-03-05 17:48:20 -05:00
when :syck
# Syck adds extra spaces into array dumps
m1 = "%\n#{field}: \n%- #{value}\n%"
m2 = "%\n#{field}: \n-%\n- #{value}\n%"
2016-03-05 17:48:20 -05:00
else
raise "Unknown yaml engine"
end
arel_field.matches(m1).or(arel_field.matches(m2))
end
2016-03-05 17:48:20 -05:00
# Returns a symbol identifying the YAML engine. Syck was removed from
# the ruby stdlib in ruby 2.0, but is still available as a gem.
# @api private
def yaml_engine_id
if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") ||
(defined?(::Psych) && ::YAML == ::Psych)
:psych
else
:syck
end
end
end
end
end