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, request = nil) ⇒ Rule

Returns a new instance of Rule.



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

def initialize(suite, record, weight = 1.0, request = nil)
  @suite, @weight, @score, @reasons, @body, @request = suite, weight, 0, [], record.send(suite.body), request
  @user = record.user # todo: customize user field
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.



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

def body
  @body
end

#reasonsObject

Returns the value of attribute reasons.



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

def reasons
  @reasons
end

#scoreObject

Returns the value of attribute score.



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

def score
  @score
end

#suiteObject (readonly)

Returns the value of attribute suite.



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

def suite
  @suite
end

#weightObject (readonly)

Returns the value of attribute weight.



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

def weight
  @weight
end

Class Method Details

.inherited(_subclass) ⇒ Object



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

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



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

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

#line_safe?(string) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/splam/rule.rb', line 75

def line_safe?(string)
  ([
    /\.dylib\b/,
    /\b0x[0-9a-f]{6,16}\b/i,
    /\b\/Applications\//,
    /\b\/System\/Library\//,
    /\bLibrary\/Application Support\//
  ].map {|r| r.match string }).compact.size > 0
end

#nameObject



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

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


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

def run
end