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

38 lines
1.2 KiB
Ruby
Raw Normal View History

require "yaml"
module PaperTrail
module Serializers
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 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 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
if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") ||
2016-03-05 17:30:53 -05:00
(defined?(::Psych) && ::YAML == ::Psych)
arel_field.matches("%\n#{field}:\n- #{value}\n%").
or(arel_field.matches("%\n#{field}:\n-%\n- #{value}\n%"))
else # Syck adds extra spaces into array dumps
arel_field.matches("%\n#{field}: \n%- #{value}\n%").
or(arel_field.matches("%\n#{field}: \n-%\n- #{value}\n%"))
end
end
end
end
end