Class: Spidr::Rules
- Inherits:
-
Object
- Object
- Spidr::Rules
- Defined in:
- lib/spidr/rules.rb
Overview
The Rules class represents collections of acceptance and rejection rules, which are used to filter data.
Instance Attribute Summary collapse
-
#accept ⇒ Object
readonly
Accept rules.
-
#reject ⇒ Object
readonly
Reject rules.
Instance Method Summary collapse
-
#accept?(data) ⇒ Boolean
Determines whether the data should be accepted or rejected.
-
#initialize(accept: nil, reject: nil) ⇒ Rules
constructor
Creates a new Rules object.
-
#reject?(data) ⇒ Boolean
Determines whether the data should be rejected or accepted.
-
#test_data(data, rule) ⇒ Boolean
protected
Tests the given data against a given pattern.
Constructor Details
#initialize(accept: nil, reject: nil) ⇒ Rules
Creates a new Rules object.
25 26 27 28 29 30 31 |
# File 'lib/spidr/rules.rb', line 25 def initialize(accept: nil, reject: nil) @accept = [] @reject = [] @accept += accept if accept @reject += reject if reject end |
Instance Attribute Details
#accept ⇒ Object (readonly)
Accept rules
11 12 13 |
# File 'lib/spidr/rules.rb', line 11 def accept @accept end |
#reject ⇒ Object (readonly)
Reject rules
14 15 16 |
# File 'lib/spidr/rules.rb', line 14 def reject @reject end |
Instance Method Details
#accept?(data) ⇒ Boolean
Determines whether the data should be accepted or rejected.
40 41 42 43 44 45 46 |
# File 'lib/spidr/rules.rb', line 40 def accept?(data) unless @accept.empty? @accept.any? { |rule| test_data(data,rule) } else !@reject.any? { |rule| test_data(data,rule) } end end |
#reject?(data) ⇒ Boolean
Determines whether the data should be rejected or accepted.
55 56 57 |
# File 'lib/spidr/rules.rb', line 55 def reject?(data) !accept?(data) end |
#test_data(data, rule) ⇒ Boolean (protected)
Tests the given data against a given pattern.
67 68 69 70 71 72 73 74 75 |
# File 'lib/spidr/rules.rb', line 67 def test_data(data,rule) if rule.kind_of?(Proc) rule.call(data) == true elsif rule.kind_of?(Regexp) !((data.to_s =~ rule).nil?) else data == rule end end |