Class: RAML::EvalParser

Inherits:
Object
  • Object
show all
Defined in:
lib/raml/eval_parser.rb

Overview

The EvalParser parses RAML documents using standard Ruby evaluation. This has some limitations, but also some benefits.

Unlike the pure data evaluator, the ruby evaluator can be instructed to keep methods available for access, as well as use a custom scope.

Defined Under Namespace

Modules: MethodMissing

Constant Summary collapse

SAFE =
4
HASH =

Need tainted data store.

{}.taint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EvalParser

You can pass in an object to act as the scope. All non-essential public and private Object methods will be removed from the scope unless a ‘keep` string or regex matches the name. Protected methods are also kept intact.



31
32
33
34
35
36
37
38
39
# File 'lib/raml/eval_parser.rb', line 31

def initialize(options={})
  @options   = options

  self.safe      = options[:safe]
  #self.file      = options[:file]
  self.keep      = options[:keep]
  self.scope     = options[:scope] || BasicObject.new
  self.multi_key = options[:multikey]
end

Instance Attribute Details

#keepObject

Returns Array<Regexp>.



57
58
59
# File 'lib/raml/eval_parser.rb', line 57

def keep
  @keep
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/raml/eval_parser.rb', line 26

def options
  @options
end

#scopeObject

Returns [BasicObject,Object] scope object.



77
78
79
# File 'lib/raml/eval_parser.rb', line 77

def scope
  @scope
end

Instance Method Details

#multi_key=(bool) ⇒ Object



72
73
74
# File 'lib/raml/eval_parser.rb', line 72

def multi_key=(bool)
  @multi_key = !!bool
end

#multi_key?Boolean

Returns [Boolean] true/false.

Returns:

  • (Boolean)


67
68
69
# File 'lib/raml/eval_parser.rb', line 67

def multi_key?
  @multi_key
end

#parse(code = nil, file = nil, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/raml/eval_parser.rb', line 99

def parse(code=nil, file=nil, &block)
  data = HASH.dup

  scope.instance_eval{ @__data__ = data}

  result = nil

  thread = Thread.new do
    $SAFE = safe_level unless $SAFE == safe_level
    result = if block
      scope.instance_eval(&block)
    else
      scope.instance_eval(code, file)
    end
  end

  thread.join

  return result if data.empty?  # good idea?
  return data
end

#safe=(boolean) ⇒ Object



47
48
49
# File 'lib/raml/eval_parser.rb', line 47

def safe=(boolean)
  @safe = !!boolean
end

#safe?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/raml/eval_parser.rb', line 42

def safe?
  @safe
end

#safe_levelObject

Returns 4 is safe is true, otherwise 0.



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

def safe_level
  safe? ? 4 : 0
end