writer 0.4.0.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/writer/file_creator.rb +24 -2
- data/lib/writer/version.rb +1 -1
- data/spec/creator_spec.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a3046185551339a7a5cf840529eee79c33a7b59
|
4
|
+
data.tar.gz: 1e3deb73f505f33e03a95559f8c7ddef13f42bb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6b712af7f5480d939e6ca2c2856ab5fefdcbf6e2690d44b5433bce1778d5ca8474411bdbff8d683ab57daf6d392f541de1e1101c18acbc4752f05b6efed66f2
|
7
|
+
data.tar.gz: 31ab8ede44b3adf1b09f876a0eded87ac91b9f4b55c8ca14734fcb5f4cb042be6a9fc4d62ba98c8b7565448c2292d8fec0a6ff48feeb9ac450e1aee577ff6076
|
data/lib/writer/file_creator.rb
CHANGED
@@ -8,14 +8,36 @@ module Writer
|
|
8
8
|
private
|
9
9
|
def create_file(name, content)
|
10
10
|
File.open(name, 'w') do |f|
|
11
|
-
|
11
|
+
case content
|
12
|
+
when /[A-Z]+=\w+/
|
13
|
+
f.puts template_with_variables(content)
|
14
|
+
when nil
|
15
|
+
f.puts template
|
16
|
+
else
|
17
|
+
f.puts content
|
18
|
+
end
|
12
19
|
end
|
13
20
|
end
|
14
21
|
|
15
22
|
def template
|
16
23
|
unless Writer.template_path.blank?
|
17
|
-
File.open(Writer.template_path).read
|
24
|
+
@template ||= File.open(Writer.template_path).read
|
18
25
|
end
|
19
26
|
end
|
27
|
+
|
28
|
+
def template_with_variables(content)
|
29
|
+
return content if template.nil?
|
30
|
+
modified_template = template
|
31
|
+
|
32
|
+
content.split(' ').each do |k_v|
|
33
|
+
key = k_v.split('=')[0].downcase
|
34
|
+
value = k_v.split('=')[1]
|
35
|
+
puts key
|
36
|
+
puts value
|
37
|
+
modified_template.gsub!(/%{#{key}}/, value)
|
38
|
+
end
|
39
|
+
|
40
|
+
modified_template
|
41
|
+
end
|
20
42
|
end
|
21
43
|
end
|
data/lib/writer/version.rb
CHANGED
data/spec/creator_spec.rb
CHANGED
@@ -18,6 +18,16 @@ module Writer
|
|
18
18
|
creator.create!('filename', 'hi')
|
19
19
|
expect(file).to have_received(:puts).with('hi')
|
20
20
|
end
|
21
|
+
|
22
|
+
context 'with variables' do
|
23
|
+
it 'fills in the variables in the template' do
|
24
|
+
allow(creator).to receive(:template) { "hello\n%{name}, you are %{trait}" }
|
25
|
+
allow(file).to receive(:puts)
|
26
|
+
|
27
|
+
creator.create!('filename', 'NAME=joe TRAIT=cool')
|
28
|
+
expect(file).to have_received(:puts).with("hello\njoe, you are cool")
|
29
|
+
end
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
33
|
context "without content" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Sak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|