Sha256: f82428c2af4bc7c2ab4f3c490f7b928003e33028870aaa8b65c6fbd2f6a8fd0b

Contents?: true

Size: 962 Bytes

Versions: 11

Compression:

Stored size: 962 Bytes

Contents

require 'itamae'
require 'erb'
require 'tempfile'

module Itamae
  module Resource
    class Template < File
      define_attribute :source, type: String, required: true
      define_attribute :variables, type: Hash, default: {}

      def pre_action
        src = ::File.expand_path(attributes.source, ::File.dirname(@recipe.path))
        attributes.content = RenderContext.new(self).render_file(src)

        super
      end

      class RenderContext
        def initialize(resource)
          @resource = resource

          @resource.attributes.variables.each_pair do |key, value|
            instance_variable_set("@#{key}".to_sym, value)
          end
        end

        def render_file(src)
          template = ::File.read(src)
          ERB.new(template, nil, '-').tap do |erb|
            erb.filename = src
          end.result(binding)
        end

        def node
          @resource.recipe.runner.node
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
itamae-1.1.19 lib/itamae/resource/template.rb
itamae-1.1.18 lib/itamae/resource/template.rb
itamae-1.1.17 lib/itamae/resource/template.rb
itamae-1.1.16 lib/itamae/resource/template.rb
itamae-1.1.15 lib/itamae/resource/template.rb
itamae-1.1.14 lib/itamae/resource/template.rb
itamae-1.1.13 lib/itamae/resource/template.rb
itamae-1.1.12 lib/itamae/resource/template.rb
itamae-1.1.11 lib/itamae/resource/template.rb
itamae-1.1.10 lib/itamae/resource/template.rb
itamae-1.1.9 lib/itamae/resource/template.rb