Class: FormCore::YAMLCoder

Inherits:
Coder
  • Object
show all
Defined in:
lib/form_core/coders/yaml_coder.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Coder

#object_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coder

#initialize, #strict?

Constructor Details

This class inherits a constructor from FormCore::Coder

Class Method Details

.whitelist_classesObject



9
10
11
# File 'lib/form_core/coders/yaml_coder.rb', line 9

def self.whitelist_classes
  @whitelist_classes ||= []
end

Instance Method Details

#dump(obj) ⇒ Object



17
18
19
20
21
# File 'lib/form_core/coders/yaml_coder.rb', line 17

def dump(obj)
  return YAML.dump({}) unless obj

  YAML.dump obj.serializable_hash
end

#load(yaml) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/form_core/coders/yaml_coder.rb', line 23

def load(yaml)
  return object_class.new if yaml.blank?

  return new_or_raise_decoding_error unless yaml.is_a?(String) && /^---/.match?(yaml)

  decoded =
    if safe_mode?
      YAML.safe_load(yaml, YAMLCoder.whitelist_classes)
    else
      YAML.safe_load(yaml)
    end
  return new_or_raise_decoding_error unless decoded.is_a? Hash

  object_class.new valid_attributes(decoded)
end

#safe_mode?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/form_core/coders/yaml_coder.rb', line 13

def safe_mode?
  YAMLCoder.safe_mode
end