Class: EacRubyUtils::Yaml

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/yaml.rb

Overview

A safe YAML loader/dumper with common types included.

Defined Under Namespace

Classes: Sanitizer

Constant Summary collapse

DEFAULT_PERMITTED_CLASSES =
[::Array, ::Date, ::FalseClass, ::Hash, ::NilClass, ::Numeric,
::String, ::Symbol, ::Time, ::TrueClass].freeze

Class Method Summary collapse

Class Method Details

.dump(object) ⇒ Object


12
13
14
# File 'lib/eac_ruby_utils/yaml.rb', line 12

def dump(object)
  ::YAML.dump(sanitize(object))
end

.load(string) ⇒ Object


16
17
18
# File 'lib/eac_ruby_utils/yaml.rb', line 16

def load(string)
  ::YAML.safe_load(string, permitted_classes)
end

.permitted_classesObject


20
21
22
# File 'lib/eac_ruby_utils/yaml.rb', line 20

def permitted_classes
  DEFAULT_PERMITTED_CLASSES
end

.sanitize(object) ⇒ Object


24
25
26
# File 'lib/eac_ruby_utils/yaml.rb', line 24

def sanitize(object)
  Sanitizer.new(object).result
end

.yaml?(object) ⇒ Boolean

Returns:


28
29
30
31
32
33
34
35
36
# File 'lib/eac_ruby_utils/yaml.rb', line 28

def yaml?(object)
  return false unless object.is_a?(::String)
  return false unless object.start_with?('---')

  load(object)
  true
rescue ::Psych::SyntaxError
  false
end