class Havox::Rule

Attributes

actions[R]
dp_id[R]
matches[R]
raw[R]

Public Class Methods

new(raw, opts = {}) click to toggle source
# File lib/havox/classes/rule.rb, line 5
def initialize(raw, opts = {})
  @opts = opts
  @syntax = @opts[:syntax] || :trema
  @matches = parsed_matches(raw)
  @actions = parsed_actions(raw)
  @dp_id = @matches[:dp_id].to_i
  @matches.delete(:dp_id)
  @raw = raw.strip
end

Public Instance Methods

inspect() click to toggle source
# File lib/havox/classes/rule.rb, line 24
def inspect
  "Rule #{object_id.to_s(16)}, dp_id = #{@dp_id}: #{to_s}"                  # Prints the rule when debugging or array listing.
end
to_h() click to toggle source
# File lib/havox/classes/rule.rb, line 28
def to_h
  { dp_id: @dp_id, matches: @matches, actions: @actions }
end
to_s() click to toggle source
# File lib/havox/classes/rule.rb, line 15
def to_s                                                                    # Stringifies the rule.
  sep = ' AND '
  matches_str = @matches.map { |k, v| "#{k.to_s} = #{v.to_s}" }.join(sep)
  actions_str = @actions.map do |o|
    %Q(#{o[:action]}(#{o[:arg_a]}#{", #{o[:arg_b]}" unless o[:arg_b].nil?}))
  end
  "#{matches_str} --> #{actions_str.join(sep)}"
end

Private Instance Methods

already_set?(matches_hash, stmt) click to toggle source
# File lib/havox/classes/rule.rb, line 66
def already_set?(matches_hash, stmt)
  field = translate.fields_to(@syntax)[stmt.first]
  unless matches_hash[field].nil? || matches_hash[field].eql?(stmt.last)
    return !@opts[:force]
  end
  false
end
hashed(match_data) click to toggle source
# File lib/havox/classes/rule.rb, line 58
def hashed(match_data)
  Hash[match_data.names.map(&:to_sym).zip(match_data.captures)]             # Converts the match data to a hash object.
end
parsed_actions(raw_rule) click to toggle source
# File lib/havox/classes/rule.rb, line 47
def parsed_actions(raw_rule)
  ok_actions = []
  raw_actions = raw_rule.split('->').last.strip                             # Parses the actions in the 2nd part.
  raw_actions = raw_actions.split(/(?<=\))\s+(?=\w)/)                       # Splits the string into single raw actions.
  raw_actions.each do |raw_action|
    regex = /(?<action>\w+)\((?<arg_a>[\w<>]+)[,\s]*(?<arg_b>[\w<>]*)\)/    # Matches raw actions in the format 'Action(x, y)'.
    ok_actions << hashed(raw_action.match(regex))                           # Adds the structured action to the returning array.
  end
  translate.actions_to(@syntax, ok_actions, @opts)
end
parsed_matches(raw_rule) click to toggle source
# File lib/havox/classes/rule.rb, line 34
def parsed_matches(raw_rule)
  ok_matches = {}
  raw_matches = raw_rule.split('->').first.split('and')                     # Parses each match field in the 1st part.
  raw_matches = raw_matches.map { |str| str.tr('()*', '').strip }           # Removes unwanted characters.
  raw_matches = raw_matches.reject(&:empty?)                                # Rejects resulting empty match fields.
  raw_matches.each do |raw_match|                                           # Parses and adds each match based on the dictionary.
    stmt = raw_match.split(/\s?=\s?/)                                       # Splits the statement by '='.
    field = translate.fields_to(@syntax)[stmt.first]                        # Gets the right field by the raw field name.
    ok_matches[field] = stmt.last unless already_set?(ok_matches, stmt)
  end
  translate.matches_to(@syntax, ok_matches)
end
translate() click to toggle source
# File lib/havox/classes/rule.rb, line 62
def translate
  Havox::Translator.instance
end