module Yarii::Serializers::YAML

Active Model YAML Serializer

Public Instance Methods

as_yaml(options = {}) click to toggle source

Same thing as as_json, but returns yaml instead of a hash (unless you include the as_hash:true option)

# File lib/content_model_mixins.rb, line 24
def as_yaml(options = {})
  as_hash = options.delete(:as_hash)
  hash = serializable_hash(options)

  # 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 = options && options[:root]
    hash = { custom_root || self.class.model_name.element => hash }
  end

  as_hash ? hash : hash.to_yaml
end
from_yaml(yaml) click to toggle source
# 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