Class: ActiveRecord::Coders::YAMLColumn::SafeCoder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/coders/yaml_column.rb

Instance Method Summary collapse

Constructor Details

#initialize(permitted_classes: [], unsafe_load: nil) ⇒ SafeCoder

Returns a new instance of SafeCoder.



9
10
11
12
# File 'lib/active_record/coders/yaml_column.rb', line 9

def initialize(permitted_classes: [], unsafe_load: nil)
  @permitted_classes = permitted_classes
  @unsafe_load = unsafe_load
end

Instance Method Details

#dump(object) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_record/coders/yaml_column.rb', line 15

def dump(object)
  if @unsafe_load.nil? ? ActiveRecord.use_yaml_unsafe_load : @unsafe_load
    ::YAML.dump(object)
  else
    ::YAML.safe_dump(
      object,
      permitted_classes: @permitted_classes + ActiveRecord.yaml_column_permitted_classes,
      aliases: true,
    )
  end
end

#load(payload) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record/coders/yaml_column.rb', line 33

def load(payload)
  if @unsafe_load.nil? ? ActiveRecord.use_yaml_unsafe_load : @unsafe_load
    YAML.unsafe_load(payload)
  else
    YAML.safe_load(
      payload,
      permitted_classes: @permitted_classes + ActiveRecord.yaml_column_permitted_classes,
      aliases: true,
    )
  end
end