Class: Splam::Rule
- Inherits:
-
Object
- Object
- Splam::Rule
- Defined in:
- lib/splam/rule.rb
Direct Known Subclasses
Splam::Rules::ArmsRace, Splam::Rules::BadWords, Splam::Rules::Bbcode, Splam::Rules::Chinese, Splam::Rules::Fuzz, Splam::Rules::GoodWords, Splam::Rules::Href, Splam::Rules::Html, Splam::Rules::Httpbl, Splam::Rules::LineLength, Splam::Rules::Punctuation, Splam::Rules::Russian, Splam::Rules::User, Splam::Rules::WordLength
Class Attribute Summary collapse
-
.default_rules ⇒ Object
Global set of rules for all splammable classes.
-
.rules ⇒ Object
readonly
Index linking all splam_keys to the rule classes.
- .splam_key ⇒ Object
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#reasons ⇒ Object
Returns the value of attribute reasons.
-
#score ⇒ Object
Returns the value of attribute score.
-
#suite ⇒ Object
readonly
Returns the value of attribute suite.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Class Method Summary collapse
Instance Method Summary collapse
- #add_score(points, reason) ⇒ Object
-
#initialize(suite, record, weight = 1.0, request = nil) ⇒ Rule
constructor
A new instance of Rule.
- #line_safe?(string) ⇒ Boolean
- #name ⇒ Object
-
#run ⇒ Object
Overload this method to run your rule.
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_rules ⇒ Object
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 |
.rules ⇒ Object (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_key ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
50 51 52 |
# File 'lib/splam/rule.rb', line 50 def body @body end |
#reasons ⇒ Object
Returns the value of attribute reasons.
51 52 53 |
# File 'lib/splam/rule.rb', line 51 def reasons @reasons end |
#score ⇒ Object
Returns the value of attribute score.
51 52 53 |
# File 'lib/splam/rule.rb', line 51 def score @score end |
#suite ⇒ Object (readonly)
Returns the value of attribute suite.
50 51 52 |
# File 'lib/splam/rule.rb', line 50 def suite @suite end |
#weight ⇒ Object (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
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 |
#name ⇒ Object
38 39 40 |
# File 'lib/splam/rule.rb', line 38 def name self.class.splam_key end |
#run ⇒ Object
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 |