Class: OneApm::Support::RulesEngine::ReplacementRule
- Inherits:
-
Object
- Object
- OneApm::Support::RulesEngine::ReplacementRule
- Defined in:
- lib/one_apm/support/rules_engine/replacement_rule.rb
Instance Attribute Summary collapse
-
#each_segment ⇒ Object
readonly
Returns the value of attribute each_segment.
-
#eval_order ⇒ Object
readonly
Returns the value of attribute eval_order.
-
#ignore ⇒ Object
readonly
Returns the value of attribute ignore.
-
#match_expression ⇒ Object
readonly
Returns the value of attribute match_expression.
-
#replace_all ⇒ Object
readonly
Returns the value of attribute replace_all.
-
#replacement ⇒ Object
readonly
Returns the value of attribute replacement.
-
#terminate_chain ⇒ Object
readonly
Returns the value of attribute terminate_chain.
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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 16 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)
Returns the value of attribute each_segment.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def each_segment @each_segment end |
#eval_order ⇒ Object (readonly)
Returns the value of attribute eval_order.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def eval_order @eval_order end |
#ignore ⇒ Object (readonly)
Returns the value of attribute ignore.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def ignore @ignore end |
#match_expression ⇒ Object (readonly)
Returns the value of attribute match_expression.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def match_expression @match_expression end |
#replace_all ⇒ Object (readonly)
Returns the value of attribute replace_all.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def replace_all @replace_all end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def replacement @replacement end |
#terminate_chain ⇒ Object (readonly)
Returns the value of attribute terminate_chain.
8 9 10 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 8 def terminate_chain @terminate_chain end |
Instance Method Details
#<=>(other) ⇒ Object
74 75 76 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 74 def <=>(other) eval_order <=> other.eval_order end |
#apply(string) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Object
57 58 59 60 |
# 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) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# 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) ⇒ Boolean
37 38 39 40 41 42 43 44 45 |
# 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? ⇒ Boolean
33 34 35 |
# File 'lib/one_apm/support/rules_engine/replacement_rule.rb', line 33 def terminal? @terminate_chain || @ignore end |