Class: AIHype::Blacklist

Inherits:
Object
  • Object
show all
Defined in:
lib/aihype/blacklist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory_file) ⇒ Blacklist



10
11
12
13
# File 'lib/aihype/blacklist.rb', line 10

def initialize(memory_file)
  @rules = []
  load_rules(memory_file)
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



8
9
10
# File 'lib/aihype/blacklist.rb', line 8

def rules
  @rules
end

Class Method Details

.from_file(file_path) ⇒ Object



15
16
17
18
19
20
# File 'lib/aihype/blacklist.rb', line 15

def self.from_file(file_path)
  memory_file = Memory.load(file_path)
  raise InvalidMemoryFileError, "Memory file not found: #{file_path}" unless memory_file

  new(memory_file)
end

Instance Method Details

#add_rule(content, category: :user_defined) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/aihype/blacklist.rb', line 34

def add_rule(content, category: :user_defined)
  existing = find_by_content(content)
  return existing if existing

  rule = BlacklistRule.new(content: content, category: category)
  @rules << rule
  rule
end

#disable_rule(content) ⇒ Object



43
44
45
46
# File 'lib/aihype/blacklist.rb', line 43

def disable_rule(content)
  rule = find_by_content(content)
  rule&.disable
end

#enable_rule(content) ⇒ Object



48
49
50
51
# File 'lib/aihype/blacklist.rb', line 48

def enable_rule(content)
  rule = find_by_content(content)
  rule&.enable
end

#enabled_rulesObject



22
23
24
# File 'lib/aihype/blacklist.rb', line 22

def enabled_rules
  @rules.select(&:enabled?)
end

#find_by_content(content) ⇒ Object



30
31
32
# File 'lib/aihype/blacklist.rb', line 30

def find_by_content(content)
  @rules.find { |r| r.content.downcase == content.downcase }
end

#rule_contentsObject



26
27
28
# File 'lib/aihype/blacklist.rb', line 26

def rule_contents
  enabled_rules.map(&:content)
end