Module: PaperTrail::Serializers::YAML

Extended by:
YAML
Included in:
YAML
Defined in:
lib/paper_trail/serializers/yaml.rb

Overview

The default serializer for, e.g. ‘versions.object`.

Instance Method Summary collapse

Instance Method Details

#dump(object) ⇒ Object



13
14
15
# File 'lib/paper_trail/serializers/yaml.rb', line 13

def dump(object)
  ::YAML.dump object
end

#load(string) ⇒ Object



9
10
11
# File 'lib/paper_trail/serializers/yaml.rb', line 9

def load(string)
  ::YAML.load string
end

#where_object_changes_condition(arel_field, field, value) ⇒ Object

Returns a SQL LIKE condition to be used to match the given field and value in the serialized ‘object_changes`.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/paper_trail/serializers/yaml.rb', line 25

def where_object_changes_condition(arel_field, field, value)
  # Need to check first (before) and secondary (after) fields
  m1 = nil
  m2 = nil
  case yaml_engine_id
  when :psych
    m1 = "%\n#{field}:\n- #{value}\n%"
    m2 = "%\n#{field}:\n-%\n- #{value}\n%"
  when :syck
    # Syck adds extra spaces into array dumps
    m1 = "%\n#{field}: \n%- #{value}\n%"
    m2 = "%\n#{field}: \n-%\n- #{value}\n%"
  else
    raise "Unknown yaml engine"
  end
  arel_field.matches(m1).or(arel_field.matches(m2))
end

#where_object_condition(arel_field, field, value) ⇒ Object

Returns a SQL LIKE condition to be used to match the given field and value in the serialized object.



19
20
21
# File 'lib/paper_trail/serializers/yaml.rb', line 19

def where_object_condition(arel_field, field, value)
  arel_field.matches("%\n#{field}: #{value}\n%")
end

#yaml_engine_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



46
47
48
49
50
51
52
53
# File 'lib/paper_trail/serializers/yaml.rb', line 46

def yaml_engine_id
  if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") ||
      (defined?(::Psych) && ::YAML == ::Psych)
    :psych
  else
    :syck
  end
end