Class: Promptmenot::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/promptmenot/pattern.rb

Constant Summary collapse

SENSITIVITY_LEVELS =
%i[low medium high paranoid].freeze
CONFIDENCE_LEVELS =
%i[high medium low].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, category:, regex:, sensitivity: :medium, confidence: :medium) ⇒ Pattern

Returns a new instance of Pattern.



10
11
12
13
14
15
16
17
18
19
# File 'lib/promptmenot/pattern.rb', line 10

def initialize(name:, category:, regex:, sensitivity: :medium, confidence: :medium)
  validate_sensitivity!(sensitivity)
  validate_confidence!(confidence)

  @name = name.to_sym
  @category = category.to_sym
  @regex = regex
  @sensitivity = sensitivity.to_sym
  @confidence = confidence.to_sym
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



8
9
10
# File 'lib/promptmenot/pattern.rb', line 8

def category
  @category
end

#confidenceObject (readonly)

Returns the value of attribute confidence.



8
9
10
# File 'lib/promptmenot/pattern.rb', line 8

def confidence
  @confidence
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/promptmenot/pattern.rb', line 8

def name
  @name
end

#regexObject (readonly)

Returns the value of attribute regex.



8
9
10
# File 'lib/promptmenot/pattern.rb', line 8

def regex
  @regex
end

#sensitivityObject (readonly)

Returns the value of attribute sensitivity.



8
9
10
# File 'lib/promptmenot/pattern.rb', line 8

def sensitivity
  @sensitivity
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



42
43
44
# File 'lib/promptmenot/pattern.rb', line 42

def ==(other)
  other.is_a?(Pattern) && name == other.name && category == other.category
end

#active_at?(level) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/promptmenot/pattern.rb', line 21

def active_at?(level)
  level_index = SENSITIVITY_LEVELS.index(level.to_sym)
  pattern_index = SENSITIVITY_LEVELS.index(@sensitivity)
  return false unless level_index && pattern_index

  level_index >= pattern_index
end

#hashObject



48
49
50
# File 'lib/promptmenot/pattern.rb', line 48

def hash
  [name, category].hash
end

#match(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/promptmenot/pattern.rb', line 29

def match(text)
  matches = []
  text.to_s.scan(regex) do
    match_data = Regexp.last_match
    matches << Match.new(
      pattern: self,
      matched_text: match_data[0],
      position: match_data.begin(0)...match_data.end(0)
    )
  end
  matches
end