text-interpolator 1.1.3 → 1.1.4
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 +23 -1
- data/lib/text_interpolator/version.rb +1 -1
- data/spec/text_interpolator_spec.rb +38 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzM4N2Q0MmFiMWM0YjVmM2U1ODAxYWRmZGRkNzA0ZTQ2NmMzMGIwNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmFiYzM1YTViMzgzOGE1ZjEzZTdkZDkwZjQxY2ZhYjM5MjE1YzgyOA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTc4YWYyMzNmMmJjY2E4YTZiOGQyZDE4ODllYjA1NGU5ZjlhYjYzMzczOWYx
|
10
|
+
OWM5YWFiNGRjMzE5ZDhlYjE0ZmY2Mzc1YzU2ZjEwZGM2NjkyZjVhNDhjYWUy
|
11
|
+
MTJhMzM1NWU4NjFkZDYyNDRhNzg4MTMwNGNhM2M3YjY3N2Y0YWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzdhOTJiMTBkOTljZmM2MGRmNGRkZGZmOTM4N2RkMGNkNjc3MWY0ODZjMWE5
|
14
|
+
MWQxN2RhNGUyMjU3ZGQwMzE2MTBhN2U1YzU1MWZlNDY2YWE5ZThjNTNiZGVj
|
15
|
+
MjM2ZTA4NzdmNzBlNWI5YjJjODg2N2VlN2NiYzEzMDQ3Nzg2NmQ=
|
data/CHANGES
CHANGED
@@ -25,10 +25,14 @@ class TextInterpolator
|
|
25
25
|
def interpolate_string string, env={}
|
26
26
|
clear_errors
|
27
27
|
|
28
|
+
env = symbolize_keys env
|
29
|
+
|
28
30
|
value = interpolate_system_variable string
|
29
31
|
|
32
|
+
var_table = build_variables_table(env) # one-dimensional collection of variables
|
33
|
+
|
30
34
|
begin
|
31
|
-
new_value = interpolate_variable value,
|
35
|
+
new_value = interpolate_variable value, var_table
|
32
36
|
rescue KeyError => e
|
33
37
|
new_value = string
|
34
38
|
errors << e.message
|
@@ -169,4 +173,22 @@ class TextInterpolator
|
|
169
173
|
StringIO.new(new_value).read % env
|
170
174
|
end
|
171
175
|
|
176
|
+
def symbolize_keys hash
|
177
|
+
new_hash = {}
|
178
|
+
|
179
|
+
hash.each do |key, value|
|
180
|
+
new_value = if value.kind_of? String
|
181
|
+
value
|
182
|
+
elsif value.kind_of? Hash
|
183
|
+
symbolize_keys value
|
184
|
+
else
|
185
|
+
value
|
186
|
+
end
|
187
|
+
|
188
|
+
new_hash[key.to_sym] = new_value
|
189
|
+
end
|
190
|
+
|
191
|
+
new_hash
|
192
|
+
end
|
193
|
+
|
172
194
|
end
|
@@ -5,16 +5,22 @@ require 'text_interpolator'
|
|
5
5
|
describe TextInterpolator do
|
6
6
|
|
7
7
|
describe "#interpolate_string" do
|
8
|
-
it "interpolates string with %{} tokens" do
|
9
|
-
env = {name1: '
|
8
|
+
it "interpolates string with %{} symbol tokens" do
|
9
|
+
env = {name1: 'value1', name2: 'value2'}
|
10
10
|
|
11
|
-
expect(subject.interpolate_string "%{name1}, %{name2}", env).to eq "
|
11
|
+
expect(subject.interpolate_string "%{name1}, %{name2}", env).to eq "value1, value2"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "interpolates string with %{} string tokens" do
|
15
|
+
env = {'name1' => 'value1', 'name2' => 'value2'}
|
16
|
+
|
17
|
+
expect(subject.interpolate_string "%{name1}, %{name2}", env).to eq "value1, value2"
|
12
18
|
end
|
13
19
|
|
14
20
|
it "interpolates string with '#{}' token" do
|
15
|
-
env = {name1: '
|
21
|
+
env = {name1: 'value1', name2: 'value2'}
|
16
22
|
|
17
|
-
expect(subject.interpolate_string '#{name1}, #{name2}', env).to eq "
|
23
|
+
expect(subject.interpolate_string '#{name1}, #{name2}', env).to eq "value1, value2"
|
18
24
|
end
|
19
25
|
|
20
26
|
it "interpolates string with ENV token" do
|
@@ -25,28 +31,46 @@ describe TextInterpolator do
|
|
25
31
|
end
|
26
32
|
|
27
33
|
it "interpolates string with ENV token and white space" do
|
28
|
-
ENV['name1'] = '
|
29
|
-
ENV['name2'] = '
|
34
|
+
ENV['name1'] = 'value1'
|
35
|
+
ENV['name2'] = 'value2'
|
30
36
|
|
31
|
-
expect(subject.interpolate_string " ENV['name1'], ENV['name2']").to eq "
|
37
|
+
expect(subject.interpolate_string " ENV['name1'], ENV['name2']").to eq " value1, value2"
|
32
38
|
end
|
33
39
|
|
34
40
|
it "interpolates string with mixed tokens" do
|
35
|
-
env = {name1: '
|
36
|
-
ENV['name3'] = '
|
41
|
+
env = {name1: 'value1', name2: 'value2'}
|
42
|
+
ENV['name3'] = 'value3'
|
43
|
+
|
44
|
+
expect(subject.interpolate_string '#{name1}, #{name2}, ENV[\'name3\']', env).to eq "value1, value2, value3"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "interpolates string with multi-level tokens" do
|
48
|
+
env = {"name1" => 'value1', 'level2.name2' => 'value2'}
|
37
49
|
|
38
|
-
|
50
|
+
result = subject.interpolate_string '#{name1}, #{level2.name2}', env
|
51
|
+
|
52
|
+
expect(subject.errors).to be_empty
|
53
|
+
expect(result).to eq "value1, value2"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "interpolates string with multi-level tokens (part 2)" do
|
57
|
+
env = {name1: 'value1', level2: {name2: 'value2'}}
|
58
|
+
|
59
|
+
result = subject.interpolate_string '#{name1}, #{level2.name2}', env
|
60
|
+
|
61
|
+
expect(subject.errors).to be_empty
|
62
|
+
expect(result).to eq "value1, value2"
|
39
63
|
end
|
40
64
|
end
|
41
65
|
|
42
66
|
describe "#interpolate_io" do
|
43
67
|
it "interpolates io stream" do
|
44
|
-
env = {name1: '
|
45
|
-
ENV['name3'] = '
|
68
|
+
env = {name1: 'value1', name2: 'value2'}
|
69
|
+
ENV['name3'] = 'value3'
|
46
70
|
|
47
71
|
io = StringIO.new '#{name1}, #{name2}, ENV[\'name3\']'
|
48
72
|
|
49
|
-
expect(subject.interpolate_io io, env).to eq "
|
73
|
+
expect(subject.interpolate_io io, env).to eq "value1, value2, value3"
|
50
74
|
end
|
51
75
|
end
|
52
76
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Shvets
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gemspec_deps_gen
|