text-interpolator 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGES +5 -1
- data/lib/text_interpolator/text_interpolator.rb +130 -9
- data/lib/text_interpolator/version.rb +1 -1
- metadata +1 -2
- data/lib/text_interpolator/hash_processor.rb +0 -125
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODFiMjU4MGFiYTgyMTJjMDg1ODAwODZkODk0OTU1YjkyMzBhMTI4NA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjZkNzBiYmYxNThmNDU3ZGFjNjg5MTA1NjIzMTA4N2FkYjM5MTI0NQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YTM4OTRmYjMwMDE5ODc5NTIyZDA2Y2UzOTdiM2RhY2EzNzExZDg2OWJjYzkz
|
10
|
+
YmY3YjlmMGRhODM3MGEyYmJlODAxMWJiMmEyMjE2YjMzMDRkYzMyZTljMDk5
|
11
|
+
OGFkMGU2ZmE3NWQwNzRmZWZjNmEyMDM2ZTUxZmUzYzYwODMzYWQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Nzk5Yzc1Y2IyZTYwMzRlMjVmYzNhOWUxMTdkMTMwNjcyNmRiM2U1NDdiNjNh
|
14
|
+
NDQxYmM1YmJmNDkwN2RjZTMzNjAxYWQ4ODIzNTZmYTAxYjk2MDUyZTIxYWFl
|
15
|
+
OTg2NmM0ZTNmOTZiOTdiZGExYWE5MTU2MDcxMTE1ZTQ3ZGZkZDc=
|
data/CHANGES
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'stringio'
|
2
|
-
require 'text_interpolator/hash_processor'
|
3
2
|
|
4
3
|
class TextInterpolator
|
5
4
|
|
@@ -8,20 +7,37 @@ class TextInterpolator
|
|
8
7
|
def interpolate object, env={}
|
9
8
|
if object.kind_of? String
|
10
9
|
interpolate_string object, env
|
11
|
-
elsif object.kind_of? IO
|
12
|
-
interpolate_io object, env
|
13
10
|
elsif object.kind_of? Hash
|
14
11
|
interpolate_hash object
|
12
|
+
elsif object.kind_of? IO
|
13
|
+
interpolate_io object, env
|
15
14
|
else
|
16
15
|
object
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
def interpolate_string string, env={}
|
21
|
-
|
22
|
-
|
20
|
+
@errors = {}
|
21
|
+
|
22
|
+
value = interpolate_system_variable string
|
23
|
+
|
24
|
+
begin
|
25
|
+
new_value = interpolate_variable value, env
|
26
|
+
rescue KeyError => e
|
27
|
+
errors << e.message
|
28
|
+
end
|
29
|
+
|
30
|
+
new_value
|
31
|
+
end
|
32
|
+
|
33
|
+
def interpolate_hash hash
|
34
|
+
@errors = {}
|
35
|
+
|
36
|
+
content = interpolate_system_variables(hash)
|
37
|
+
|
38
|
+
var_table = build_variables_table(content) # one-dimensional collection of variables
|
23
39
|
|
24
|
-
|
40
|
+
interpolate_variables(content, var_table)
|
25
41
|
end
|
26
42
|
|
27
43
|
def interpolate_io io, env={}
|
@@ -35,10 +51,115 @@ class TextInterpolator
|
|
35
51
|
result
|
36
52
|
end
|
37
53
|
|
38
|
-
|
39
|
-
result, @errors = *HashProcessor.instance.process(hash)
|
54
|
+
private
|
40
55
|
|
41
|
-
|
56
|
+
def interpolate_system_variables hash
|
57
|
+
content = {}
|
58
|
+
|
59
|
+
hash.each do |key, value|
|
60
|
+
if value.kind_of? String
|
61
|
+
new_value = interpolate_system_variable value
|
62
|
+
|
63
|
+
content[key] = new_value if new_value
|
64
|
+
elsif value.kind_of? Hash
|
65
|
+
content[key] = interpolate_system_variables value
|
66
|
+
else
|
67
|
+
content[key] = value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
content
|
72
|
+
end
|
73
|
+
|
74
|
+
def interpolate_system_variable value
|
75
|
+
new_value = value
|
76
|
+
|
77
|
+
while new_value.index('ENV[')
|
78
|
+
index1 = new_value.index("ENV[")
|
79
|
+
index2 = new_value.index("]")
|
80
|
+
|
81
|
+
name = new_value[index1+5..index2-2]
|
82
|
+
|
83
|
+
left = (index1 == 0) ? '' : new_value[0..index1-1]
|
84
|
+
right = new_value[index2+1..-1]
|
85
|
+
|
86
|
+
new_value = left + ENV[name] + right
|
87
|
+
end
|
88
|
+
|
89
|
+
new_value
|
90
|
+
end
|
91
|
+
|
92
|
+
def build_variables_table content
|
93
|
+
var_table = {}
|
94
|
+
|
95
|
+
content.each do |key, value|
|
96
|
+
build_variable(var_table, key, key, value)
|
97
|
+
end
|
98
|
+
|
99
|
+
var_table
|
100
|
+
end
|
101
|
+
|
102
|
+
def build_variable(var_table, compound_key, key, value)
|
103
|
+
if value.kind_of? String
|
104
|
+
var_table[key] = interpolate_variable value, var_table
|
105
|
+
elsif value.kind_of? Hash
|
106
|
+
build_hash var_table, compound_key, value
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def build_hash var_table, compound_key, hash
|
111
|
+
hash.each do |key, value|
|
112
|
+
new_compound_key = "#{compound_key}.#{key}"
|
113
|
+
|
114
|
+
if value.kind_of? Hash
|
115
|
+
build_variable(var_table, new_compound_key, key, value)
|
116
|
+
else
|
117
|
+
var_table[new_compound_key.to_sym] = value
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def interpolate_variables content, env
|
123
|
+
hash = {}
|
124
|
+
|
125
|
+
begin
|
126
|
+
substitutions = false
|
127
|
+
|
128
|
+
content.each do |key, value|
|
129
|
+
new_value = value
|
130
|
+
substitutions = false
|
131
|
+
|
132
|
+
if value.kind_of? String
|
133
|
+
if value.index(/\#\{/)
|
134
|
+
substitutions = true
|
135
|
+
|
136
|
+
begin
|
137
|
+
new_value = interpolate_variable value, env
|
138
|
+
rescue KeyError => e
|
139
|
+
substitutions = false
|
140
|
+
|
141
|
+
errors << e.message
|
142
|
+
end
|
143
|
+
end
|
144
|
+
elsif value.kind_of? Hash
|
145
|
+
new_value = interpolate_variables value, env
|
146
|
+
end
|
147
|
+
|
148
|
+
if hash[key] == new_value
|
149
|
+
substitutions = false
|
150
|
+
else
|
151
|
+
hash[key] = new_value
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end while substitutions
|
155
|
+
|
156
|
+
hash
|
157
|
+
end
|
158
|
+
|
159
|
+
def interpolate_variable value, env
|
160
|
+
new_value = value.index(/\#\{/) ? value.gsub(/\#{/, '%{') : value
|
161
|
+
|
162
|
+
StringIO.new(new_value).read % env
|
42
163
|
end
|
43
164
|
|
44
165
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text-interpolator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Shvets
|
@@ -54,7 +54,6 @@ files:
|
|
54
54
|
- README.md
|
55
55
|
- Rakefile
|
56
56
|
- lib/text_interpolator.rb
|
57
|
-
- lib/text_interpolator/hash_processor.rb
|
58
57
|
- lib/text_interpolator/text_interpolator.rb
|
59
58
|
- lib/text_interpolator/version.rb
|
60
59
|
- spec/spec_helper.rb
|
@@ -1,125 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
|
-
class HashProcessor
|
4
|
-
include Singleton
|
5
|
-
|
6
|
-
def process hash
|
7
|
-
errors = {}
|
8
|
-
|
9
|
-
content = interpolate_system_variables(hash)
|
10
|
-
|
11
|
-
var_table = build_variables_table(content) # one-dimensional collection of variables
|
12
|
-
|
13
|
-
result = interpolate_variables(content, var_table, errors)
|
14
|
-
|
15
|
-
[result, errors]
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def interpolate_system_variables hash
|
21
|
-
content = {}
|
22
|
-
|
23
|
-
hash.each do |key, value|
|
24
|
-
if value.kind_of? String
|
25
|
-
new_value = interpolate_system_variable value
|
26
|
-
|
27
|
-
content[key] = new_value if new_value
|
28
|
-
elsif value.kind_of? Hash
|
29
|
-
content[key] = interpolate_system_variables value
|
30
|
-
else
|
31
|
-
content[key] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
content
|
36
|
-
end
|
37
|
-
|
38
|
-
def interpolate_system_variable value
|
39
|
-
new_value = value
|
40
|
-
|
41
|
-
while new_value.index('ENV[')
|
42
|
-
index1 = new_value.index("ENV[")
|
43
|
-
index2 = new_value.index("]")
|
44
|
-
|
45
|
-
name = new_value[index1+5..index2-2]
|
46
|
-
|
47
|
-
left = (index1 == 0) ? '' : new_value[0..index1-1]
|
48
|
-
right = new_value[index2+1..-1]
|
49
|
-
|
50
|
-
new_value = left + ENV[name] + right
|
51
|
-
end
|
52
|
-
|
53
|
-
new_value
|
54
|
-
end
|
55
|
-
|
56
|
-
def build_variables_table content
|
57
|
-
var_table = {}
|
58
|
-
|
59
|
-
content.each do |key, value|
|
60
|
-
build_variable(var_table, key, key, value)
|
61
|
-
end
|
62
|
-
|
63
|
-
var_table
|
64
|
-
end
|
65
|
-
|
66
|
-
def build_variable(var_table, compound_key, key, value)
|
67
|
-
if value.kind_of? String
|
68
|
-
var_table[key] = value
|
69
|
-
elsif value.kind_of? Hash
|
70
|
-
build_hash var_table, compound_key, value
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def build_hash var_table, compound_key, hash
|
75
|
-
hash.each do |key, value|
|
76
|
-
new_compound_key = "#{compound_key}.#{key}"
|
77
|
-
|
78
|
-
if value.kind_of? Hash
|
79
|
-
build_variable(var_table, new_compound_key, key, value)
|
80
|
-
else
|
81
|
-
var_table[new_compound_key.to_sym] = value
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def interpolate_variables content, env, errors
|
87
|
-
hash = {}
|
88
|
-
|
89
|
-
begin
|
90
|
-
substitutions = false
|
91
|
-
|
92
|
-
content.each do |key, value|
|
93
|
-
new_value = value
|
94
|
-
substitutions = false
|
95
|
-
|
96
|
-
if value.kind_of? String
|
97
|
-
if value.index(/\#\{/)
|
98
|
-
substitutions = true
|
99
|
-
|
100
|
-
new_value = value.gsub(/\#{/, '%{')
|
101
|
-
|
102
|
-
begin
|
103
|
-
new_value = StringIO.new(new_value).read % env
|
104
|
-
rescue KeyError => e
|
105
|
-
substitutions = false
|
106
|
-
|
107
|
-
errors << e.message
|
108
|
-
end
|
109
|
-
end
|
110
|
-
elsif value.kind_of? Hash
|
111
|
-
new_value = interpolate_variables value, env, errors
|
112
|
-
end
|
113
|
-
|
114
|
-
if hash[key] == new_value
|
115
|
-
substitutions = false
|
116
|
-
else
|
117
|
-
hash[key] = new_value
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end while substitutions
|
121
|
-
|
122
|
-
hash
|
123
|
-
end
|
124
|
-
|
125
|
-
end
|