Class: NewRelic::Agent::RulesEngine::ReplacementRule

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/rules_engine/replacement_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ReplacementRule

Returns a new instance of ReplacementRule.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 12

def initialize(options)
  if !options['match_expression']
    raise ArgumentError.new('missing required match_expression')
  end
  if !options['replacement'] && !options['ignore']
    raise ArgumentError.new('must specify replacement when ignore is false')
  end

  @match_expression = Regexp.new(options['match_expression'], Regexp::IGNORECASE)
  @replacement = options['replacement']
  @ignore = options['ignore'] || false
  @eval_order = options['eval_order'] || 0
  @replace_all = options['replace_all'] || false
  @each_segment = options['each_segment'] || false
  @terminate_chain = options['terminate_chain'] || false
end

Instance Attribute Details

#each_segmentObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def each_segment
  @each_segment
end

#eval_orderObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def eval_order
  @eval_order
end

#ignoreObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def ignore
  @ignore
end

#match_expressionObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def match_expression
  @match_expression
end

#replace_allObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def replace_all
  @replace_all
end

#replacementObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def replacement
  @replacement
end

#terminate_chainObject (readonly)



9
10
11
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9

def terminate_chain
  @terminate_chain
end

Instance Method Details

#<=>(other) ⇒ Object



70
71
72
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 70

def <=>(other)
  eval_order <=> other.eval_order
end

#apply(string) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 43

def apply(string)
  if @ignore
    nil
  elsif @each_segment
    apply_to_each_segment(string)
  else
    apply_replacement(string)
  end
end

#apply_replacement(string) ⇒ Object



53
54
55
56
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 53

def apply_replacement(string)
  method = @replace_all ? :gsub : :sub
  string.send(method, @match_expression, @replacement)
end

#apply_to_each_segment(string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 58

def apply_to_each_segment(string)
  string = string.dup
  leading_slash = string.slice!(LEADING_SLASH_REGEX)
  segments = string.split(SEGMENT_SEPARATOR)

  segments.map! do |segment|
    apply_replacement(segment)
  end

  "#{leading_slash}#{segments.join(SEGMENT_SEPARATOR)}"
end

#matches?(string) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 33

def matches?(string)
  if @each_segment
    string.split(SEGMENT_SEPARATOR).any? do |segment|
      segment.match(@match_expression)
    end
  else
    string.match(@match_expression)
  end
end

#terminal?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 29

def terminal?
  @terminate_chain || @ignore
end