summaryrefslogtreecommitdiff
path: root/lib/psych.rb
blob: df28ba0700555d546f1d6154ae5ea785eecccbc1 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'psych/visitable'

require 'psych/nodes/node'
require 'psych/nodes/stream'
require 'psych/nodes/document'
require 'psych/nodes/sequence'
require 'psych/nodes/scalar'
require 'psych/nodes/mapping'
require 'psych/nodes/alias'

require 'psych/visitors'

require 'psych/handler'
require 'psych/tree_builder'
require 'psych/parser'
require 'psych/ruby'
require 'psych/psych'

module Psych
  VERSION         = '1.0.0'
  LIBYAML_VERSION = Psych.libyaml_version.join '.'

  ###
  # Load +yaml+ in to a Ruby data structure
  def self.load yaml
    parse(yaml).to_ruby
  end

  ###
  # Parse a YAML string.  Returns the first object of a YAML parse tree
  def self.parse yaml
    yaml_ast(yaml).children.first.children.first
  end

  ###
  # Parse a YAML string in +yaml+.  Returns the AST for the YAML parse tree.
  def self.yaml_ast yaml
    parser = Psych::Parser.new(TreeBuilder.new)
    parser.parse yaml
    parser.handler.root
  end

  ###
  # Dump object +o+ to a YAML string
  def self.dump o, options = {}
    visitor = Psych::Visitors::YASTBuilder.new options
    visitor.accept o
    visitor.tree.to_yaml
  end

  ###
  # Load multiple documents given in +yaml+, yielding each document to
  # the block provided.
  def self.load_documents yaml, &block
    yaml_ast(yaml).children.each do |child|
      block.call child.to_ruby
    end
  end

  @domain_types = {}
  def self.add_domain_type domain, type_tag, &block
    @domain_types[type_tag] = [domain, block]
  end
  class << self; attr_accessor :domain_types; end
end