Sha256: cddd2393e6458ac731ac3c1e4ee81ea861eaf52cb7cdc1038747b41645138869

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 KB

Contents

require 'representable/binding'

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

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

      def serialize(value)
        write_scalar super(value)
      end

      def write_scalar(value)
        return value if typed?

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

      def serialize_method
        :to_ast
      end

      def deserialize_method
        :from_hash
      end
    end


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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
representable-2.0.4 lib/representable/bindings/yaml_bindings.rb
representable-2.0.3 lib/representable/bindings/yaml_bindings.rb
representable-2.0.2 lib/representable/bindings/yaml_bindings.rb
representable-2.0.1 lib/representable/bindings/yaml_bindings.rb
representable-2.0.0 lib/representable/bindings/yaml_bindings.rb
representable-2.0.0.rc2 lib/representable/bindings/yaml_bindings.rb
representable-2.0.0.rc1 lib/representable/bindings/yaml_bindings.rb
representable-1.8.5 lib/representable/bindings/yaml_bindings.rb
representable-1.8.4 lib/representable/bindings/yaml_bindings.rb
representable-1.8.3 lib/representable/bindings/yaml_bindings.rb
representable-1.8.2 lib/representable/bindings/yaml_bindings.rb
representable-1.8.1 lib/representable/bindings/yaml_bindings.rb
representable-1.8.0 lib/representable/bindings/yaml_bindings.rb