Module: NetLinx::ERB
- Defined in:
- lib/netlinx/erb.rb,
lib/netlinx/erb/erb.rb,
lib/netlinx/erb/helpers.rb,
lib/netlinx/erb/hash_helpers.rb
Overview
NetLinx code generation.
Defined Under Namespace
Modules: HashHelpers, Helpers
Class Method Summary collapse
-
.binding {|module| ... } ⇒ Object
A binding for ERB to evaluate code in.
-
.execute(template_file, binding: NetLinx::ERB.binding) ⇒ String
Run ERB on the given template file.
-
.generate_axi_files_from_template(devices, **kwargs) {|module| ... } ⇒ Object
Generate AXI files for multiple devices based on a template file.
Class Method Details
.binding {|module| ... } ⇒ Object
Returns a binding for ERB to evaluate code in.
9 10 11 12 13 14 |
# File 'lib/netlinx/erb/erb.rb', line 9 def self.binding &block Module.new.tap { |mod| mod.instance_eval { extend NetLinx::ERB::Helpers } block.call mod if block_given? }.instance_eval { binding } end |
.execute(template_file, binding: NetLinx::ERB.binding) ⇒ String
Run ERB on the given template file.
21 22 23 24 25 |
# File 'lib/netlinx/erb/erb.rb', line 21 def self.execute template_file, binding: NetLinx::ERB.binding raise LoadError, "Template not found: #{template_file}" unless File.exists? template_file $AUTOGEN_HEADER + ::ERB.new(File.read(template_file), nil, '%<>-') .result(binding) end |
.generate_axi_files_from_template(devices, **kwargs) {|module| ... } ⇒ Object
Generate AXI files for multiple devices based on a template file.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/netlinx/erb/erb.rb', line 41 def self.generate_axi_files_from_template devices, **kwargs, &block template_dir = File. kwargs.fetch(:template_dir, './include/ui/template') template_files = Dir["#{template_dir}/*.axi.erb.tmpl"] devices.each do |name, params| b = self.binding do |m| m.instance_eval { extend NetLinx::ERB::Helpers # Generate template instance variables. @tmpl_suffix = name.to_s @tmpl_var_suffix = name.to_s.downcase # Variable suffix. @tmpl_suffixes = devices.keys @tmpl_var_suffixes = devices.keys.map &:downcase params.each { |k,v| instance_variable_set :"@tmpl_#{k.to_s.downcase}", v } @dvTP = "dvTP_#{@tmpl_suffix}" # Inject suffix into Hash for helper methods. Hash.instance_variable_set :@tmpl_suffix, @tmpl_suffix Hash.instance_variable_set :@tmpl_var_suffix, @tmpl_var_suffix } block.call m if block_given? end # Generate files. template_files.each do |path| file_base_name = File.basename(path).gsub(/\..*/, '') output_dir = File. "#{template_dir}/.." output_path = "#{output_dir}/#{file_base_name}-#{name.to_s.downcase.gsub('_', '-')}.axi" puts ' ' + output_path.gsub(Dir.pwd, '')[1..-1] # Print canonical path. File.write output_path, execute(path, binding: b) end end end |