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

Instance Method Summary collapse

Methods inherited from Coder

#initialize, #strict?

Constructor Details

This class inherits a constructor from FormCore::Coder

Instance Method Details

#dump(obj) ⇒ Object



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

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

  YAML.dump obj.to_h
end

#load(yaml) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/form_core/coders/yaml_coder.rb', line 13

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 = YAML.load(yaml)
  unless decoded.is_a? Hash
    return new_or_raise_decoding_error
  end

  object_class.new decoded
end