Sha256: ae0f52525e6cd40b59afa7ac115d3c855c5ed427831110a39233b422c6be54b1

Contents?: true

Size: 943 Bytes

Versions: 28

Compression:

Stored size: 943 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(template)
          ERB.new(template, nil, '-').result(binding)
        end

        def render_file(src)
          render(::File.read(src))
        end

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

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
itamae-1.1.8 lib/itamae/resource/template.rb
itamae-1.1.7 lib/itamae/resource/template.rb
itamae-1.1.5 lib/itamae/resource/template.rb
itamae-1.1.4 lib/itamae/resource/template.rb
itamae-1.1.3 lib/itamae/resource/template.rb
itamae-1.1.2 lib/itamae/resource/template.rb
itamae-1.1.1 lib/itamae/resource/template.rb
itamae-1.1.0 lib/itamae/resource/template.rb
itamae-1.0.13 lib/itamae/resource/template.rb
itamae-1.0.12 lib/itamae/resource/template.rb
itamae-1.0.11 lib/itamae/resource/template.rb
itamae-1.0.10 lib/itamae/resource/template.rb
itamae-1.0.9 lib/itamae/resource/template.rb
itamae-1.0.8 lib/itamae/resource/template.rb
itamae-1.0.7 lib/itamae/resource/template.rb
itamae-1.0.6 lib/itamae/resource/template.rb
itamae-1.0.5 lib/itamae/resource/template.rb
itamae-1.0.4 lib/itamae/resource/template.rb
itamae-1.0.3 lib/itamae/resource/template.rb
itamae-1.0.2 lib/itamae/resource/template.rb