itamae 1.0.0.beta44 → 1.0.0.beta45

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17f9bf959f475d6e6850dbc37dacc9cf0a94bd49
4
- data.tar.gz: 773577a6b48749d2e2bb8f69613707a3c93bbb49
3
+ metadata.gz: 30928582ba392469a4486a627a338679a043325d
4
+ data.tar.gz: ebe9aa8b1277b3ff6bd1fc24eb90e3d4bdbdb09a
5
5
  SHA512:
6
- metadata.gz: 73ad64bc7db28c8e27cc68ab19d617fd6a81af7bede6ab28535899891730dd2cd398ad21150abc171ea0ddb20d3c0f80b57a1e2341ac8f9df3d7f1a50d43fa91
7
- data.tar.gz: 3e2b064d6a2910cf159c03ea06868aba3fdcb3300d50d4431e899ddb51ec2a22f469c08b776f2b237e1fc165eaae1639901830d0ca1b84dce43eef2f73f2a48e
6
+ metadata.gz: 7c520cec281a78ac8adb14dbe3f65c9736b9a2dadd810d6fc38e3e12a5e03484444404ee9cf068125d680a6c36e7cc4c77004757e46d844c83af1f4a666c2aa7
7
+ data.tar.gz: 642f82a7545b77029ebce1372996eb18488864d315b9e302a5a0e4a39b702e47bec32231d5af8ff649fdf4cd77c42992e076613ad9c320274729294918d550ce
@@ -6,13 +6,36 @@ module Itamae
6
6
  module Resource
7
7
  class Template < File
8
8
  define_attribute :source, type: String, required: true
9
+ define_attribute :variables, type: Hash, default: {}
9
10
 
10
11
  def pre_action
11
12
  src = ::File.expand_path(attributes.source, ::File.dirname(@recipe.path))
12
- attributes.content = ERB.new(::File.read(src), nil, '-').result(binding)
13
+ attributes.content = RenderContext.new(self).render_file(src)
13
14
 
14
15
  super
15
16
  end
17
+
18
+ class RenderContext
19
+ def initialize(resource)
20
+ @resource = resource
21
+
22
+ @resource.attributes.variables.each_pair do |key, value|
23
+ instance_variable_set("@#{key}".to_sym, value)
24
+ end
25
+ end
26
+
27
+ def render(template)
28
+ ERB.new(template, nil, '-').result(binding)
29
+ end
30
+
31
+ def render_file(src)
32
+ render(::File.read(src))
33
+ end
34
+
35
+ def node
36
+ @resource.recipe.runner.node
37
+ end
38
+ end
16
39
  end
17
40
  end
18
41
  end
@@ -1 +1 @@
1
- 1.0.0.beta44
1
+ 1.0.0.beta45
@@ -32,6 +32,7 @@ end
32
32
  describe file('/tmp/template') do
33
33
  it { should be_file }
34
34
  its(:content) { should match(/Hello/) }
35
+ its(:content) { should match(/Good bye/) }
35
36
  end
36
37
 
37
38
  describe file('/tmp/file') do
@@ -74,6 +74,7 @@ end
74
74
 
75
75
  template "/tmp/template" do
76
76
  source "hello.erb"
77
+ variables(goodbye: "Good bye")
77
78
  end
78
79
 
79
80
  file "/tmp/file" do
@@ -1 +1,2 @@
1
1
  <%= node['greeting'] %>
2
+ <%= @goodbye %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itamae
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta44
4
+ version: 1.0.0.beta45
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryota Arai