Skip to content

Instantly share code, notes, and snippets.

@vadv
Created November 23, 2013 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadv/7613799 to your computer and use it in GitHub Desktop.
Save vadv/7613799 to your computer and use it in GitHub Desktop.
require 'ostruct'
module ErbCompile
def erb_compile(atr)
case
when atr.kind_of?(Hash), atr.kind_of?(Array) # Chef::Node::ImmutableArray
JSON.parse erb_compile(JSON.generate(JSON.parse(atr.to_json)))
when atr.kind_of?(String)
values = OpenStruct.new({:node => self.node})
ERB.new(atr).result(values.instance_eval{ binding })
when atr.kind_of?(Integer), atr.kind_of?(Float), atr.kind_of?(TrueClass), atr.kind_of?(FalseClass), atr.kind_of?(NilClass)
atr
else
raise "[ErbCompile::erb_compile] #{atr.class} not supported yet"
end
end
end
[ Chef::Recipe, Chef::Mixin::Template::TemplateContext ].each do |klass|
klass.send(:include, ErbCompile) unless klass.respond_to?('erb_compile')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment