Class: Splam::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/splam/rule.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suite, record, weight = 1.0) ⇒ Rule

Returns a new instance of Rule.



33
34
35
# File 'lib/splam/rule.rb', line 33

def initialize(suite, record, weight = 1.0)
  @suite, @weight, @score, @reasons, @body = suite, weight, 0, [], record.send(suite.body)
end

Class Attribute Details

.default_rulesObject

Global set of rules for all splammable classes. By default it is an array of all Splam::Rule subclasses. It can be set to a subset of all rules, or even a hash with specified weights.

self.default_rules = [:bad_words, :bbcode]
self.default_rules = {:bad_words => 0.5, :bbcode => 7}


10
11
12
# File 'lib/splam/rule.rb', line 10

def default_rules
  @default_rules
end

.rulesObject (readonly)

Index linking all splam_keys to the rule classes. This is populated automatically.



13
14
15
# File 'lib/splam/rule.rb', line 13

def rules
  @rules
end

.splam_keyObject



15
16
17
# File 'lib/splam/rule.rb', line 15

def splam_key
  @splam_key || (self.splam_key = name.demodulize.underscore.to_sym)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



49
50
51
# File 'lib/splam/rule.rb', line 49

def body
  @body
end

#reasonsObject

Returns the value of attribute reasons.



50
51
52
# File 'lib/splam/rule.rb', line 50

def reasons
  @reasons
end

#scoreObject

Returns the value of attribute score.



50
51
52
# File 'lib/splam/rule.rb', line 50

def score
  @score
end

#suiteObject (readonly)

Returns the value of attribute suite.



49
50
51
# File 'lib/splam/rule.rb', line 49

def suite
  @suite
end

#weightObject (readonly)

Returns the value of attribute weight.



49
50
51
# File 'lib/splam/rule.rb', line 49

def weight
  @weight
end

Class Method Details

.inherited(_subclass) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/splam/rule.rb', line 41

def self.inherited(_subclass)
  @rules      ||= {}
  @default_rules ||= []
  @default_rules << _subclass
  _subclass.splam_key
  super
end

.run(*args) ⇒ Object



26
27
28
29
30
# File 'lib/splam/rule.rb', line 26

def run(*args)
  rule = new(*args)
  rule.run
  rule
end

Instance Method Details

#add_score(points, reason) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/splam/rule.rb', line 65

def add_score(points, reason)
  @score ||= 0
  if points != 0
    @reasons << "#{name}: [#{points}#{" * #{weight}" if weight != 1}] #{reason}"
    points = points * weight.to_i
    @score += points
  end
end

#nameObject



37
38
39
# File 'lib/splam/rule.rb', line 37

def name
  self.class.splam_key
end

#runObject

Overload this method to run your rule. Call #add_score to modify the suiteā€™s splam score.

def run
  add_score -5, 'water'
  add_score  5, 'PBR'
  add_score 10, 'black butte'
  add_score 30, 'red wine'
  add_score 95, 'everclear'
end


62
63
# File 'lib/splam/rule.rb', line 62

def run
end