Sha256: d8e12bc1af4976feaa129655333add6a6eafb503a38992c0db10ef26901f396d

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'representable/hash'
require 'representable/bindings/yaml_bindings'

module Representable
  module YAML
    include Hash
    
    def self.included(base)
      base.class_eval do
        include Representable
        extend ClassMethods
        #self.representation_wrap = true # let representable compute it.
      end
    end
    
    
    module ClassMethods
      # Creates a new Ruby object from XML using mapping information declared in the class.
      #
      # Accepts a block yielding the currently iterated Definition. If the block returns false 
      # the property is skipped.
      #
      # Example:
      #   band.from_xml("<band><name>Nofx</name></band>")
      def from_yaml(*args, &block)
        create_represented(*args, &block).from_yaml(*args)
      end
    end
    
    
    def from_yaml(doc, options={})
      hash = Psych.load(doc)
      from_hash(hash, options, :yaml)
    end
    
    # Returns a Nokogiri::XML object representing this object.
    def to_ast(options={})
      #root_tag = options[:wrap] || representation_wrap
      
      Psych::Nodes::Mapping.new.tap do |map|
        create_representation_with(map, options, :yaml)
      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

  private

    def yaml_binding_for_definition(definition)
      return CollectionBinding.new(definition)      if definition.array?
      #return HashBinding.new(definition)            if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
      #return AttributeHashBinding.new(definition)   if definition.hash? and definition.options[:use_attributes]
      #return AttributeBinding.new(definition)       if definition.attribute
      PropertyBinding.new(definition)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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