Class: Splam::Suite

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



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

def body
  @body
end

#conditionsObject

Returns the value of attribute conditions

Returns:

  • (Object)

    the current value of conditions



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

def conditions
  @conditions
end

#reasonsObject (readonly)

Returns the value of attribute reasons.



17
18
19
# File 'lib/splam.rb', line 17

def reasons
  @reasons
end

#requestObject

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

#rulesObject

Returns the value of attribute rules

Returns:

  • (Object)

    the current value of rules



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

def rules
  @rules
end

#scoreObject (readonly)

Returns the value of attribute score.



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

def score
  @score
end

#thresholdObject

Returns the value of attribute threshold

Returns:

  • (Object)

    the current value of 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

Returns:

  • (Boolean)


43
44
45
# File 'lib/splam.rb', line 43

def splam?(score)
  score >= threshold
end