Sha256: 8beb9c6081c920b579f874f152d90725d3851ce82d1e6d3568008b92679ba907

Contents?: true

Size: 1010 Bytes

Versions: 5

Compression:

Stored size: 1010 Bytes

Contents

require 'psych'
require 'representable'

module Representable
  module YAML
    autoload :Binding, 'representable/yaml/binding'
    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

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/lib/representable/yaml.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/lib/representable/yaml.rb
representable-3.2.0 lib/representable/yaml.rb
representable-3.1.1 lib/representable/yaml.rb
representable-3.1.0 lib/representable/yaml.rb