Sha256: 337095cd30c482aa86996fa93e25784610dc2713415fba73b3994a44da5c1424

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

module Ufo
  class TemplateScope
    attr_reader :helper
    def initialize(helper=nil)
      @helper = helper
      load_variables_file("base")
      load_variables_file(Ufo.env)
    end

    # Load the variables defined in ufo/variables/* to make available in the
    # template blocks in ufo/templates/*.
    #
    # Example:
    #
    #   `ufo/variables/base.rb`:
    #     @name = "docker-process-name"
    #     @image = "docker-image-name"
    #
    #   `ufo/templates/main.json.erb`:
    #   {
    #     "containerDefinitions": [
    #       {
    #          "name": "<%= @name %>",
    #          "image": "<%= @image %>",
    #      ....
    #   }
    #
    # NOTE: Only able to make instance variables avaialble with instance_eval
    #   Wasnt able to make local variables available.
    def load_variables_file(filename)
      path = "#{Ufo.root}/.ufo/variables/#{filename}.rb"
      instance_eval(IO.read(path)) if File.exist?(path)
    end

    def assign_instance_variables
      # copy over the instance variables to make available in RenderMePretty's scope
      hash = {}
      instance_variables.each do |var|
        key = var.to_s.sub('@','') # rid of the leading @
        hash[key.to_sym] = instance_variable_get(var)
      end
      hash
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ufo-3.4.4 lib/ufo/template_scope.rb
ufo-3.4.3 lib/ufo/template_scope.rb
ufo-3.4.2 lib/ufo/template_scope.rb
ufo-3.4.1 lib/ufo/template_scope.rb
ufo-3.4.0 lib/ufo/template_scope.rb