Sha256: a3ebbdba88fcd77d5a88093d2e6fa2f360a4a33bf67d2d4694c83c139be48ddc

Contents?: true

Size: 985 Bytes

Versions: 1

Compression:

Stored size: 985 Bytes

Contents

require 'representable/hash'
require 'representable/yaml/binding'

module Representable
  module YAML
    include Hash

    def self.included(base)
      base.class_eval do
        include Representable
        register_feature Representable::YAML
        extend ClassMethods
      end
    end

    module ClassMethods
      def format_engine
        Representable::YAML
      end
    end

    def from_yaml(doc, options={})
      hash = Psych.load(doc)
      from_hash(hash, options, Binding)
    end

    # Returns a Nokogiri::XML object representing this object.
    def to_ast(options={})
      Psych::Nodes::Mapping.new.tap do |map|
        create_representation_with(map, options, Binding)
      end
    end

    def to_yaml(*args)
      stream = Psych::Nodes::Stream.new
      stream.children << doc = Psych::Nodes::Document.new

      doc.children << to_ast(*args)
      stream.to_yaml
    end

     alias_method :render, :to_yaml
    alias_method :parse, :from_yaml
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-3.0.2 lib/representable/yaml.rb