Module: FIRM::Serializable::YAML

Defined in:
lib/firm/serializer/yaml.rb

Defined Under Namespace

Modules: YamlSerializePatch Classes: NoClassYAMLTree, RestrictedRelaxed

Class Method Summary collapse

Class Method Details

.dump(obj, io = nil) ⇒ Object



92
93
94
95
96
# File 'lib/firm/serializer/yaml.rb', line 92

def self.dump(obj, io=nil, **)
  visitor = YAML::NoClassYAMLTree.create
  visitor << obj
  visitor.tree.yaml io
end

.load(source) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/firm/serializer/yaml.rb', line 98

def self.load(source)
  result = ::YAML.parse(source, filename: nil)
  return nil unless result
  allowed_classes =(YAML.serializables + Serializable.serializables.to_a).map(&:to_s)
  class_loader = RestrictedRelaxed.new(allowed_classes)
  scanner      = ::YAML::ScalarScanner.new(class_loader)
  visitor = ::YAML::Visitors::ToRuby.new(scanner, class_loader)
  visitor.extend(YamlSerializePatch)
  visitor.accept result
end

.serializablesObject



21
22
23
24
25
26
# File 'lib/firm/serializer/yaml.rb', line 21

def serializables
  list = [::Date, ::DateTime, ::Range, ::Rational, ::Complex, ::Regexp, ::Struct, ::Symbol, ::Time, ::Set]
  list.push(::OpenStruct) if ::Object.const_defined?(:OpenStruct)
  list.push(::BigDecimal) if ::Object.const_defined?(:BigDecimal)
  list
end