Class: Ripe::Blocks::ERBBlock

Inherits:
WorkingBlock show all
Defined in:
lib/ripe/blocks/erb_block.rb

Overview

This class represents a working block that should be processed using the ERB templating system.

Keys defined as:

vars["some_key"] = "value"

can be substituted in the ERB template using:

<%= vars.some_key %>

See Also:

Instance Attribute Summary

Attributes inherited from Block

#blocks, #id, #vars

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WorkingBlock

#prune, subclasses, #targets_exist?, #topology

Methods inherited from Block

#+, #prune, #targets_exist?, #topology, #|

Constructor Details

#initialize(filename, vars = {}) ⇒ ERBBlock

Create a new, empty LiquidBlock.

Parameters:

  • filename (String)

    filename of the template file

  • vars (Hash<Symbol, String>) (defaults to: {})

    key-value pairs



29
30
31
# File 'lib/ripe/blocks/erb_block.rb', line 29

def initialize(filename, vars = {})
  super(filename, vars)
end

Class Method Details

.extensionString

Return expected file extension type for this type of ‘WorkingBlock`.

Returns:

  • (String)

See Also:



81
82
83
# File 'lib/ripe/blocks/erb_block.rb', line 81

def self.extension
  'sh.erb'
end

.idString

Return string handle for referring to this type of ‘WorkingBlock`.

Returns:

  • (String)

See Also:



70
71
72
# File 'lib/ripe/blocks/erb_block.rb', line 70

def self.id
  'erb'
end

Instance Method Details

#commandString

Return the string command of the subtree starting at the current block.

The resulting string contains the render result of the liquid template based on the parameters specified in vars.

Returns:

  • (String)

    subtree command



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ripe/blocks/erb_block.rb', line 39

def command
  vars = @vars
  vars.define_singleton_method(:get_binding) { binding } # Expose private method
  vars.define_singleton_method(:method_missing) do |name|
    return self[name] if key? name
    self.each { |k,v| return v if k.to_s.to_sym == name }
    # super.method_missing name
  end

  template = <<-EOF.gsub(/^[ ]+/, '')

  # <#{id}>

  exec 1>"<%= vars.log %>" 2>&1

  #{File.new(@filename).read}
  echo "##.DONE.##"

  # </#{id}>
  EOF

  ERB.new(template).result(vars.get_binding)
end