Module: Representable::YAML

Includes:
Hash
Defined in:
lib/representable/yaml.rb,
lib/representable/bindings/yaml_bindings.rb

Defined Under Namespace

Modules: ClassMethods, ObjectBinding Classes: CollectionBinding, PropertyBinding

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hash

#from_hash, #to_hash

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/representable/yaml.rb', line 8

def self.included(base)
  base.class_eval do
    include Representable
    extend ClassMethods
    #self.representation_wrap = true # let representable compute it.
  end
end

Instance Method Details

#from_yaml(doc, options = {}) ⇒ Object



31
32
33
34
# File 'lib/representable/yaml.rb', line 31

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

#to_ast(options = {}) ⇒ Object

Returns a Nokogiri::XML object representing this object.



37
38
39
40
41
42
43
# File 'lib/representable/yaml.rb', line 37

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

#to_yaml(*args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/representable/yaml.rb', line 45

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