1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
require 'psych'
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
|