Sunday, 15 April 2012

Ruby .erb template from hash, but catch unset variables -



Ruby .erb template from hash, but catch unset variables -

i'm bit of ruby-newbie, i'm trying render puppet .erb templates using script , load of synthesised info (which i'll set in yaml file or something). our puppet templates along these sorts of lines:

# config file <%= @some_ordinary_variable %> <%= some_other_variable %> <%= @something_undefined %> <%= scope.function_hiera("some_hiera_variable") %>

i've got far mocking hiera lookups, , found problem using openstruct erb way substitute in "some_other_variable" (i'm bit stuck on getting "@some_ordinary_variable" work, think can figure 1 out.

what i'm asking how can utilize binding lets me run bit of code each variable lookup? i'm thinking i'd run like:

def variable_lookup(key) if @variables.has_key?(key) homecoming @variables[key] else warn "the variable " + key + " required template not set" end end

i merge hiera mock-up lookup hiera data. code have far is:

require 'rubygems' require 'yaml' require 'erb' require 'ostruct' class erbbinding < openstruct include enumerable attr_accessor :yamlfile, :hiera_config, :erbfile, :yaml def scope self end def function_hiera(key) val = @yaml['values'][key] if val.is_a?(yaml::syck::scalar) homecoming val.value else warn "erby: " + key + " required in template not set in yaml" homecoming nil end end def get_binding homecoming binding() end end variables = {'some_other_variable' => 'hello world'} hs = erbbinding.new(variables) template = file.read('./template.erb') hs.yaml = yaml.parse( file.read('./data.yaml') ) erb = erb.new(template) vars_binding = hs.send(:get_binding) puts erb.result(vars_binding)

i can't figure out how set binding runs code, rather using 'variables' hash. ideas?

this turned out surprisingly simple! found can utilize special method "method_missing" scoop method calls aren't defined. is, we're using binding create hash object (with each hash key becoming method in object). if there's missing method (ie. no key set in hash), ruby calls "method_missing" - i've done got method name missing. if found of import bit of info here: http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html

if you're wondering, here's total code have far...

require 'rubygems' require 'yaml' require 'erb' require 'ostruct' class erbbinding < openstruct include enumerable attr_accessor :yamlfile, :hiera_config, :erbfile, :yaml def method_missing(m, *args, &block) warn "erby: variable #{m} required not set in yaml" end def scope self end def function_hiera(key) val = @yaml['values'][key] if val.is_a?(yaml::syck::scalar) homecoming val.value else warn "erby: " + key + " required in template not set in yaml" homecoming nil end end def get_binding homecoming binding() end end variables = {'some_other_variable' => 'hello world'} hs = erbbinding.new(variables) template = file.read('./template.erb') hs.yaml = yaml.parse( file.read('./data.yaml') ) erb = erb.new(template) vars_binding = hs.send(:get_binding) puts erb.result(vars_binding)

this code still won't handle template <%= @variable %>, it'll handle other 2 cases in original question.

ruby erb

No comments:

Post a Comment