Module: Psych::Comments

Defined in:
lib/psych/comments.rb,
lib/psych/comments/emitter.rb,
lib/psych/comments/parsing.rb,
lib/psych/comments/version.rb

Defined Under Namespace

Modules: NodeUtils Classes: Error

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.emit_yaml(node) ⇒ Object



312
313
314
315
316
# File 'lib/psych/comments/emitter.rb', line 312

def self.emit_yaml(node)
  emitter = Emitter.new
  emitter.emit(node)
  emitter.out
end

.parse(yaml, filename: nil) ⇒ Object



85
86
87
88
89
90
# File 'lib/psych/comments/parsing.rb', line 85

def self.parse(yaml, filename: nil)
  parse_stream(yaml, filename: filename) do |node|
    return node
  end
  false
end

.parse_file(filename) ⇒ Object



92
93
94
95
96
# File 'lib/psych/comments/parsing.rb', line 92

def self.parse_file(filename)
  File.open filename, 'r:bom|utf-8' do |f|
    parse f, filename: filename
  end
end

.parse_stream(yaml, filename: nil, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/psych/comments/parsing.rb', line 98

def self.parse_stream(yaml, filename: nil, &block)
  filename ||= yaml.respond_to?(:path) ? yaml.path : "<unknown>"
  yaml = yaml.read if yaml.respond_to?(:read)
  ast = Psych.parse_stream(yaml, filename: filename)
  Analyzer.new(yaml).visit(ast)
  if block_given?
    ast.children.each do |doc|
      block.(doc)
    end
    nil
  else
    ast
  end
end