Class: Splam::Suite
- Inherits:
-
Struct
- Object
- Struct
- Splam::Suite
- Defined in:
- lib/splam.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#reasons ⇒ Object
readonly
Returns the value of attribute reasons.
-
#request ⇒ Object
Should be a Rack::Request, in case you want to inspect user agents and whatnot unimplemented, cry about it fanboy!.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#initialize(body, rules, threshold, conditions, &block) ⇒ Suite
constructor
A new instance of Suite.
- #run(record, request) ⇒ Object
- #splam?(score) ⇒ Boolean
Constructor Details
#initialize(body, rules, threshold, conditions, &block) ⇒ Suite
Returns a new instance of Suite.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/splam.rb', line 19 def initialize(body, rules, threshold, conditions, &block) super(body, rules, threshold, conditions) block.call(self) if block self.rules = self.rules.inject({}) do |memo, (rule, weight)| if (rule.is_a?(Class) && rule.superclass == Splam::Rule) || rule = Splam::Rule.rules[rule] memo[rule] = weight || 1.0 else raise ArgumentError, "Invalid rule: #{rule.inspect}" end memo end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
11 12 13 |
# File 'lib/splam.rb', line 11 def body @body end |
#conditions ⇒ Object
Returns the value of attribute conditions
11 12 13 |
# File 'lib/splam.rb', line 11 def conditions @conditions end |
#reasons ⇒ Object (readonly)
Returns the value of attribute reasons.
17 18 19 |
# File 'lib/splam.rb', line 17 def reasons @reasons end |
#request ⇒ Object
Should be a Rack::Request, in case you want to inspect user agents and whatnot unimplemented, cry about it fanboy!
14 15 16 |
# File 'lib/splam.rb', line 14 def request @request end |
#rules ⇒ Object
Returns the value of attribute rules
11 12 13 |
# File 'lib/splam.rb', line 11 def rules @rules end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
16 17 18 |
# File 'lib/splam.rb', line 16 def score @score end |
#threshold ⇒ Object
Returns the value of attribute threshold
11 12 13 |
# File 'lib/splam.rb', line 11 def threshold @threshold end |
Instance Method Details
#run(record, request) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/splam.rb', line 32 def run(record, request) score, reasons = 0, [] rules.each do |rule_class, weight| weight ||= 1 worker = rule_class.run(self, record, weight, request) score += worker.score reasons << worker.reasons end [score, reasons] end |
#splam?(score) ⇒ Boolean
43 44 45 |
# File 'lib/splam.rb', line 43 def splam?(score) score >= threshold end |