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

Class Method Details

.binding {|module| ... } ⇒ Object

Returns a binding for ERB to evaluate code in.

Examples:

ERB.new(buffer, nil, '%<>-').result(NetLinx::ERB.binding)

Yield Parameters:

  • module (Module)

    the object whose binding is returned.

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.

Parameters:

  • template_file (String)

    file path

  • binding (Binding) (defaults to: NetLinx::ERB.binding)

Returns:

Raises:

  • (LoadError)

    template not found



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.

Examples:

# Sample input for `devices` parameter:
touch_panels = {
    WALL:     { dps: 10001 },
    FLOATING: { dps: 10002 },
}

Parameters:

  • devices (Hash)

    device names, each with a corresponding hash of k/v pairs that will be turned into ‘@tmpl_(key)` for use in the template.

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :template_dir (String) — default: './include/ui/template'

    directory containing ‘.axi.erb.tmpl` template files.

Yield Parameters:

  • module (Module)

    the object whose binding is used when evaluating the template. This can be used to add customized data during evaluation.



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.expand_path 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.expand_path "#{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