This repository was archived by the owner on Mar 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathparser.rb
More file actions
59 lines (48 loc) · 1.41 KB
/
parser.rb
File metadata and controls
59 lines (48 loc) · 1.41 KB
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
require 'yaml'
module Raml
# @private
class Parser
class << self
def parse(data, file_dir=Dir.getwd)
register_include_tag
data = YAML.load data
expand_includes data, file_dir
Root.new data
end
private
def register_include_tag
YAML.add_tag '!include', Raml::Parser::Include
end
def expand_includes(val, cwd)
case val
when Hash
val.merge!(val, &expand_includes_transform(cwd))
when Array
val.map!(&expand_includes_transform(cwd))
end
end
def expand_includes_transform(cwd)
proc do |arg1, arg2|
val = arg2.nil? ? arg1 : arg2
child_wd = cwd
if val.is_a? Raml::Parser::Include
child_wd = expand_includes_working_dir cwd, val.path
path = val.path
val = val.content cwd
val.define_singleton_method(:file_path) { child_wd + "/" + path.split("/").last }
end
expand_includes val, child_wd
val
end
end
def expand_includes_working_dir(current_wd, include_pathname)
include_path = File.dirname include_pathname
if include_path.start_with? '/'
include_path
else
"#{current_wd}/#{include_path}"
end
end
end
end
end