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 pathtemplate.rb
More file actions
55 lines (45 loc) · 1.31 KB
/
template.rb
File metadata and controls
55 lines (45 loc) · 1.31 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
require 'active_support/core_ext/string'
module Raml
class Template < ValueNode
# @private
def interpolate(params)
name = @name.clone
data = clone_data
interpolate_params name, params
interpolate_params data, params
[ name, data ]
end
private
def clone_data
# ugly but effective
Marshal.load Marshal.dump @value.dup
end
def interpolate_params(value, params)
case value
when String
interpolate_params_string value, params
when Hash
value.map! { |key,val| [ interpolate_params(key, params), interpolate_params(val, params) ] }
when Array
value.map! { |val| interpolate_params val, params }
else
value
end
end
def interpolate_params_string(value, params)
value = value.dup if value.frozen?
value.gsub!(/(<<([^!\s>]+)(?:\s*\|\s*!(\w+))?>>)/) do |match|
param_name = $2
function = $3
param = params[param_name]
raise UnknownTypeOrTraitParameter, "#{param_name} is not a known parameter." if param.nil?
if function
raise UnknownTypeOrTraitParamFunction, function unless [ 'singularize', 'pluralize'].include? function
param = param.send function
end
param
end
value
end
end
end