Class: EacRubyUtils::Yaml
Overview
A safe YAML loader/dumper with common types included.
Defined Under Namespace
Classes: Sanitizer
Constant Summary collapse
- DEFAULT_PERMITTED_CLASSES =
[::Array, ::Date, ::DateTime, ::FalseClass, ::Hash, ::NilClass, ::Numeric, ::String, ::Symbol, ::Time, ::TrueClass].freeze
Class Method Summary collapse
- .dump(object) ⇒ Object
- .load(string) ⇒ Object
- .permitted_classes ⇒ Object
- .sanitize(object) ⇒ Object
- .yaml?(object) ⇒ Boolean
Class Method Details
.dump(object) ⇒ Object
13 14 15 |
# File 'lib/eac_ruby_utils/yaml.rb', line 13 def dump(object) ::YAML.dump(sanitize(object)) end |
.load(string) ⇒ Object
17 18 19 |
# File 'lib/eac_ruby_utils/yaml.rb', line 17 def load(string) ::YAML.safe_load(string, permitted_classes) end |
.permitted_classes ⇒ Object
21 22 23 |
# File 'lib/eac_ruby_utils/yaml.rb', line 21 def permitted_classes DEFAULT_PERMITTED_CLASSES end |
.sanitize(object) ⇒ Object
25 26 27 |
# File 'lib/eac_ruby_utils/yaml.rb', line 25 def sanitize(object) Sanitizer.new(object).result end |
.yaml?(object) ⇒ Boolean
29 30 31 32 33 34 35 36 37 |
# File 'lib/eac_ruby_utils/yaml.rb', line 29 def yaml?(object) return false unless object.is_a?(::String) return false unless object.start_with?('---') load(object) true rescue ::Psych::SyntaxError false end |