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(options={}) click to toggle source

Creates a new Rules object.

@param [Hash] options

Additional options.

@option options [Array<String, Regexp, Proc>] :accept

The patterns to accept data with.

@option options [Array<String, Regexp, Proc>] :reject

The patterns to reject data with.
# File lib/spidr/rules.rb, line 26
def initialize(options={})
  @accept = []
  @reject = []

  @accept += options[:accept] if options[:accept]
  @reject += options[:reject] if options[: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 41
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 56
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 68
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