Sha256: 7e888bb8219c6b81670a383b283fd9ccf2161f3fa2dc48d29564f455ab14dcc3

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

require 'dm-serializer/common'

module DataMapper
  module Serialize
    # Serialize a Resource to YAML
    #
    # @return <YAML> a YAML representation of this Resource
    def to_yaml(opts_or_emitter = {})
      if !opts_or_emitter.is_a?(Hash)
        emitter = opts_or_emitter
        opts = {}
      else
        emitter = {}
        opts = opts_or_emitter
      end

      YAML::quick_emit(object_id,emitter) do |out|
        out.map(nil,to_yaml_style) do |map|
          properties_to_serialize(opts).each do |property|
            value = __send__(property.name.to_sym)
            map.add(property.name, value.is_a?(Class) ? value.to_s : value)
          end
          # add methods
          (opts[:methods] || []).each do |meth|
            if respond_to?(meth)
              map.add(meth.to_sym, __send__(meth))
            end
          end
          (instance_variable_get("@yaml_addes") || []).each do |k,v|
            map.add(k.to_s,v)
          end
        end
      end
    end

    module ValidationErrors
      module ToYaml
        def to_yaml(*args)
          errors.to_hash.to_yaml(*args)
        end
      end
    end

  end

  class Collection
    def to_yaml(opts_or_emitter = {})
      if !opts_or_emitter.is_a?(Hash)
        to_a.to_yaml(opts_or_emitter)
      else
        # FIXME: Don't double handle the YAML (remove the YAML.load)
        to_a.collect {|x| YAML.load(x.to_yaml(opts_or_emitter)) }.to_yaml
      end
    end
  end

  if Serialize.dm_validations_loaded?

    module Validations
      class ValidationErrors
        include DataMapper::Serialize::ValidationErrors::ToYaml
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dm-serializer-1.0.2 lib/dm-serializer/to_yaml.rb
dm-serializer-1.0.1 lib/dm-serializer/to_yaml.rb
dm-serializer-1.0.0 lib/dm-serializer/to_yaml.rb
dm-serializer-1.0.0.rc3 lib/dm-serializer/to_yaml.rb
dm-serializer-1.0.0.rc2 lib/dm-serializer/to_yaml.rb
dm-serializer-1.0.0.rc1 lib/dm-serializer/to_yaml.rb