Module: Representable::YAML

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

Defined Under Namespace

Modules: ClassMethods Classes: Binding

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hash

#from_hash, #to_hash

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.class_eval do
    include Representable
    register_feature Representable::YAML
    extend ClassMethods
  end
end

Instance Method Details

#from_yaml(doc, options = {}) ⇒ Object Also known as: parse



23
24
25
26
# File 'lib/representable/yaml.rb', line 23

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

#to_ast(options = {}) ⇒ Object

Returns a Nokogiri::XML object representing this object.



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

def to_ast(options={})
  Psych::Nodes::Mapping.new.tap do |map|
    create_representation_with(map, options, Binding)
  end
end

#to_yaml(*args) ⇒ Object Also known as: render



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

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