Module: Yarii::Serializers::YAML
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Serialization
- Included in:
- ContentModel, DatafileModel
- Defined in:
- lib/content_model_mixins.rb
Overview
Active Model YAML Serializer
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#as_yaml(options = {}) ⇒ Object
Same thing as as_json, but returns yaml instead of a hash (unless you include the as_hash:true option).
- #from_yaml(yaml) ⇒ Object
Instance Method Details
#as_yaml(options = {}) ⇒ Object
Same thing as as_json, but returns yaml instead of a hash (unless you include the as_hash:true option)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/content_model_mixins.rb', line 24 def as_yaml( = {}) as_hash = .delete(:as_hash) hash = serializable_hash() # we don't like DateTime in YML. Needs to be just Time. hash.each do |k,v| if v.nil? # we don't want to save nil values as empty front matter variables hash.delete(k) elsif v.is_a?(DateTime) hash[k] = v.to_time end end if include_root_in_json custom_root = && [:root] hash = { custom_root || self.class.model_name.element => hash } end as_hash ? hash : hash.to_yaml end |
#from_yaml(yaml) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/content_model_mixins.rb', line 46 def from_yaml(yaml) hash = SafeYAML.load(yaml) hash = hash.values.first if include_root_in_json self.assign_attributes(hash) self end |