class Spidr::Rules

The {Rules} class represents collections of acceptance and rejection rules, which are used to filter data.

Attributes

accept[R]

Accept rules

reject[R]

Reject rules

Public Class Methods

new(accept: nil, reject: nil) click to toggle source

Creates a new Rules object.

@param [Array<String, Regexp, Proc>, nil] accept

The patterns to accept data with.

@param [Array<String, Regexp, Proc>, nil] reject

The patterns to reject data with.
# File lib/spidr/rules.rb, line 25
def initialize(accept: nil, reject: nil)
  @accept = []
  @reject = []

  @accept += accept if accept
  @reject += reject if reject
end

Public Instance Methods

accept?(data) click to toggle source

Determines whether the data should be accepted or rejected.

@return [Boolean]

Specifies whether the given data was accepted, using the rules
acceptance patterns.
# 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) click to toggle source

Determines whether the data should be rejected or accepted.

@return [Boolean]

Specifies whether the given data was rejected, using the rules
rejection patterns.
# File lib/spidr/rules.rb, line 55
def reject?(data)
  !accept?(data)
end

Protected Instance Methods

test_data(data,rule) click to toggle source

Tests the given data against a given pattern.

@return [Boolean]

Specifies whether the given data matched the pattern.
# 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