Class: Riptables::TableExport

Inherits:
Object
  • Object
show all
Defined in:
lib/riptables/table_export.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, options = {}) ⇒ TableExport

Returns a new instance of TableExport.



4
5
6
7
# File 'lib/riptables/table_export.rb', line 4

def initialize(table, options = {})
  @table = table
  @options = options
end

Instance Method Details

#col(number, text) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/riptables/table_export.rb', line 31

def col(number, text)
  if @options[:color] == false
    text
  else
    "\e[#{number}m#{text}\e[0m"
  end
end

#to_savefile(version = 4) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/riptables/table_export.rb', line 9

def to_savefile(version = 4)
  Array.new.tap do |s|
    s << "*#{col 31, @table.name}"
    @table.chains.each do |_, chain|
      s << ":#{col 32, chain.name.to_s.upcase} #{col 35, chain.default_action.to_s.upcase} [0:0]"
    end

    @table.chains.each do |_, chain|
      chain.rules.map(&:permuted_rules).flatten.each do |rule|
        next unless rule.include?(@options[:conditions] || {})
        next unless rule.versions.include?(version)
        action = rule.action ? "-j #{rule.action.is_a?(Symbol) ? rule.action.upcase : rule.action}" : ''
        comment = "-m comment --comment=\"#{rule.description.gsub('"', '\'')}\""
        s << "-A #{col 32, chain.name.to_s.upcase} #{col 33, rule.rule} #{col 35, action} #{col 36, comment}"
      end
    end

    s << "COMMIT"
    s << "# Compiled by riptables on #{Time.now.strftime("%a %b %e %H:%M:%S %Y")}"
  end.join("\n")
end