Sha256: 0428e353549b3f2297c82ed74d4dc9d5f6cba6d78a90065c7178e033efd50df6

Contents?: true

Size: 1.39 KB

Versions: 15

Compression:

Stored size: 1.39 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, PropertyBinding)
    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, PropertyBinding)
      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
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
representable-1.5.3 lib/representable/yaml.rb
representable-1.5.2 lib/representable/yaml.rb
representable-1.5.1 lib/representable/yaml.rb
representable-1.5.0 lib/representable/yaml.rb
representable-1.4.2 lib/representable/yaml.rb
representable-1.4.1 lib/representable/yaml.rb
representable-1.4.0 lib/representable/yaml.rb
representable-1.3.5 lib/representable/yaml.rb
representable-1.3.4 lib/representable/yaml.rb
representable-1.3.3 lib/representable/yaml.rb
representable-1.3.2 lib/representable/yaml.rb
representable-1.3.1 lib/representable/yaml.rb
representable-1.3.0 lib/representable/yaml.rb
representable-1.2.9 lib/representable/yaml.rb
representable-1.2.8 lib/representable/yaml.rb