Class: Coaster::SafeYamlSerializer
- Defined in:
- lib/coaster/safe_yaml_serializer.rb
Overview
ActiveRecord::AttributeMethods::Serialization#serialize 적용시 YAML 시리얼라이저로 지정한다. 다음과 같은 특징이 있다.
*
주의
*
Example
class User < ApplicationRecord
serialize :data, SafeYamlSerializer
end
Class Method Summary collapse
Class Method Details
._load_hash(yaml) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/coaster/safe_yaml_serializer.rb', line 24 def _load_hash(yaml) return {} if yaml.nil? return yaml unless yaml.is_a?(String) && yaml.start_with?("---") if Rails.env.production? YAML.load(yaml) else begin YAML.safe_load(yaml, [], [], true) || {} rescue Psych::DisallowedClass => e # Rails.logger.warn e.inspect # Rails.logger.warn e.backtrace.join("\n") YAML.load(yaml) || {} end end end |
.dump(obj) ⇒ Object
19 20 21 22 |
# File 'lib/coaster/safe_yaml_serializer.rb', line 19 def dump(obj) return if obj.nil? YAML.dump(JSON.load(obj.to_json)) end |
.load(yaml) ⇒ HashWithIndifferentAccess
42 43 44 |
# File 'lib/coaster/safe_yaml_serializer.rb', line 42 def load(yaml) (_load_hash(yaml) || {}).with_indifferent_access end |