File tree 3 files changed +25
-19
lines changed
3 files changed +25
-19
lines changed Original file line number Diff line number Diff line change @@ -7,22 +7,32 @@ module Boxing
7
7
class Generator
8
8
include Utils
9
9
10
+ # @since 0.11.0
11
+ def initialize ( destination , content )
12
+ pp current_path
13
+ @destination = current_path . join ( destination )
14
+ @content = content
15
+ end
16
+
10
17
# Generate file
11
18
#
12
19
# @since 0.11.0
13
- def execute ( destination , content = nil )
14
- content = yield if block_given?
15
- destination = current_path . join ( destination )
16
- write ( destination , content )
20
+ def execute
21
+ FileUtils . mkdir_p ( File . dirname ( @destination ) )
22
+ File . write ( @destination , render )
17
23
end
18
24
19
- # Write file to relative path
25
+ # Render content
20
26
#
21
- # @param [String] destination
22
- # @param [String] content
23
- def write ( destination , content )
24
- FileUtils . mkdir_p ( File . dirname ( destination ) )
25
- File . write ( destination , content )
27
+ # @return [String]
28
+ #
29
+ # @since 0.11.0
30
+ def render
31
+ @render ||= if @content . is_a? ( Proc )
32
+ @content . call
33
+ else
34
+ @content
35
+ end
26
36
end
27
37
end
28
38
end
Original file line number Diff line number Diff line change @@ -27,9 +27,7 @@ def current_path
27
27
#
28
28
# @since 0.11.0
29
29
def template ( destination , template , context : nil )
30
- Generator . new . execute ( destination ) do
31
- Template . new ( template ) . render ( context )
32
- end
30
+ Generator . new ( destination , -> { Template . new ( template ) . render ( context ) } ) . execute
33
31
end
34
32
end
35
33
end
Original file line number Diff line number Diff line change 3
3
require 'tmpdir'
4
4
5
5
RSpec . describe Boxing ::Generator do
6
- subject ( :generator ) { described_class . new }
6
+ subject ( :generator ) { described_class . new ( 'Dockerfile' , 'test' ) }
7
7
8
8
let ( :tmpdir ) { Pathname . new ( Dir . mktmpdir ) }
9
9
10
- before do
11
- allow ( generator ) . to receive ( :current_path ) . and_return ( tmpdir )
12
-
13
- generator . execute ( 'Dockerfile' , 'test' )
14
- end
10
+ around { |example | Dir . chdir ( tmpdir ) { example . run } }
15
11
after { FileUtils . remove_entry ( tmpdir ) }
16
12
13
+ before { generator . execute }
14
+
17
15
it 'creates a file' do
18
16
expect ( File . exist? ( File . join ( tmpdir , 'Dockerfile' ) ) ) . to be true
19
17
end
You can’t perform that action at this time.
0 commit comments