Class: NewRelic::Agent::RulesEngine::ReplacementRule
- Inherits:
-
Object
- Object
- NewRelic::Agent::RulesEngine::ReplacementRule
- Defined in:
- lib/new_relic/agent/rules_engine/replacement_rule.rb
Instance Attribute Summary collapse
- #each_segment ⇒ Object readonly
- #eval_order ⇒ Object readonly
- #ignore ⇒ Object readonly
- #match_expression ⇒ Object readonly
- #replace_all ⇒ Object readonly
- #replacement ⇒ Object readonly
- #terminate_chain ⇒ Object readonly
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #apply(string) ⇒ Object
- #apply_replacement(string) ⇒ Object
- #apply_to_each_segment(string) ⇒ Object
-
#initialize(options) ⇒ ReplacementRule
constructor
A new instance of ReplacementRule.
- #matches?(string) ⇒ Boolean
- #terminal? ⇒ Boolean
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() if !['match_expression'] raise ArgumentError.new('missing required match_expression') end if !['replacement'] && !['ignore'] raise ArgumentError.new('must specify replacement when ignore is false') end @match_expression = Regexp.new(['match_expression'], Regexp::IGNORECASE) @replacement = ['replacement'] @ignore = ['ignore'] || false @eval_order = ['eval_order'] || 0 @replace_all = ['replace_all'] || false @each_segment = ['each_segment'] || false @terminate_chain = ['terminate_chain'] || false end |
Instance Attribute Details
#each_segment ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def each_segment @each_segment end |
#eval_order ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def eval_order @eval_order end |
#ignore ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def ignore @ignore end |
#match_expression ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def match_expression @match_expression end |
#replace_all ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def replace_all @replace_all end |
#replacement ⇒ Object (readonly)
9 10 11 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 9 def replacement @replacement end |
#terminate_chain ⇒ Object (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
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
29 30 31 |
# File 'lib/new_relic/agent/rules_engine/replacement_rule.rb', line 29 def terminal? @terminate_chain || @ignore end |