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.0.1 lib/itamae/resource/template.rb
itamae-1.0.0 lib/itamae/resource/template.rb
itamae-1.0.0.beta50 lib/itamae/resource/template.rb
itamae-1.0.0.beta49 lib/itamae/resource/template.rb
itamae-1.0.0.beta48 lib/itamae/resource/template.rb
itamae-1.0.0.beta47 lib/itamae/resource/template.rb
itamae-1.0.0.beta46 lib/itamae/resource/template.rb
itamae-1.0.0.beta45 lib/itamae/resource/template.rb