Module: RAML

Defined in:
lib/raml.rb,
lib/raml/eval_parser.rb,
lib/raml/multi_value.rb,
lib/raml/ripper_parser.rb

Defined Under Namespace

Classes: EvalParser, MultiValue, RipperParser

Class Method Summary collapse

Class Method Details

.eval(io, options = {}) ⇒ Object

Evaluate a RAML document. Like ‘load()` but parses the document via #eval. (same as load with :eval=>true). This can be done a $SAFE level 4 by setting the :safe option to true.

Arguments

io - a String, File or any object that responds to #read.

Options

:safe     - true/false
:keep     - public methods to keep in scope
:scope    - an object to act as the evaluation context
:multikey - handle duplicate keys

Returns [Hash] data parsed from document.



52
53
54
55
56
# File 'lib/raml.rb', line 52

def self.eval(io, options={})
  code, file = io(io)
  parser = RAML::EvalParser.new(options)
  parser.parse(code, file)
end

.io(io) ⇒ Object (private)

Take a String, File, IO or any object that respons to #read and return the string or result of calling #read and the file name if any and “(eval)” if not.

Arguments

io - a String, File or any object that responds to #read.

Returns [String, String] text of string or file and file name or “(eval)”.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/raml.rb', line 95

def self.io(io)
  case io
  when String
    file = '(eval)'
    code = io
  when File
    file = io.path
    code = io.read
  else #IO
    file = '(eval)'
    code = io.read
  end
  return code, file
end

.load(io, options = {}) ⇒ Object

Load a RAML document. Like ‘eval()` but parses the document via Ripper, ensuring a pure data format.

IMPORTANT: Ruby 1.8.x and older does not support Ripper. In this case RAML falls back to using ‘eval()` with $SAFE = 4.

Arguments

io - a String, File or any object that responds to #read.

Options

:eval     - false for data-only parser
:safe     - true sets $SAFE=4
:keep     - public methods to keep in scope
:scope    - an object to act as the evaluation context
:multikey - handle duplicate keys

Returns [Hash] data parsed from document.



28
29
30
31
32
33
34
# File 'lib/raml.rb', line 28

def self.load(io, options={})
  if FalseClass === options[:eval]
    read(io, options)
  else
    eval(io, options)
  end
end

.read(io, options = {}) ⇒ Object

Read in a RAML document. Like ‘load()` but parses the document via Ripper (same as load with :eval=>false). This only work in Ruby 1.9+, otherwise it falls back to the eval parer with `:safe=>true`.

Arguments

io - a String, File or any object that responds to #read.

Options

:keep     - public methods to keep in scope
:scope    - an object to act as the evaluation context
:multikey - handle duplicate keys

Returns [Hash] data parsed from document.



73
74
75
76
77
78
79
80
81
82
# File 'lib/raml.rb', line 73

def self.read(io, options={})
  code, file = io(io)
  if RUBY_VERSION > '1.9'
    parser = RAML::RipperParser.new(options)
  else
    options[:safe] = true
    parser = RAML::EvalParser.new(options)
  end
  parser.parse(code, file)
end