class OneApm::Support::RulesEngine::ReplacementRule

Attributes

each_segment[R]
eval_order[R]
ignore[R]
match_expression[R]
replace_all[R]
replacement[R]
terminate_chain[R]

Public Class Methods

new(options) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 16
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

Public Instance Methods

<=>(other) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 74
def <=>(other)
  eval_order <=> other.eval_order
end
apply(string) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 47
def apply(string)
  if @ignore
    nil
  elsif @each_segment
    apply_to_each_segment(string)
  else
    apply_replacement(string)
  end
end
apply_replacement(string) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 57
def apply_replacement(string)
  method = @replace_all ? :gsub : :sub
  string.send(method, @match_expression, @replacement)
end
apply_to_each_segment(string) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 62
def apply_to_each_segment(string)
  string        = string.dup
  leading_slash = string.slice!(OA_LEADING_SLASH_REGEX)
  segments      = string.split(OA_SEGMENT_SEPARATOR)

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

  "#{leading_slash}#{segments.join(OA_SEGMENT_SEPARATOR)}"
end
matches?(string) click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 37
def matches?(string)
  if @each_segment
    string.split(OA_SEGMENT_SEPARATOR).any? do |segment|
      segment.match(@match_expression)
    end
  else
    string.match @match_expression
  end
end
terminal?() click to toggle source
# File lib/one_apm/support/rules_engine/replacement_rule.rb, line 33
def terminal?
  @terminate_chain || @ignore
end