Class: FormCore::YAMLCoder
- Inherits:
-
Coder
- Object
- Coder
- FormCore::YAMLCoder
show all
- Defined in:
- lib/form_core/coders/yaml_coder.rb
Overview
Instance Attribute Summary
Attributes inherited from Coder
#object_class
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Coder
#initialize, #strict?
Class Method Details
.whitelist_classes ⇒ Object
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.to_h
end
|
#load(yaml) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/form_core/coders/yaml_coder.rb', line 23
def load(yaml)
return object_class.new if yaml.blank?
unless yaml.is_a?(String) && /^---/.match?(yaml)
return new_or_raise_decoding_error
end
decoded =
if safe_mode?
YAML.safe_load(yaml, YAMLCoder.whitelist_classes)
else
YAML.load(yaml)
end
unless decoded.is_a? Hash
return new_or_raise_decoding_error
end
object_class.new decoded
end
|
#safe_mode? ⇒ Boolean
13
14
15
|
# File 'lib/form_core/coders/yaml_coder.rb', line 13
def safe_mode?
YAMLCoder.safe_mode
end
|