Sha256: 5a9fc9b012ca259c98dfa15ab2e3d8766f427fb480da5b69b0b3edfafca8b5cb

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'representable/binding'

module Representable
  module YAML
    module ObjectBinding
      include Binding::Object

      def serialize_method
        :to_ast
      end

      def deserialize_method
        :from_hash
      end

      def write_scalar(value)
        value
      end
    end

    class PropertyBinding < Representable::Hash::PropertyBinding
      def self.build_for(definition, *args)
        return CollectionBinding.new(definition, *args) if definition.array?
        new(definition, *args)
      end

      def initialize(*args) # FIXME. make generic.
        super
        extend ObjectBinding if typed?
      end

      def write(map, value)
        map.children << Psych::Nodes::Scalar.new(from)
        map.children << serialize_for(value)  # FIXME: should be serialize.
      end

      def serialize_for(value)
        write_scalar serialize(value)
      end

      def write_scalar(value)
        Psych::Nodes::Scalar.new(value.to_s)
      end
    end


    class CollectionBinding < PropertyBinding
      def serialize_for(value)
        Psych::Nodes::Sequence.new.tap do |seq|
          seq.style = Psych::Nodes::Sequence::FLOW if options[:style] == :flow
          value.each { |obj| seq.children << super(obj) }
        end
      end

      def deserialize_from(fragment)  # FIXME: redundant from Hash::Bindings
        CollectionDeserializer.new(self).deserialize(fragment)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-1.7.0 lib/representable/bindings/yaml_bindings.rb