Class: Havox::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/havox/classes/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, opts = {}) ⇒ Rule

Returns a new instance of Rule.



5
6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



3
4
5
# File 'lib/havox/classes/rule.rb', line 3

def actions
  @actions
end

#dp_idObject (readonly)

Returns the value of attribute dp_id.



3
4
5
# File 'lib/havox/classes/rule.rb', line 3

def dp_id
  @dp_id
end

#matchesObject (readonly)

Returns the value of attribute matches.



3
4
5
# File 'lib/havox/classes/rule.rb', line 3

def matches
  @matches
end

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'lib/havox/classes/rule.rb', line 3

def raw
  @raw
end

Instance Method Details

#inspectObject



24
25
26
# 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_hObject



28
29
30
# File 'lib/havox/classes/rule.rb', line 28

def to_h
  { dp_id: @dp_id, matches: @matches, actions: @actions }
end

#to_sObject

Stringifies the rule.



15
16
17
18
19
20
21
22
# 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